Https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable
Http://stackoverflow.com/questions/8874021/close-socket-directly-after-send-unsafe
Send-side code
Sock = socket (af_inet, sock_stream, 0); Connect (sock, &remote, sizeof (remote)); Write (sock, buffer, 1000000); Returns 1000000 shutdown (sock, SHUT_WR);
The last step is to add a timeout to prevent the receiving side from maliciously sending the data for (;;) { Res=read (sock, buffer, 4000); if (Res < 0) { perror ("reading"); Exit (1); } if (!res) break ; } Close (sock);
The reason for doing this is
1. Send succeeded just to put the data in the kernel's send buffer
2. When close is called, if there is data in the TCP receive buffer, it causes the RST (not FIN) to be sent, and the send buffer is emptied.
It is best to return an ACK to the sending side at the application level. TCP is not absolutely reliable.
Safe Close TCP Connection