Search code examples
c#socketsuwpiotwindowsiot

How to connect to UWP apps between the same network


I try to make a connection between to UWP apps, one is the client on a laptop, the second is the server on a raspberry Pi 3 model B with Windows Iot, and the two devices are on the same network.

I have tried this code on the server app :

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace IOTServer
{
    public sealed partial class MainPage : Page
    {
        Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint ipEnd;
        string hôte, iptest;
        Task<System.Net.IPHostEntry> iphe;
        public MainPage()
        {
            this.InitializeComponent();
            hôte = Dns.GetHostName();
            iphe = Dns.GetHostEntryAsync(hôte);
            iptest = iphe.Result.AddressList[0].ToString();
            ipEnd = new IPEndPoint(IPAddress.Parse("192.168.1.102"), 8888);
            listenerSocket.NoDelay = true;
            listenerSocket.Bind(ipEnd);
            listenerSocket.Listen(0);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Socket clientSocket = listenerSocket.Accept();
            Byte[] Buffer = new byte[clientSocket.SendBufferSize];
            int readByte = clientSocket.Receive(Buffer);
            tbData.Text += readByte.ToString() + Convert.ToChar(13);
        }
    }
}

And this code for the client app :

using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace IOTClient
{
    public sealed partial class MainPage : Page
    {
        Socket client= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint ipEnd;
        string hôte, iptest;
        Task<System.Net.IPHostEntry> iphe;
        public MainPage()
        {
            this.InitializeComponent();
            hôte = Dns.GetHostName();
            iphe = Dns.GetHostEntryAsync(hôte);
            iptest = iphe.Result.AddressList[0].ToString();
            ipEnd = new IPEndPoint(IPAddress.Parse("192.168.1.102"), 8888);
        }

        private void BSend_Click(object sender, RoutedEventArgs e)
        {
            client.Connect(ipEnd);
            client.Send(System.Text.Encoding.UTF8.GetBytes(tbSend.Text));
        }
    }
}

When I click on the bSend button, the client app crash with this exception error :

Error message for the socket exception

I don't understand this, I can ping the raspberry in command line, and execute the server app directly from VS2017 to the raspberry.

Someone can help me please ? Thanks in advance.

Please apologize my english, i'm french guy ;)

Théo


Solution

  • The error message shows that the socket server is reachable, so the client can not connect. Please try with following way:

    1. Check the privateNetworkClientServer capability has been added in Package.appxmanifest. This capability provides inbound and outbound access to home and work networks through the firewall. Please see App capability declarations; enter image description here
    2. Check the firewall on your Raspberry Pi. You need to add rule for the port 8888 to allow tcp communication. Please try to run the command via powershell connected to the device.

      netsh advfirewall firewall add rule name="Port8888" dir=in protocol=TCP localport=8888 action=Allow
      
    3. You need to add Accept method in server code. This method will accept the incoming connection attempt in the queue. Please refer to following samples.

    https://learn.microsoft.com/en-us/dotnet/framework/network-programming/synchronous-server-socket-example

    https://learn.microsoft.com/en-us/windows/uwp/networking/sockets