c# - Windows 10 中 UDP 的问题。UWP

标签 c# udp win-universal-app datagram

我遇到了以下问题: 我正在使用 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/

相关文章:

c# - 具有复合索引的 Nhibernate Map 映射

c# - MobileServiceClient.LoginAsync() 引发方法 'LoginAsync' 没有重载需要 1 个参数错误

c# - 汉堡菜单按钮的动态列表

xaml - 如何在 XAML Winrt 中使用没有空格的 TextBlock 中的运行?

c# - 如何在一页中使用两个表单?

c# - 如何在运行时在 ASP.NET MVC 中创建强类型 View ?

python - UDP 服务是否必须从连接的 IP 地址响应?

c++ - 如何找到通过 UDP 发送数据所花费的时间

C 非阻塞 UDP 套接字

c# - 使用C#更新SQL中的数据时发生 fatal error