本文介绍了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的本地主机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);
}



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



Here is what I've tried, and what I know from my troubleshooting:


  1. 从 UWP发送UDP数据报到.NET桌面正常

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

UWP代码通过本地主机端口11111将消息发送到.NET桌面:

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(两台不同的计算机)上工作

    我已经通过将侦听器和发话者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限制的影响,无法解决此问题。从我阅读的内容来看似乎没什么关系(),应该已经可以在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.

    来源:

    最好的解决方法是使用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数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-29 03:50