UWP 应用程序不从本地主机上的 .NET 桌面应用程序接收 UDP 数据报
UWP app doesn't receive UDP Datagram from a .NET desktop app on localhost
我一直在尝试在作为客户端的 UWP 应用程序和作为服务器的 .NET 桌面应用程序之间设置客户端服务器。我正在使用 UDP 数据报作为两者之间的消息传递系统。
这是我的 UWP 代码,用于在端口 22222 的本地主机 IP 上侦听数据报:
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 上的本地主机上发送数据报:
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);
}
这是我尝试过的方法,以及我从故障排除中了解到的内容:
发送 UDP 数据报 从 UWP 到 .NET 桌面 有效。
通过本地主机端口 11111 向 .NET 桌面发送消息的 UWP 代码:
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 的消息:
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 台不同的计算机)
我已经通过将监听器和通话器的 IP 地址设置为与服务器 运行 相同的 IP 地址来对此进行了测试,它可以完美地工作。这导致我进行了研究 #3...
环回豁免没有影响
运行 CheckNetIsolation.exe 和用于使 UWP 应用免受环回 IP 限制的环回豁免工具未解决此问题。从我读到的(Problems with UDP in windows 10. UWP)来看,这似乎无关紧要,Visual Studio 环境中的 运行 应该已经免于环回,但我还是尝试了,而不是运气。
虽然这很糟糕,但它已被 Microsoft 设计阻止。
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 应用程序连接到您的桌面应用程序(而不是相反)。
我一直在尝试在作为客户端的 UWP 应用程序和作为服务器的 .NET 桌面应用程序之间设置客户端服务器。我正在使用 UDP 数据报作为两者之间的消息传递系统。
这是我的 UWP 代码,用于在端口 22222 的本地主机 IP 上侦听数据报:
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 上的本地主机上发送数据报:
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);
}
这是我尝试过的方法,以及我从故障排除中了解到的内容:
发送 UDP 数据报 从 UWP 到 .NET 桌面 有效。
通过本地主机端口 11111 向 .NET 桌面发送消息的 UWP 代码:
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 的消息:
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 台不同的计算机)
我已经通过将监听器和通话器的 IP 地址设置为与服务器 运行 相同的 IP 地址来对此进行了测试,它可以完美地工作。这导致我进行了研究 #3...
环回豁免没有影响
运行 CheckNetIsolation.exe 和用于使 UWP 应用免受环回 IP 限制的环回豁免工具未解决此问题。从我读到的(Problems with UDP in windows 10. UWP)来看,这似乎无关紧要,Visual Studio 环境中的 运行 应该已经免于环回,但我还是尝试了,而不是运气。
虽然这很糟糕,但它已被 Microsoft 设计阻止。
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 应用程序连接到您的桌面应用程序(而不是相反)。