我遇到了以下问题: 我正在使用 UDP 在本地网络中与 2 台不同的机器进行通信。
一方面,我有一台安装了 4.5 框架的 Windows 7 机器。我正在使用 System.Net 类 使用此代码:
public static void UDPWriter()
{
Task.Run(async () =>
{
byte[] data = new byte[10000];
IPEndPoint ipep = new IPEndPoint(IPAddress.Pars("192.168.0.16"), 5002);
Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpClient.Connect(ipep);
while (true)
{
await Task.Delay(24);
string input = packagetosend;
data = Encoding.ASCII.GetBytes(input);
var receivedResults = udpClient.Send(data, SocketFlags.None);
}
});
}
另一方面,我使用以下代码使用 Windows 10 通用应用程序:
async static private void EnablerListener()
{
//Click
HostName hostname = new HostName("192.168.0.16");
listener = new DatagramSocket();
listener.Control.InboundBufferSizeInBytes=10000;
listener.MessageReceived += socket_MessageReceived;
await listener.BindServiceNameAsync("5002");
}
static void socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
// Message received. Place your logic here
}
只要我发送一个“小”包(我的理论是小于 MTU),我就会正确接收发送的内容。
问题是我的 udp 包碎片化了。当我发送 1 个分成 4 个的包时(我在 Wireshark 中看到过),Windows 10 软件没有收到任何东西。我试过更改 listener.Control.Donotfragment(也许我用错了)但它似乎不起作用。 更新1: 在 wireshark 中,我收到此消息 time-to-live exceeded (fragment reassembly time exceeded) Wireshark 中只有部分包,其他包成功重组(几乎全部)
最佳答案
在 UWP 上使用 Loopback 的本地计算机 IPC 受到限制。我最近自己遇到了这个问题。也许考虑一种不同的方法,如应用程序到应用程序通信 - https://channel9.msdn.com/Events/Build/2015/3-765
来自 DatagramSocket 示例:
Note Network communications using an IP loopback address cannot normally be used for interprocess communication between a Universal Windows Platform (UWP) app and a different process (a different UWP app or a desktop app) because this is restricted by network isolation. Network communication using an IP loopback address is allowed within the same process for communication purposes in a UWP app. For more information, see How to set network capabilities.
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket (向底部)
关于c# - Windows 10 中 UDP 的问题。UWP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832880/