locked
[UWP] Datagramsocket automatic stop receive message. RRS feed

  • Question

  • First Datagramsocket can't receive any message from UDP

    my Code like this

     udpSocket.MessageReceived += UdpSocket_MessageReceived;

                Windows.Networking.HostName hostName = null;
                IReadOnlyList<Windows.Networking.HostName> networkinfo = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
                foreach (Windows.Networking.HostName h in networkinfo)
                {
                    if (h.IPInformation != null)
                    {
                        Windows.Networking.Connectivity.IPInformation ipinfo = h.IPInformation;
                        if (h.RawName == IPAddr)
                        {
                            hostName = h;
                            break;
                        }
                    }
                }
                if (hostName != null)
                    await udpSocket.BindEndpointAsync(hostName, Port);
                else
                    await udpSocket.BindServiceNameAsync(Port);

    after 2 days debug

    i add some code like this

               

       outstm = await udpSocket.GetOutputStreamAsync(new Windows.Networking.HostName("255.255.255.255"), "49002");
        Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(outstm);
         dw.WriteString("Start");
        await dw.StoreAsync();

    then UdpSocket_MessageReceived Method start working

    but about when i received about 90 messages 

    output windows display

    thread exit and not any messages receive...

    if i keep sending messages it may received about 1000 messages and then stop receive.


    • Edited by Krunal Parekh Thursday, January 28, 2016 5:42 AM subject
    Wednesday, January 27, 2016 9:58 PM

Answers

  • Hello Refire,

    >>thread exit and not any messages receive...

    >>if i keep sending messages it may received about 1000 messages and then stop receive.

    There could be number of reasons for this and I can not just find root cause regarding this by just looking at the code above but There is official sample DatagramSocket here which you could look at.

    This sample shows you how to a use datagram (UDP) socket to send and receive data using the DatagramSocket and related classes in the Windows.Networking.Sockets namespace in your Universal Windows Platform (UWP) app.

    The client component of the sample creates a UDP socket, uses the socket to send and receive data, and closes the socket. The server component of the sample creates a UDP socket to listen for incoming network packets, receives incoming UDP packets from the client, sends data to the client, and closes the socket.

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, February 2, 2016 9:02 AM
    • Marked as answer by Barry Wang Monday, February 15, 2016 9:25 AM
    Thursday, January 28, 2016 5:37 AM

All replies

  • Hello Refire,

    >>thread exit and not any messages receive...

    >>if i keep sending messages it may received about 1000 messages and then stop receive.

    There could be number of reasons for this and I can not just find root cause regarding this by just looking at the code above but There is official sample DatagramSocket here which you could look at.

    This sample shows you how to a use datagram (UDP) socket to send and receive data using the DatagramSocket and related classes in the Windows.Networking.Sockets namespace in your Universal Windows Platform (UWP) app.

    The client component of the sample creates a UDP socket, uses the socket to send and receive data, and closes the socket. The server component of the sample creates a UDP socket to listen for incoming network packets, receives incoming UDP packets from the client, sends data to the client, and closes the socket.

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, February 2, 2016 9:02 AM
    • Marked as answer by Barry Wang Monday, February 15, 2016 9:25 AM
    Thursday, January 28, 2016 5:37 AM
  • Finally, I change my code to received 3 Message per second. It works, but other UDP packets just drop and lost. 

    I think the there is some different between DataramSocket and UDPClient.

    By the way thanks for anyway.


    • Edited by Refire Tuesday, February 16, 2016 2:16 AM
    Tuesday, February 16, 2016 2:15 AM