NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

TcpServer Receive Bottleneck

Open aqoamann opened this issue 2 years ago • 1 comments

I wrote a simple TCP server/client application, where in rare situations the TcpClient sends short messages (SendAsync) to TcpServer without any delay (in a loop). The problem is: the client needs the replys from TcpServer so it can continue and go further... but at the TcpServer side, the replys to client are not pumped (approximately) immediately and are "collected". These collected replays are then sent to client all at once, as you can see in the Wireshark-Screenshots (two links at the end).

  1. Am I doing something wrong? What am I doing wrong?
  2. How can I guarantee that replys from TcpServer are not collected and sent all-at-once?

This is the code in client:

    for (int i = 1; i <= 10000; i++)
    {
        if (i % 40 == 0)
        {
            var dt = DateTime.Now;
            var msg = $"CurrentTime;{dt:yyyy-MM-dd HH:mm:ss.fff}";
            c.SendAsync(msg);
        }
        else
        {
            c.Send("Command_1");
        }
    }

These are the wireshark screenshots: https://ibb.co/khkJ7pv https://ibb.co/sbcpqsL

Kindly regards.

aqoamann avatar Mar 04 '22 12:03 aqoamann

It sounds like you're running into Nagle's Algorithm, which can be disabled by setting OptionNoDelay to true:

https://github.com/chronoxor/NetCoreServer/blob/master/source/NetCoreServer/TcpServer.cs#L110-L116

lukebakken avatar May 26 '22 23:05 lukebakken