【问题标题】:How to connect to UWP apps between the same network如何在同一网络之间连接到 UWP 应用
【发布时间】:2019-12-15 14:28:20
【问题描述】:

我尝试在 UWP 应用程序之间建立连接,一个是笔记本电脑上的客户端,第二个是带有 Windows Iot 的树莓派 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执行服务端应用到树莓派。

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

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

泰奥

【问题讨论】:

    标签: c# sockets uwp iot windowsiot


    【解决方案1】:

    报错信息显示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

    【讨论】:

    • 抱歉让您久等了。我尝试了这个和另一个解决方案,我会随时通知你。
    • 没关系。请随时让我有任何问题。
    猜你喜欢
    • 2016-07-28
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    相关资源
    最近更新 更多