Knowledge about closing TCP connections

Source: Internet
Author: User
Tags connection reset

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, four waves to close the connection; another type is abnormal shutdown, which is usually called a Connection RESET (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.