3

I have read that the combination of three things causes something like a 200ms delay with TCP: Nagle's algorithm, delayed acknowledgement, and the "write-write-read" combination. However, I cannot reproduce this delay with Java sockets and I am therefore not sure if I have understood correctly.

I am running a test on Windows 7 with Java 7 with two threads using sockets over the loopback address. I have not touched the tcpNoDelay option on any socket (false by default) nor played around with any TCP settings on the OS. The main piece of the code in the client is as below. The server is responding with a byte after each two bytes it receives from the client.

for (int i = 0; i < 100; i++) {
    client.getOutputStream().write(1);
    client.getOutputStream().write(2);
    System.out.println(client.getInputStream().read());
}

I do not see any delay. Why not?

AmyGamy
  • 101
  • 7
  • Look here. http://stackoverflow.com/questions/18588978/nagles-algorithm-on-localhost –  Sep 25 '13 at 14:18
  • Some OSes (such as Windows) avoid some TCP logic on the loopback connection. You'll have to try across a network. – David Schwartz Sep 30 '16 at 20:43

1 Answers1

0

I believe you see delay acknowledgment. You write 4 and 4 bytes to the socket. The server's TCP stack receives a segment (that probably contains at least 4 bytes from an int number) and wakes up the server application thread. This thread writes a byte back to the stream and this byte is sent to the client within ACK segment. I.e. TCP stack gives a chance to an application to send a reply immediately. So you see no delay. You can write a dump of traffic and also make an experiment between two computers to see what really happens.

Vadim
  • 567
  • 6
  • 13