Cannot receive broadcast response with UWP DatagramSocket

15229 1 Reputation point
2020-07-17T08:50:43.34+00:00

Thank you for your help.

In UWP, using DatagramSocket, we are making an application that performs UDP broadcast transmission and searches for connected devices on the network.
It has been confirmed by Wireshark's frame analysis that both the broadcast transmission and the response from each connected device on the network are transmitted normally.
However, in the MessageReceived event of DatagramSocket, miss the response event from each connected device? The situation is as follows.
Multiple devices (specifically, 3 nodes) are connected to the network and respond to broadcast transmissions all at once.
At this time, in most cases, the MessageReceived event of DatagramSocket cannot receive the response frames from all the connected devices, and in some cases, all of them can be received.

In such a situation, I think that DatagramSocket buffers all response frames and sequentially generates MessageReceive events even if responses are returned from all devices at the same time. , Is that not the case?
For example, is it possible that the number of receive buffers is too small to get rid of because multiple response frames arrive?
It seems that you can also set the amount of receive buffer for DatagramSocket (DatagramSocket.Control.InboundBufferSizeInByte), but this does not seem to be the direct cause because the phenomenon does not change no matter how you set it.

In the above situation, are there any possible causes and remedies?
Thank you.

The following is Datagram Socket establishment processing
        try
        {
            //---------------------------------------------------------------------------
            //  Datagram socket connection establishment (listener setting)
            //---------------------------------------------------------------------------
            //
            // Create DatagramSocket and establish connection with echo server
            var clientDatagramSocket = new DatagramSocket();
            // Specify multicast transmission
            //clientDatagramSocket.Control.MulticastOnly = true;
            // Fragment invalid specification
            //clientDatagramSocket.Control.DontFragment = true;
            //
            //clientDatagramSocket.Control.InboundBufferSizeInBytes = 32 * 1024;
            // Registering a message reception event handler
            clientDatagramSocket.MessageReceived += DiscoveryMessageReceived;

            // Connection establishment (DatagramSocket binding)
            await clientDatagramSocket.BindServiceNameAsync(AppsPage.ClientPortNumber);
The following is MessageReceived Event processing
    //======
    //  Incoming processing of SLMP NodeSearch response message
    //======
    private async void DiscoveryMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    {
        // Connection Info. -  Local IP Address
        HostName lAddr = args.LocalAddress;
        // Connection info. -  Remote IP Address
        HostName rAddr = args.RemoteAddress;
        // Connection info. -  Remote Port
        string rPort = args.RemotePort;


        // Read message
        using (DataReader dataReader = args.GetDataReader())
        {
            // Get unread buffer size
            uint length = dataReader.UnconsumedBufferLength;

            // Byte order acquisition (little endian or big endian)
            //  →It is planned to transmit in BINARY communication mode, so it must be little endian
            ByteOrder order = dataReader.ByteOrder;

            // Read received data
            byte[] aucRecvPacket = new byte[length];
            dataReader.ReadBytes(aucRecvPacket);

            // SLMP Node Search command response frame analysis
            SLMPClientService sLMPClientService = new SLMPClientService();
            UserEnvironmentDefinition status = sLMPClientService.NodeSearchResFrame(ref this.sNodeListData, this.sNodeListNumber, ref aucRecvPacket);

            // Node number increment
            this.sNodeListNumber++;
        }

        // Asynchronous screen update from threads other than UI thread
        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {

        });

        // Release unmanaged resources
        sender.Dispose();
    }
Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.