如何在同一网络之间连接到 UWP 应用程序

How to connect to UWP apps between the same network

我尝试在 UWP 应用程序之间建立连接,一个是笔记本电脑上的客户端,第二个是带有 Windows Iot 的 raspberry Pi 3 模型 B 上的服务器,两个设备在同一网络上。

我已经在服务器应用程序上尝试过此代码:

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);
        }
    }
}

客户端应用程序的代码:

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));
        }
    }
}

当我单击 bSend 按钮时,客户端应用程序崩溃并出现此异常错误:

Error message for the socket exception

这个我不懂,可以在命令行ping树莓派,直接从VS2017到树莓派执行server app

有人可以帮助我吗? 提前致谢。

请原谅我的英语,我是法国人 ;)

泰奥

错误信息显示socket服务器可达,所以客户端连接不上。请尝试以下方式:

  1. 检查 privateNetworkClientServer 功能已添加到 Package.appxmanifest。此功能提供通过防火墙对家庭和工作网络的入站和出站访问。请看App capability declarations
  2. 检查 Raspberry Pi 上的防火墙。您需要为端口 8888 添加规则以允许 tcp 通信。请尝试通过连接到设备的 powershell 运行 命令。

    netsh advfirewall firewall add rule name="Port8888" dir=in protocol=TCP localport=8888 action=Allow
    
  3. 您需要在服务器代码中添加Accept方法。此方法将接受队列中的传入连接尝试。请参考以下示例。

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

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