本文介绍了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 桌面应用程序,用于在端口 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.

通过本地主机端口 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.

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

    来源: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 数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 08:58