Things used in this project

Hardware components:
Pi 3 02
Raspberry Pi 3 Model B
Software apps and online services:
Microsoft Visual Studio 2015


Server Side - Raspberry piC#
Listen to a port (e.g 8800)
private async void listen()
                //Create a StreamSocketListener to start listening for TCP connections.
                Windows.Networking.Sockets.StreamSocketListener socketListener = new Windows.Networking.Sockets.StreamSocketListener();

                //Hook up an event handler to call when connections are received.
                socketListener.ConnectionReceived += SocketListener_ConnectionReceived;

                //Start listening for incoming TCP connections on the specified port. You can specify any port that' s not currently in use.
                await socketListener.BindServiceNameAsync("8800");
            catch (Exception e)
                if (OnError != null)

Server read from clientC#
private async void SocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender,
    Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)

            StringBuilder request = new StringBuilder();
            byte[] data = new byte[BuffreSize];
            IBuffer buffer = data.AsBuffer();
            StreamSocket socket = args.Socket;
            using (IInputStream input = socket.InputStream)
                buffer = await input.ReadAsync(buffer, BuffreSize, InputStreamOptions.Partial);
                request.Append(Encoding.UTF8.GetString(data, 0, (int)buffer.Length));
Server send data to clientC#
using (var outputStream = socket.OutputStream)
                using (Stream resp = outputStream.AsStreamForWrite())
                    var message = request.ToString()+" sent";
                    byte[] headerArray = Encoding.UTF8.GetBytes(message);
                    await resp.WriteAsync(headerArray, 0, headerArray.Length);
                    await resp.FlushAsync();
Sample ClientC#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class AsynchronousSocketClient
    public static void Main(String[] args)
        Connect("","Hello World");
    static void Connect(String server, String message)
            // Create a TcpClient.
            // Note, for this client to work you need to have a TcpServer 
            // connected to the same address as specified by the server, port
            // combination.
            Int32 port = 8800;
            TcpClient client = new TcpClient(server, port);

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // Get a client stream for reading and writing.
            //  Stream stream = client.GetStream();

            NetworkStream stream = client.GetStream();

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            Console.WriteLine("Sent: {0}", message);

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            data = new Byte[1024];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            Console.WriteLine("Received: {0}", responseData+"0");

            // Close everything.
        catch (ArgumentNullException e)
            Console.WriteLine("ArgumentNullException: {0}", e);
        catch (SocketException e)
            Console.WriteLine("SocketException: {0}", e);

        Console.WriteLine("\n Press Enter to continue...");



3x4 pjqx9uhj4o
Arioo Barzan
1 project • 2 followers
Desktop Programmer Web Developer Mobile Developer Network Programmer Graphic Designer


Add projectSign up / Login