What does the Socket method setTcpNoDelay() do and when should I use it?

John Mitchell

This controls the TCP_NODELAY socket option. TCP_NODELAY disables/enables the use of Nagle's Algorithm to control the amount of buffering used when transferring data. Nagle's algorithm tries to send full data segments by waiting, if necessary, for enough writes to come through to fill up the segment.

It's useful to disable the use of Nagle's algorithm (setTcpNoDelay(true)) when your communication over that socket comprises small packets and where latency is important. The classic example that comes to mind is a talk/chat application.

Nagle's algorithm is described in more detail in RFC 896.