title: socket.setTcpNoDelay(true) is not working
body: <p>I have a problem in sending data packet using tcp socket. What my program does is pretty simple: Firstly, a client socket connects to a server socket. Then the server socket sends 5 "hello" back to the client and the client outputs the 5 "hello" to the console. Both the client and server run on my local computer and below is the full code:</p>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>public class Server {
  
  private static ServerSocket serverSocket;
  
  public static void main(String[] args) throws IOException, InterruptedException {
      serverSocket = new ServerSocket(60009);
      serverSocket.setReuseAddress(true);
      System.out.println("Server started!");
      final Socket socket = serverSocket.accept();
      socket.setTcpNoDelay(true);
      Thread.sleep(1000);
      socket.getOutputStream().write("hello".getBytes());
      socket.getOutputStream().write("hello".getBytes());
      socket.getOutputStream().write("hello".getBytes());
      socket.getOutputStream().write("hello".getBytes());
      socket.getOutputStream().write("hello".getBytes());
      InputStream is = socket.getInputStream();
      is.read(new byte[100]);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>public class Client {
  static Socket socket;
  static byte[] buffer;
  
  public static void main(String[] args) throws IOException {
      socket = new Socket();
      socket.setTcpNoDelay(true);
      socket.connect(new InetSocketAddress("localhost", 60009));
      buffer = new byte[100];
  
      new Thread(new Runnable() {
          @Override
          public void run() {
              while (true) {
                  InputStream is = null;
                  try {
                      is = socket.getInputStream();
                      is.read(buffer);
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
                      String rcvStr = new String(buffer);
                      System.out.println(rcvStr);
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Given the code posted above, my expected output should be like this:</p>
  
  <p>hello</p>
  
  <p>hello</p>
  
  <p>hello</p>
  
  <p>hello</p>
  
  <p>hello</p>
  
  <p>However, the actual result I get is this:</p>
  
  <p>hello</p>
  
  <p>hellohellohellohello</p>
  
  <p>My problem is right here. The result output does not follow the order I send them on server. I ask the server to send 5 "hello" one by one. But according to the result, it seems like what the server really does is it sends one "hello" at first and then sends the rest of four "hello" all together.</p>
  
  <p>After searching the google, I found many people relate this problem to the so called Nagle algorithm which will avoid tcp socket sending short data packet immediately for increasing the efficiency of network use. To disable the Nagle algorithm, you only need to call setTcpNoDelay(true) on socket. Then the computer will send whatever you ask it to send right away. But unfortunately, after I disabled the Nagle algorithm, my client still outputs the same result as above. Can you guys tell me what am I missing or doing wrong? Any help is much appreciated!</p>