Say goodbye gracefully - TCP protocol waves four times

Say goodbye gracefully - TCP protocol waves four times

Say hello politely - The article TCP Three-Way Handshake has already introduced how the TCP protocol establishes a connection. After the connection is established and the data transmission is completed, how can we close the connection gracefully?

say goodbye gracefully

Leaving without saying goodbye is always easy to catch people off guard. What will you do when we have to end a pleasant chat and leave a city that is hard to let go of?

Of course, you have to learn to say goodbye gracefully.

Say hello politely - TCP protocol three-way handshake " has already introduced how the TCP protocol establishes a connection. After the connection is established and the data transfer is complete, how can we gracefully close the connection?

Never forget - TCP’s four waves

first wave

When the client is about to close the connection, it will send a FIN=1 packet to the server and enter the FIN_WAIT_1 state.

second wave

After the server receives the FIN=1 data packet from the client, it will respond with an ACK data packet to the client and enter the state of preparing to close the connection. At this time, the server will start preparing to stop data transmission.

After the client receives the ACK packet responded by the server, it enters the FIN_WAIT_2 state. At this time, there may still be data transmission, and you need to wait until the server actually stops data transmission before it can enter the shutdown state.

third wave

After the server finishes processing the data transmission, it will send a FIN packet to the client and enter the LASK_ACK state, indicating that the server has entered the connection closed state.

fourth wave

After the client receives the FIN packet, it can confirm that the data transmission has completely stopped, enter the TIME_WAIT state, and respond to the server with an ACK packet. After waiting for 2MSL (Maximum Segment Lifetime, maximum message survival time), the connection is truly closed and enters the CLOSE state.

After receiving the ACK packet, the server disconnects and enters the CLOSE state.

Retry and Fault Tolerance

When the FIN data packet is sent out, the data packet that has not received an ACK response for a long time will trigger a timeout retransmission.

Why does the client not close the connection immediately after receiving the FIN instruction, but wait for 2MSL before closing it?

Assuming that the client does not have the state of TIME_WAIT, but closes the connection inside, if the client immediately re-establishes the connection, after the connection is successfully established, it receives the last data packet for closing the connection and responds to the server with an ACK data packet. This will cause data confusion on the server side.

Summarize

When the TCP protocol closes the connection, since data transmission may be in progress, both the client and the server will first enter the process of waiting to close the connection.

When the client or server sends a FIN packet and does not receive an ACK response packet within a certain period of time, it will retry.

After the client finally receives the FIN packet from the server, it will first enter the TIME_WAIT state and wait for 2MSL (maximum message lifetime). In order to prevent the occurrence of message transmission confusion caused by network delay, message transmission timeout and other problems.