Things used in this project

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

Code

Server Side - Raspberry piC#
Listen to a port (e.g 8800)
private async void listen()
        {
            try
            {
                //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)
                    OnError(e.Message);
            }

        }
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("192.168.1.77","Hello World");
    }
    static void Connect(String server, String message)
    {
        try
        {
            // 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.
            stream.Close();
            client.Close();
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }

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

}

Credits

3x4 pjqx9uhj4o
Arioo Barzan

Desktop Programmer Web Developer Mobile Developer Network Programmer Graphic Designer

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Winforms+Lattepanda=Control Everything With Your Mouse!
Easy
  • 1,659
  • 11

Protip

If you use other things instead of the LED, then you can use your mouse to control everything!!

Sorting Hat: Circuit Playground Express Starter Project
Easy
  • 393
  • 5

Protip

Which Hogwarts House do you belong in? A quick first project for this beginner-friendly board from Adafruit.

Getting Started - Cortana Skills, Bot Framework, RaspberryPi
Easy
  • 1,569
  • 10

Protip

Gentle introduction to Cortana Skills and Bot Framework connecting to Raspberry Pi, using Cortana Skills to Echo sent message.

Awesome Azure IoT
Easy
  • 250
  • 1

A curated list of awesome Azure Internet of Things projects and resources.

Microsoft Workshop Setup Guide
Easy
  • 1,435
  • 12

Protip

A collection of screencasts for setting up your laptop for the Hackster/Microsoft IoT Roadshow events.

One Small Step For a Yedi, One Giant Leap for Mankind
Easy
  • 243
  • 4

Work in progress

Counting your steps with the Calliope Mini. Hearing the Star Wars theme is always a huge motivator.

Add projectSign up / Login