Knowledge about closing TCP connections

Source: Internet
Author: User
Tags connection reset

Http://blog.csdn.net/wjtxt/article/details/6598925

From the perspective of the TCP protocol, there are two methods to close a established TCP connection. One is to close the connection normally, that is, to close the connection four times, and the other is to close the connection abnormally, we usually call it a reset ).

First, let's talk about the status change of the four waves when the connection is closed normally. The status change of the active party that closes the connection is fin_wait_1-> fin_wait_2-> time_wait, the passive party that closes the connection changes to close_wait-> last_ack-> time_wait. During the four waves, the ACK package is automatically completed by the protocol stack, while the fin package must be actively sent by the application layer through closesocket or shutdown. After the connection is normally closed, recv returns 0 and send returns 10058.

In addition, in our daily applications, there are also many cases of abnormal disconnection. For example, if the application is forcibly closed, the local network is suddenly interrupted (the NIC is disabled, the network cable is unplugged), or the program is improperly handled, the connection is reset, And the RST package is generated during Connection reset, at the same time, unreceived (sent) data in the network buffer zone will be lost. After the connection is reset, the local send or Recv will get the error code 10053 (10038 for closesocket), the other party's Recv will get the error code 10054, And the send will get the error code 10053 (10054 for closesocket ).

The operating system provides two functions for us to close a TCP connection: closesocket and shutdown. Usually, closesocket sends a fin package to the other party, but there are exceptions. For example, a worker thread is calling Recv to receive data. When closesocket is called externally, the connection is reset and an rst package is sent to the other party. This rst package is automatically generated by the local party.

Shutdown can be used to close connections in the specified direction. This function receives two parameters: socket and closed. The available values are sd_send, sd_receive, and sd_both. When the direction is sd_send, a FIN packet is sent to the other party no matter what status the socket is in (Recv blocking or idle). Note the difference between this and closesocket. At this time, the local party enters the fin_wait_2 state, and the other party enters the close_wait state. This party can still call Recv to receive data. When the direction is sd_receive, the status of the dual-sending connection remains unchanged and it is still in the established, the local Party can still send data. However, if the other party calls the send method again, the connection will be reset immediately and an rst package will be sent to the other party. This rst package is generated passively, pay attention to the difference from closesocket.

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.