Rapid TCP Socket .Send() Hangs/Freezes Application

Hi, I know this issue has been brought up in the past but I wanted to bring it back to the spotlight…

I’m using a TCP server socket to send log data to a TCP client on the local network. These log messages can have a very fast output, sometimes as high as a thousand strings per second.

When the log messages are slower, everything works fine. But when the logging frequency speeds up, the entire system freezes on the .Send() function from the System.Net.Socket class.

I have performed the same log tests using UART with no problems.
I have performed the same log tests writing to an SD card with no problems.

I am including my socket setup below, but I have tried tweaking a variety of settings with no success.
I have tested using other servers and verified the TCP client I’m testing with can handle the data rate.
I have also tried the same situation with the socket configured as a TCP client–same issue.
I’m using an SC20260D SOM.

I cannot for the life of me figure out why this is happening. Please help!

//Open a new server socket
Socket _serverHostSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverHostSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
_serverHostSocket.SendTimeout = 2000;
_serverHostSocket.ReceiveTimeout = 2000;
_serverHostSocket.Bind(new IPEndPoint(IPAddress.Any, 8000));
_serverHostSocket.Listen(int.MaxValue);

//Wait for a client connection - blocking
Socket _clientConnectionSocket = _serverHostSocket.Accept();

//Once connected, send data (called in a separate, public function)
_serverConnectionSocket.Send("[*Warning - ControllerID: This is an example log message.\r");

Sending too many TCP messages hangs entire system · Issue #1343 · ghi-electronics/TinyCLR-Libraries · GitHub
Try the solution in my last post.

1 Like

Thanks Dat, I had found that post earlier and tried to implement the solution but it hadn’t worked. However, that post was for a client connection and this was for a server connection. In the above example I had incorrectly applied the “SocketOptionName.NoDelay” line to the host socket. After moving it down and applying it to the client connection socket, it worked! I ran some more tests and haven’t seen any issues yet.

For others, this is how my working setup ended up looking:

//Open a new server socket
Socket _serverHostSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverHostSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_serverHostSocket.SendTimeout = 2000;
_serverHostSocket.ReceiveTimeout = 2000;
_serverHostSocket.Bind(new IPEndPoint(IPAddress.Any, 8000));
_serverHostSocket.Listen(int.MaxValue);

//Wait for a client connection - blocking
Socket _clientConnectionSocket = _serverHostSocket.Accept();
_clientConnectionSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

//Once connected, send data (called in a separate, public function)
_serverConnectionSocket.Send("[*Warning - ControllerID: This is an example log message.\r");

1 Like

I would think setting Nagle(no delay) to false would be more effective in a packet burst situation. It would result in many fewer packets being sent.

1 Like

I’ll run some more tests and see if there’s any difference :+1: