Cross-Platform UDP Communication (send & receive)

Hi,

for a new project i need to communicate over UDP. I tried for days but the result is always the same: receiving messages on UWP does not work.
With sockets-for-pcl and using the System.Net namespaces in platform-specific classes using dependency injection
the result remains the same. Android can send and receive messages, UWP can only send. I tried in an emulator, in a VM and on real devices.
To debug the messages i used PacketSender App .
I read about the network isolation on uwp, therefor I bind to the real IP, not a loopback or broadcast address. I have also enabled client and server networking in Package.appxmanifest.
Currently I am out of ideas and luck.
Could someone provide some code samples or give some input? This is a blocker for using xamarin for the whole App.

Regards,
VisualMelody

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    What you have done on the UWP side? Can you post some code to specify your issue?

  • VisualMelodyVisualMelody Member ✭✭

    Here are the tests I did:

    using Sockets.Plugin (Sharpcaster.SocketsForPCL 2.0.4, also tried rda.SocketsForPCL 2.0.2);

    var listenPort = 11011;
    var receiver = new UdpSocketReceiver();
    
    receiver.MessageReceived += (sender, args) =>
    {
        // get the remote endpoint details and convert the received data into a string
        var from = String.Format("{0}:{1}", args.RemoteAddress, args.RemotePort);
        var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);
    
        System.Diagnostics.Debug.WriteLine("{0} - {1}", from, data);
    };
    
    // listen for udp traffic on listenPort
    await receiver.StartListeningAsync(listenPort);
    

    Port seems to be open:

            Netstat -an
             UDP    [::]:11011             *:*
             UDP    0.0.0.0:11011          *:*
    

    using Dependency Service

        using Windows.Networking.Sockets;
        using Windows.Storage.Streams;
    
        var serverDatagramSocket = new DatagramSocket();
        serverDatagramSocket.MessageReceived += ServerDatagramSocket_MessageReceived;
        await serverDatagramSocket.BindServiceNameAsync("11011");
        {
            string request;
            using (DataReader dataReader = args.GetDataReader())
            {
                request = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim();
            }
    
            System.Diagnostics.Debug.WriteLine("RECEIVED: "+request + " from "+ args.RemoteAddress);
            // Echo the request back as the response.
    
            sender.Dispose();
    
        }
    

    Port also seems to be open:

        Netstat -an
        UDP    [::]:11011             *:*
        UDP    0.0.0.0:11011          *:*
    

    Result is always the same: on UWP the MessageReceived is never called, no output. Works on Android.
    App has the following capabilities:

        <Capabilities>
            <Capability Name="internetClientServer" />
            <Capability Name="privateNetworkClientServer" />
          </Capabilities>
    
  • any idea someone? This is a real showstoper for the whole project.

Sign In or Register to comment.