UDP messenger question

Hello to everyone!!
I recently downloaded an asset UDP messenger and it looks quite good!I sent data to receive and so on!!!
But at the end of my project, I noticed something odd…(which is not after reading a lot on google)…
If the sending data device to my app stops sending then my app remains frozen with the last received values…
Of course, my next move is to add if received=“” do that and this but nothing.
After quit reading and debug log in certain places I noticed that the app stucks in the bytes received section and won’t budge until new data received…sad very sad… I need a way just simple text to notify the user that you are not receiving…is it possible???
Thank you so much!!

public void ReceiveData ()
    {
     
        while (true)
        {
         
            try
            {

                // Bytes received
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
             

                byte[] data = client.Receive(ref anyIP);


             
                // Bytes into text
                string text = "";
                text = Encoding.UTF8.GetString(data);
                received = text;
            }

            catch (Exception err)
            {
                Debug.Log("Error:" + err.ToString());



            }
        }
    }

A while(true) loop is infinite. If you want to break out of a loop, then you need to set up other conditions.

With UDP, you can pretty much do anything you want to do. It is that flexible. To show a message if no packet has been received in a while, create a coroutine and have the code in the coroutine check for new packets and update other variables accordingly. Or just check for any packets once per frame in the Update function and then process the current packets in the buffer. But don’t do an infinite loop, because that is how you get stuck.

1 Like

There is actually 3 cases when its not infinite.

  • You break out of it using break keyword or return method.
  • You use C# built in statemachine support either through Tasks or
  • Enumerators.
3 Likes