A problem about socket programming in ubuntu

Hi, I’m trying to make a socket receive sensor data in real-time on ubuntu 20.04. But something weird happens. When I use the play button, the scene works ordinarily, but in the builded executable, the socket sporadically “waits” for the “receive”. It should receive the next message immediately because the sensor only uses the local network, and it does in the UnityEditor. My code looks something like this (just a simple socket program using a thread):

_clientReceiveThread = new Thread (new ThreadStart(ListenSensor));
_clientReceiveThread.IsBackground = true; 			
_clientReceiveThread.Start();

....
....

private voide ListenSensor()
{
    _receivingUdpClient = new UdpClient(PortNumber);
    IPEndPoint ipe = new IPEndPoint(IPAddress.Any, PortNumber);                
    byte[] receivedByteData = new byte[1024];
    
    while(_isPlaying)
    {
        receivedByteData = _receivingUdpClient.Receive(ref ipe);
    }
}

The thing that can be a hint is that it works fine when it is build on windows.
Does anybody knows how to solve this??

I’ve solved this problem by myself. I’ll leave the answer for others to refer.


The socket programming itself was not the problem. The real problem was about the Garbage Collector.

In th first place, after receiving the data I have made a “new” Array or List to save it, because it always have different size of data. But because this happens after every “receive” (which happens quiet frequently), the Garbage Collector stacks and it consumes time for the removing process. It seems that the socket receive function is easily influenced by the GC. The reason why this happens only on the builded execute file is because (I heard) that UnityEditor has the ability to manage the garbage collector itself, but the execute file excludes those functionality when build.

The stuttering receive stoped when I Initalized the data buffer size only once and fixed the size of the data buffer to the maximum size. If something similar happens, I suggest to check whether your code allocates new arrays over and over.