Time_wait status in socket

Source: Internet
Author: User
TCP must ensure that all data can be shipped whenever possible. When you close a socket, the socket at the active end enters the time_wait state, while the socket at the passive end enters the closed state, which ensures that all data is transmitted. When a socket is closed, it is completed through the four handshakes of sending messages to each other at both ends. When one end calls close (), it indicates that there is no data to be sent at the local end. It seems that after the handshake is complete, the socket should be in the closed state. But there are two problems. First, we do not have any mechanism to ensure that the last ack can be transmitted normally. Second, there may still be residual packets (Wandering duplicates) on the network ), we must also be able to handle it normally.
Through the correct state machine, we know that the closing process of both parties is as follows:


If the last Ack is lost, the server resends the last fin sent by the server. Therefore, the client must maintain a status information to re-Send the Ack; if this status is not maintained, the client will respond to an rst after receiving the fin, and the server will think this is an error after receiving the rst. If the TCP protocol can normally complete the necessary operations and terminate the data stream transmission between the two parties, the four sections of the four handshakes must be completely correctly transmitted without any loss. This is why the socket is still in the time_wait status after it is closed, because it has to wait to re-release ack.
if both parties to the current connection have already called close (), it is assumed that both parties have reached the closed status, but there is no time_wait status, the following situation occurs. Now there is a new connection established, the IP address and port used are exactly the same as the previous one, and the established connection is also called an embodiment of the original connection. It is also assumed that a datagram exists in the network in the original connection, so that the datagram received by the new connection may be the datagram of the previous connection. To prevent this, TCP does not allow a connection from a socket in the time_wait status. In the time_wait status, the socket waits for twice the MSL time (because MSL is one-way transmission of a datagram in the network to the time it is determined to be lost, A datagram may become a residual datagram in the transmission diagram or in the response process. It is necessary to determine that the discarded MSL of a datagram and its response must be twice as large as that of MSL. This means that a successful connection will inevitably cause the loss of the residual datagram in the previous network.
due to problems related to the time_wait status, we can set the so_linger flag to prevent the socket from entering the time_wait status, this can replace the termination mode of the normal TCP four-way handshake by sending the rst. However, this is not a good idea. time_wait is often advantageous for us.

Related Article

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.