Windows IoT Raspberry Communication (Server – Client)

In this tutorial, I will show you how can you develop a simple but effective communications protocol between a raspberry which serves as a server (or computer, the code is universal) and one or more raspberry’s. In this tutorial, we will be using Windows IoT on the Raspberry PI. Our programming language will be c# and will be using StreamSockets. If you have never worked with streamsockets before, then I suggest that you read about streamsockets on MSDN first.



  • One or more raspberry. You can simulate the server/client on your computer if you wish.
  • We will be using Visual Studio for our development. A free community version is available on
  • I assume that you have basic programming knowledge before trying this tutorial.

What will we make?


  • A simple client program which sends “Hello WindowsInstructed” to the Raspberry server. The Raspberry server will then reply Hello Client & his hostname.

Once you we did that you will have the required knowledge to advance the hello world messages and make your own chat room using Raspberry’s!

Step 1: Adding the right Dependencies.


We skip creating the project. If you haven’t made it yet, choose New project, Windows Universal, Blank App. I’ve made a new class file called StreamSocketClass, they choice is up to you if you want to do this.

The StreamSocket class uses the Windows.Networking namespace. I recommend you include all these namespaces into your project. We will be using them all.

Step 2: Setting the right capabilities for your App.


Because you’re using networking capabilities you will need to set the correct capabilities for your project. Double-click Package.appxmanifest and click on Capabilities.

Make sure to choose the following capabilities. As shown below.

  • Internet (Client & Server)
  • Internet (Client)
  • Private Networks (Client & Server)



Step 3: Creating the Listener.


The listener will be responsible for listening for connections on either the client or server. It’s quite a simple class, but it’s very important. Let’s begin by creating the empty method called DataListener_ConnectionReceived.

This method will be responsible for listening and handling all requests. So it’s very important that we make this work as best as we can. For our simple demo, it doesn’t have to be very complex. But I do want to show you how this works. The input is a stream so we will need a string builder to transform it into a string. We will therefore first create a StringBuilder in the DataListener_ConnectionReceived method. Our DataReader will keep on reading till the buffer (unconsumed buffer) is less than 0. Once that’s done it will detach the stream and the StringBuilder will make it into readable text and place it into the DataReceived string. There isn’t much you can change here so just copy and paste it.

Once that has been created we can start on making the logic for the listener. The listener will have two tasks. 1) Decide who is listening, is it the server or client. 2) Sent a response if he is the server, otherwise print the received data. Let’s first begin by creating some variables which we will use to change the state of the program, in your final product you might want to change this but for the demo, it’s just fine.

I’ve added the following line right under the class declaration. That way it’s accessible for all methods in the class and outside the class which might be useful if you wish to create a UI for your application which depends on the state.

Now we can begin with creating the logic for your server and client. Which is really simply just make an IF statement which uses the IsServer property and then switch what the program has to do. Since we do not have a send functionality yet we will have to come back to the server if at a later stage. My final code looks like this for the listener (at this time).

As you can see I’ve used args.Socket.Information.RemoteHostName. This gives you back the HostName of the sender. So for example Raspberry2 if that’s what you raspberry is called. We will use args.Socket.Information again at a later stage in this tutorial.


Step 4: Creating the Listening Method


Now that we’ve made the listening handler we will need to create a small class that opens the ports and tell the program what it has to do. This method only has to be called a single time and could also be placed in the initialization of the app in the MainPage file. However, I recommend you don’t. It’s also important to add a new variable in your class called ServerPort. Make sure to make this method Public since we will be needing to call this outside this class file.

Step 5: Opening the Listening ports.


This is a very important step in your program. Once the program starts it has to open its ports otherwise, the server and or client cannot connect to each other. The following code should, therefore, be placed in your App Startup. I placed it in the MainPage where the project initializes.

We begin of course with declaring and creating the class in the MainPage, otherwise, we can’t call it’s methods. I assume that you know how to do this and won’t go into any details. We will be calling the public method we created in step 4 in this step, so go ahead and add that to your MainPage file in the MainPage method. Once you did that your mainpage should look like this:

Congratulations! You can now listen for connections and read the data you receive. You can also decide if it’s a client who sent it or a server! Next step is sending data!

Step 6: Creating the Data Sender


We will first be creating the DataSender. This part is the most complex one and I suggest you read on MSDN what each function does and not blindly copy and paste it. Firstly we need to declare some more variables in the class itself. You will need to add this one:



I’ve commented some parts to make it easier to understand. However, this is just how it works and there isn’t much that you can change.

Step 7: Back to the MainPage


We now have most of our code done. We will now need to change the startup behavior of the application. Go back to your MainPage file and add code for checking if it’s a server or client and then use the SentResponse method to sent data to the server if it’s the client starting the app. Make sure that you declare the IsServer variable first! And that you create the Hostname variable and set it for the server (Add using Windows.Networking; to your mainpage ;)) Your code and the mainpage file should look something like this.


Step 8: Making the server respond!


You’ve reached the last step of this tutorial! Good job! Now we need to make the server respond to the client and sent back Hello Client! We can do this by going back to the DataListener_ConnectionReceived method and adding a single line! The SentResponse method and instead of ServerAdress we will be using the Connection Adress which you can get by using args.Socket.Information.RemoteAddress. You should really be able to program this yourself by now, and once done it should look like something like this.


Final Code for StreamSocketClass


Final code for MainPage


Good to know!


  • The code has no protection for making sure there is a server. So always start the server before the client.
  • The code has ServerAdress set to my system name make sure to change this to the correct device name
  • The code might cause your Security software on Windows to go on alert. Make sure to allow the program to communicate in your Firewall settings on port 12345 or change the port number.

You can download the entire project here: WindowsInstructed Demo

Example output


Server (Got Hello WindowsInstructed and sent Reply)

Client (Sent Hello WindowsInstrucred and got Reply)


What's your reaction?
Solved it!
Helped me
Didn't work
Inaccurate Information
About The Author
Yuri Pustjens
2014, 2015, 2016, 2017 and 2018 Microsoft Most Valuable Professional Windows Yuri specializes in troubleshooting (crash analysis and devices), system resource utilisation and system performance. He is also very interested in Customer Security. Yuri can be found as Windows Community Moderator and Wiki Author on Microsoft Answers and is active on different communities all across the web.
Leave a response
  • Satnam Malhi
    May 22, 2018 at 4:01 am

    Thanks sooooo much. I can’t describe how helpful this page has been for me to setup a TCP server on my Raspberry pi. I was going around in circles for three days …. until I saw the picture, All I had to do was “enable Private Networks Client & Server” capability CHECK BOX! This is such a relief, nowhere on the internet does ANYBODY mention this little secret, except here.
    Once I checked the box, my original code worked like a charm!
    Once again, THANKS A MILLION!
    I love you man!
    – Satnam

  • Andrea Sacchi
    September 1, 2017 at 10:29 am

    When i put im my code “StreamSocketClass SocketManager = new StreamSocketClass();” It says that Namespace StreamSocketClass has not found.
    i put on top of the code “using Windows.Networking.Sockets;” but i have the same issues
    What did i do wrong?

Leave a Response