Problems with UDP in windows 10. UWP

2019-02-20 13:23发布

问题:

I am having the following problem: I am communicating 2 different machines in local network with UDP.

In one side I have a Windows 7 machine with 4.5 framework installed. I am using the class System.Net with this code:

 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);

            }

        });

    }

In the other side I am working with a Windows 10 Universal App with this code:

   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

        }

As soon as I send a "small" package ( my theory is that less than the MTU) I receive correctly what is sent.

The problem comes with I my udp package is fragmented. When I send 1 packages that is splitted in 4 ( I have seen it in Wireshark) the Windows 10 software do not receive anything. I have tried changing listener.Control.Donotfragment( maybe I am using it wrong) but it seems not working. UPDATE1: In wireshark I receive this message time-to-live exceeded (fragment reassembly time exceeded) Only some packages in Wireshark, others are succesfully reassembled ( almost all)

回答1:

Local machine IPC using Loopback on UWP is restricted. I recently ran in to this problem myself. Perhaps consider a different approach like App-to-App Communication - https://channel9.msdn.com/Events/Build/2015/3-765

From the DatagramSocket sample:

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 (towards the bottom)