Safe Close TCP Connection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.