Network Programming in Linux: time_wait status
The last time_wait status was not very understandable when I started to look at the TCP socket's 4-way handshake termination flowchart. looking back, I found that the time_wait status is very delicate. there are two reasons for designing the time_wait status:
- The TCP full-duplex connection can be reliably terminated.
- After the connection is terminated, the residual data sent to the connection by the network is discarded and not received by the new connection.
Before explaining these two reasons, we need to understand the concept of MSL (maxinum segment lifetime.
Each TCP must have an MSL value. this value is generally 2 minutes, but it is not fixed. Different systems are different. whether an error occurs or the connection is disconnected, the maximum time a data packet can stay on the network is MSL. that is to say, MSL is the data packet lifecycle time. at this time, the packet will be discarded and not sent. the time_wait status lasts twice as long as MSL, that is, 2msl time.
- TCP full-duplex connections can be reliably terminated
Reliable termination of TCP requires four handshakes. as shown in: first, the client actively closes, causing fin to be sent to the server. After the server receives the fin, it returns an ACK to the client. Then, the server closes the connection with the client, send a fin to the client. After receiving the fin, the client sends an ACK to the server. the client enters the time_wait status. if the server does not receive an ACK, the server resends a fin message to the client, and the client resends the ACK. The server then waits for the client to send an ACK. this ensures the reliable termination of both parties. both parties know that the other party has terminated. in this time_wait time, You can resend the ACK. If the client does not receive the fin message, TCP will send an rst message to the server, which will be interpreted as error by the server.
- After the connection is terminated, the residual data sent to the connection from the network is discarded and not received by the new connection.
For example:
A TCP connection is established between 10.12.24.48 port: 21 and 206.8.16.32 port: 23 (which server and client do not need to be concerned. then Link A is closed. then, a new TCP Connection B is established between 10.12.24.48 port: 21 and 206.8.16.32 port: 23 (which end is the server and which end is the client. it is likely that connection a and Connection B have different applications.ProgramCreated. then, after we close a, the data m at both ends of the connection may be transmitted over the network. at this time, a is closed and B connection is established again, because the addresses and ports of A and B are the same. in this way, M data is finally sent to both ends of B connection. this creates confusion. B receives the data of the original data. connections in the time_wait status will disable the creation of new connections (such as a and B. unless the time_wait status ends, that is, after the 2msl time. one MSL is used to discard the data that is being sent to the link on the network, and the other MSL discards the response information. in this way, after 2msl, the data obtained after the re-creation will never be sent to the connected data.
Copyright statement:
ReprintedArticlePlease indicate the original source http://blog.csdn.net/feiyinzilgd/archive/2010/09/19/5894446.aspx
Contact tan Haiyan or go to tan Haiyan's personal homepage to leave a message.