Introduction

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer protocol.
It not only ensures data integrity, but also designs a complete set of connection management mechanisms:
Three-way handshake (establishing a connection)
Four-way handshake (disconnecting a connection).
Among them, the four-way handshake is a key step when the TCP connection is disconnected, ensuring that both parties can correctly release the source,
avoiding data loss or abnormal connection termination.

Four-way handshake process

TCP is full-duplex communication, which means that data can be transmitted in both directions at the same time. Therefore, the disconnection of the connection requires not only the client to notify the server that it will no longer send data, but also the server to notify the client that it will no longer send data. This leads to a four-way handshake process, rather than a simple three-way handshake.

The first handshake

The client sends FIN (termination request): The client sends a FIN (Finish) message, indicating that it will no longer send data and enter the FIN-WAIT-1 state. At this time, the client can still receive data, but will no longer actively send data.

Second wave

The server replies ACK (confirmation): After receiving FIN, the server sends an ACK (Acknowledgment) message to confirm that it has received the client's termination request and enters the CLOSE-WAIT state. At this time, the server can still continue to send data.

Third wave

The server sends FIN (termination request): After completing data transmission, the server sends a FIN message to indicate that it will no longer send data and enter the LAST-ACK state.

Fourth wave

The client replies ACK (confirmation): After receiving the server's FIN, the client sends an ACK message to confirm that it has received the server's termination request and enters the TIME-WAIT state. At this time, the client will wait for a while (usually 2MSL, which is twice the maximum message lifetime) to ensure that the server will completely close the connection after receiving ACK.

The role of TIME-WAIT:

The existence of the TIME-WAIT state is to ensure that the last ACK can be correctly received by the server to prevent old TCP packets from affecting new connections. Usually, the TIME-WAIT state will last for 2MSL (maximum message lifetime) to ensure that all possible delayed data packets are cleared.
TIME-WAIT can also prevent invalid connection request segments from affecting new connections. If the client releases the connection immediately after sending the last ACK, the server's FIN message may be lost, which in turn affects the establishment of new connections.
The following figure shows the complete process

Why is the handshake three times, but the handshake four times?

Simply speaking: based on TCP stable communication, when the connection is disconnected, not only should the data transmission be terminated, but also the data of the other party should be ensured to be completely received. At the same time, TCP is full-duplex communication, so each channel needs to be closed separately, which leads to the request reply mechanism to be repeated twice, that is, 4 times is the minimum step of this confirmation mechanism

Summary

In general, the three-way handshake is to establish a reliable connection, and the four handshakes are to ensure the integrity of data transmission. However, in actual applications such as high-concurrency server environments:
TIME-WAIT state may cause a large number of ports to be occupied, affecting server performance. To solve this problem, many systems will use TCP connection reuse or shorten the TIME-WAIT time to optimize the connection release process.
Some systems also provide the SO_REUSEADDR option in the socket option, allowing new connections to reuse old ports in the TIME-WAIT state, thereby improving the server's concurrency.