Is TCP not reliable?

Viewed 1071

I am believing TCP is reliable. If write(socket, buf, buf_len) and close(socket) returns without error, the receiver will receive the exact same data of buf with length buf_len.

But this article says TCP is not reliable.

A:

  sock = socket(AF_INET, SOCK_STREAM, 0);  
  connect(sock, &remote, sizeof(remote));
  write(sock, buffer, 1000000);             // returns 1000000
  close(sock);

B:

int sock = socket(AF_INET, SOCK_STREAM, 0);
bind(sock, &local, sizeof(local));
listen(sock, 128);
int client=accept(sock, &local, locallen);
write(client, "220 Welcome\r\n", 13);

int bytesRead=0, res;
for(;;) {
    res = read(client, buffer, 4096);
    if(res < 0)  {
        perror("read");
        exit(1);
    }
    if(!res)
        break;
    bytesRead += res;
}
printf("%d\n", bytesRead);

Quiz question - what will program B print on completion?

A) 1000000 
B) something less than 1000000 
C) it will exit reporting an error 
D) could be any of the above

The right answer, sadly, is ā€˜D’. But how could this happen? Program A reported that all data had been sent correctly!

If this article is true, I have to change my mind. But I am not sure if this article is true or not.

Is this article true?

4 Answers
Related