TCP Four-Way Handshake Process
TCP Four-Way Handshake Process
TCP four-way handshake is the process used for terminating a TCP connection, ensuring both parties can safely close the connection. It involves the exchange of four specific segments between the client and the server.
Detailed Process:
-
First Handshake (FIN from Client)
- Description: When the client has finished sending data and decides to close the connection, it sends a TCP segment to the server. In the header of this segment, the FIN (Finish) flag is set to 1. It also includes a sequence number (Seq), let's assume it's
u, which is the sequence number of the last data byte previously sent by the client plus 1. - Client State Change: After sending the FIN segment, the client's state changes from
ESTABLISHEDtoFIN-WAIT-1. This means the client will no longer send data and begins waiting for the server's acknowledgment.
- Description: When the client has finished sending data and decides to close the connection, it sends a TCP segment to the server. In the header of this segment, the FIN (Finish) flag is set to 1. It also includes a sequence number (Seq), let's assume it's
-
Second Handshake (ACK from Server)
- Description: Upon receiving the client's FIN segment, the server must send an acknowledgment. It sends an acknowledgment (ACK) segment to the client. In the header of this segment, the ACK flag is set to 1, and the acknowledgment number (Ack) field is set to
u + 1(i.e., the client's sequence number u plus 1). This informs the client: "I have received your FIN segment with sequence number u." - Server State Change: After sending this ACK segment, the server enters the
CLOSE-WAITstate. At this point, the connection is in a "half-close" state: the client-to-server direction is closed (the client will not send more data, and the server will not receive more data), but the server-to-client direction remains open, as the server may still have data to send to the client. - Client State Change: After receiving the server's ACK segment, the client knows the server has agreed to close its side of the connection. The client's state changes from
FIN-WAIT-1toFIN-WAIT-2. At this stage, the client is still waiting for the server to send its own FIN segment.
- Description: Upon receiving the client's FIN segment, the server must send an acknowledgment. It sends an acknowledgment (ACK) segment to the client. In the header of this segment, the ACK flag is set to 1, and the acknowledgment number (Ack) field is set to
-
Third Handshake (FIN from Server)
- Description: When the server has also finished sending all its data, it prepares to close the connection. At this time, the server sends a FIN segment to the client. In the header of this segment, the FIN flag is set to 1. Let's assume the sequence number of this FIN segment sent by the server is
w. - Note: In practical implementations, this second FIN segment is often combined with the ACK segment from the second handshake to improve efficiency. That is, the server might directly send a single segment with both the
ACKandFINflags set, effectively reducing the handshake to three steps. However, in theory, these are two separate steps. - Server State Change: After sending the FIN segment, the server's state changes from
CLOSE-WAITtoLAST-ACK. This means the server has sent its close request and is now waiting for the final acknowledgment from the client.
- Description: When the server has also finished sending all its data, it prepares to close the connection. At this time, the server sends a FIN segment to the client. In the header of this segment, the FIN flag is set to 1. Let's assume the sequence number of this FIN segment sent by the server is
-
Fourth Handshake (ACK from Client)
- Description: Upon receiving the server's FIN segment, the client must send an acknowledgment segment. In the header of this segment, the ACK flag is set to 1, and the acknowledgment number (Ack) field is set to
w + 1(i.e., the server's sequence number w plus 1). - Client State Change: After sending this ACK segment, the client does not immediately close the connection but enters the
TIME-WAITstate. In this state, the client waits for a duration, typically set to 2MSL (Maximum Segment Lifetime, usually 2 minutes in theory, but often set to 30 seconds or 1 minute in practice).- The main reasons for waiting 2MSL are:
- To ensure the client's final ACK reaches the server: If this ACK is lost in the network, the server will retransmit the FIN segment after a timeout. The client, still in the
TIME-WAITstate, can receive this retransmitted FIN and send another ACK, ensuring the closing process completes reliably. - To allow all packets from this connection to expire in the network: Waiting for 2MSL ensures that all packets generated by this connection have expired in the network, preventing delayed packets from the old connection from interfering with a new connection.
- To ensure the client's final ACK reaches the server: If this ACK is lost in the network, the server will retransmit the FIN segment after a timeout. The client, still in the
- The main reasons for waiting 2MSL are:
- Server State Change: Once the server receives the client's final ACK segment, it confirms that the connection is fully closed, immediately enters the
CLOSEDstate, and releases all associated resources. - Final Closure: After the
TIME-WAITstate duration (2MSL) expires, the client also enters theCLOSEDstate, completely releasing its resources. At this point, the entire TCP connection is fully closed.
- Description: Upon receiving the server's FIN segment, the client must send an acknowledgment segment. In the header of this segment, the ACK flag is set to 1, and the acknowledgment number (Ack) field is set to
Summary: The four-way handshake can be understood as a polite farewell:
- Client: "I'm done talking, I'm going to hang up, okay?" (FIN)
- Server: "Okay, I know you're going to hang up." (ACK) ... (Server finishes its own tasks) ... "I'm also done talking, so shall I hang up?" (FIN)
- Client: "Okay, go ahead and hang up." (ACK)
- After both parties confirm, they truly hang up the call.