Transport layer: TCP four waves

Four wave introductions

First of all, you can find that after the data transmission is completed, both client A and server B are in the ESTABLISHED state. Both parties in the communication can release the connection or close the connection at the same time. However, a more common situation is when the client The client initiates a shutdown request.
The first wave: In the figure, client A waves for the first time and sends a message with a length of 1 to the server, with the key bit FIN in it. Set to 1, which means termination, requesting to release the connection, and the starting byte sequence number seq is u. Note that the seq here is different from the server-side seq. And A enters FIN-WAIT-1 (termination state 1) from the ESTABLISHED state.
The second wave: After receiving A's request to close the message, server B reacts and confirms that it has received the request to close the message from A. So the ACK confirmation here is 1, the corresponding confirmation number u+1 takes effect, and its own starting byte sequence number seq is v, B enters the CLOSE-WAIT (waiting to close) state from the ESTABLISHED state, and A receives the confirmation from B After the message, it changes from FIN-WAIT-1 (termination status 1) to FIN-WAIT-2 (termination status 2). And at this time, the TCP connection is still in the half-closed state, that is, B can send data to A, and A wants to receive it, but A cannot send data to B.
The third wave: As mentioned above, TCP is still in a semi-closed state after the second wave, so wait for server B to confirm that there is no data before sending. , then wave for the third time. The key field FIN=1 represents the request to release the connection, ACK=1 and ack=u+1 represents the starting byte sent by the client in the first wave received in response. serial number. And the starting byte sequence number of this machine is w, not v+1, because server B may have sent a message to A between the second wave and the third wave, and part of the byte sequence number has been used up, so It is disconnected here. If no data is sent midway, w=v+1, because each message is 1 byte in length. Then B changes from the CLOSE-WAIT (waiting to close) state to the LAST-ACK state. When the client receives B's third wave message, the state changes from FIN-WAIT-2 (termination state 2) to TIME-WAIT (time waiting state).
The fourth wave: When client A receives the third wave message, it immediately sends the fourth wave to server B. The content of the message is actually the same as the second wave, confirming receipt of the message, and giving the corresponding correct confirmation number, the starting byte sequence number of a message of this machine. Then server B received the fourth wave message and entered the closed state. After waiting for 2MSL, client A also entered the closed state.

Insert image description here

Why does the client still have to wait 2MSL before releasing the connection after the fourth wave?

2MSL is twice the maximum lifetime of the message segment, that is, the maximum time that the message segment can survive in the TCP connection.
MSL (Maximum Segment Life)
A confirmation message (the fourth wave) + a request message (the third wave) )

Explanation:
After the client sends the ACK=1 message, it starts to maintain the 2MSL timer. Assume that the client finally sends ACK=1 to confirm receipt of the server closing the connection message. , stuck in the network and not reaching the server. The server has not received the ACK=1 reply from the client for a period of time, and will resend the connection release message for the third wave. After receiving this message, the client resets the 2MSL timer and sends an ACK=1 message to the server again. If the client does not receive the connection release message resent by the server until the 2MSL timer expires, the client releases the TCP connection.

If the client has released the TCP connection at this time and has not waited for 2MSL, it cannot receive the connection release message resent by the server. The resent message cannot find the corresponding connection. If the client sends a TCP connection request to the server again, , at this time, there is no guarantee that the port numbers of the two connections are different, then such a problem may occur: some data of the previous connection is stuck in the network, and these delayed data reach the client after establishing a new connection. Due to the difference between the old and new connections, If the port number and IP are the same, the TCP protocol considers that the delayed data belongs to the new connection, and the new connection will receive dirty data, which will cause data packet confusion.

Why do we need to wave four times, but wouldn’t waving three times work?

Because the TCP connection is full-duplex communication, that is, both the client and the server can send and receive data. When disconnecting, both the server and the client need to confirm that the other party will no longer send data.
Explanation:
The first wave, the client sends to the server, and the server learns that the client will no longer send data
Wave for the second time, the server sends to the client, and the client learns that the server already knows that the client will no longer send data
Wave for the third time, the server sends to the client, and the client learns that the server will No more sending data
The fourth wave, the client sends it to the server, and the server learns that the client already knows that the server will no longer send data
The first and second times When waving, the server may still be sending data to the client, so the second wave and the third wave cannot be merged.

What if the connection is established but the client fails?

Use heartbeat mechanism to keep alive
Explanation: The server maintains a timer. Every time the server receives a request from the client, it will reset the timer. If no client is received after a certain period of time, The server will send a probe message to the client at a certain interval. If a certain number of probe messages are sent continuously without a response, the server will think that the client is faulty and close the connection.

Reference blog: https://blog.csdn.net/qq_37348221/article/details/114572978
Reference book: "Computer Network (Seventh Edition)" Xie Xiren

Guess you like

Origin blog.csdn.net/qq_43847153/article/details/126819580