UWP 应用未从本地主机上的 .NET 桌面应用接收 UDP 数据报 [英] UWP app doesn't receive UDP Datagram from a .NET desktop app on localhost

查看:25
本文介绍了UWP 应用未从本地主机上的 .NET 桌面应用接收 UDP 数据报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在作为客户端的 UWP 应用和作为服务器的 .NET 桌面应用之间设置客户端服务器.我使用 UDP 数据报作为两者之间的消息传递系统.

I've been trying to setup a client server between a UWP app as the client and a .NET desktop app as the server. I'm using UDP Datagrams as the messaging system between the two.

这是我的 UWP 代码,用于在端口 22222 的 localhost IP 上侦听数据报:

Here my UWP code to listen for Datagrams on the localhost IP at port 22222:

private async void listenToServer()
{
    // Setup UDP Listener
    socketListener = new DatagramSocket();
    socketListener.MessageReceived += MessageReceived;

    await socketListener.BindEndpointAsync(new HostName("127.0.0.1"),"22222");
    Debug.WriteLine("Listening: " + socketListener.Information.LocalAddress + " " + socketListener.Information.LocalPort);
 }

private async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
    // Interpret the incoming datagram's entire contents as a string.
    uint stringLength = args.GetDataReader().UnconsumedBufferLength;
    string receivedMessage = args.GetDataReader().ReadString(stringLength);
    Debug.WriteLine("Received " + receivedMessage);
}

这是我的 WinForm .NET 桌面应用程序,用于在 localhost 的 2222 端口上发送数据报:

Here is my WinForm .NET desktop app to send Datagrams on localhost on port 2222:

public async void sendToClient()
{
    // Setup UDP Talker
    talker = new UdpClient();
    sending_end_point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 22222);
    talker.Connect(sending_end_point);

    byte[] send_buffer = Encoding.ASCII.GetBytes("Hello!");
    await talker.SendAsync(send_buffer, send_buffer.Length);
}

这是我尝试过的,以及我从故障排除中了解到的:

  1. 将 UDP 数据报 UWP 发送到 .NET 桌面可行.

  1. Sending a UDP datagram from UWP to .NET desktop works.

通过 localhost 端口 11111 向 .NET 桌面发送消息的 UWP 代码:

UWP code to send message to .NET desktop over localhost port 11111:

public async void sendToServer()
{
    // Connect to the server
    socketTalker = new DatagramSocket();
    await socketTalker.ConnectAsync(new HostName("127.0.0.1"), "11111");
    Debug.WriteLine("Connected: " + socketTalker.Information.RemoteAddress + " " + socketTalker.Information.RemotePort);

    // Setup Writer
    writer = new DataWriter(socketTalker.OutputStream);
    writer.WriteString("Ping!");

    await writer.StoreAsync();

    writer.DetachStream();
    writer.Dispose();
}

.NET 桌面代码通过相同的 IP 和端口侦听来自 UWP 的消息:

.NET desktop code to listen for message from UWP over same IP and port:

private async Task listenToClient()
{
    // Setup listener
    listener = new UdpClient(11111);
    UdpReceiveResult receiveResult = await listener.ReceiveAsync();
    Debug.WriteLine(" Received: " + Encoding.ASCII.GetString(receiveResult.Buffer));

}

  • 将 UDP 数据报从 .NET 桌面发送到 UWP工作 跨不同 IP(2 台不同的计算机)

  • Sending UDP Datagram from .NET desktop to UWP works across different IPs (2 different computers)

    我已经通过将监听器和通话器 IP 地址设置为服务器运行的相同 IP 地址对此进行了测试,并且它可以完美运行.这导致了研究,使我获得了#3...

    I've tested this by setting the listener and talker IP addresses to the same IP address where the server was running, and it works flawlessly. This lead to research which got me to #3...

    环回豁免没有影响

    运行 CheckNetIsolation.exe 和环回豁免工具以使 UWP 应用免受环回 IP 限制并不能解决此问题.从我读到的(问题在 Windows 10 中使用 UDP.UWP),在 Visual Studio 环境中运行应该已经免于环回,但我还是尝试了,但运气不好.

    Running CheckNetIsolation.exe and the Loopback exemption tool to exempt the UWP app from loopback IP restriction didn't fix this issue. It seems it shouldn't matter, from what I read (Problems with UDP in windows 10. UWP), running in the Visual Studio environment should already be exempt from loopback, but I tried anyways, and not luck.

    推荐答案

    尽管这很糟糕,但它被 Microsoft 设计阻止了.

    As much as this sucks, it is blocked by Microsoft by design.

    Loopback 仅允许用于开发目的.使用在 Visual Studio 之外安装的 Windows 运行时应用程序不是允许.此外,Windows 运行时应用只能使用 IP 环回作为客户端网络请求的目标地址.所以一个窗口使用 DatagramSocket 或 StreamSocketListener 的运行时应用程序侦听 IP 环回地址被阻止接收任何传入数据包.

    Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets.

    来源:https://msdn.microsoft.com/en-us/library/windows/apps/hh780593.aspx

    您可以做的最佳解决方法是使用 TCP 套接字并从 UWP 应用连接到您的桌面应用(而不是相反).

    The best workaround you can do is to use TCP sockets and connect from the UWP app to your desktop app (not the other way around).

    这篇关于UWP 应用未从本地主机上的 .NET 桌面应用接收 UDP 数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆