title: Is TCP not reliable?
body: <p>I am believing TCP is reliable. If <code>write(socket, buf, buf_len)</code> and <code>close(socket)</code>  returns without error, the receiver will receive the exact same data of <code>buf</code> with length <code>buf_len</code>. </p>
  
  <p>But <a href="https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable" rel="nofollow noreferrer">this article</a> says TCP is not reliable.</p>
  
  <blockquote>
    <p>A:</p>
  
  <pre><code>  sock = socket(AF_INET, SOCK_STREAM, 0);  
    connect(sock, &amp;remote, sizeof(remote));
    write(sock, buffer, 1000000);             // returns 1000000
    close(sock);
  </code></pre>
    
    <p>B:</p>
  
  <pre><code>int sock = socket(AF_INET, SOCK_STREAM, 0);
  bind(sock, &amp;local, sizeof(local));
  listen(sock, 128);
  int client=accept(sock, &amp;local, locallen);
  write(client, "220 Welcome\r\n", 13);
  
  int bytesRead=0, res;
  for(;;) {
      res = read(client, buffer, 4096);
      if(res &lt; 0)  {
          perror("read");
          exit(1);
      }
      if(!res)
          break;
      bytesRead += res;
  }
  printf("%d\n", bytesRead);
  </code></pre>
    
    <p>Quiz question - what will program B print on completion?</p>
  
  <pre><code>A) 1000000 
  B) something less than 1000000 
  C) it will exit reporting an error 
  D) could be any of the above
  </code></pre>
    
    <p>The right answer, sadly, is ‘D’. But how could this happen? Program A
    reported that all data had been sent correctly!</p>
  </blockquote>
  
  <p>If this article is true, I have to change my mind. But I am not sure if this article is true or not.</p>
  
  <p>Is this article true? </p>