TCP (Three-Way Handshake)
The TCP three-way handshake refers to a three-step interaction process between a client and a server when establishing a reliable Transmission Control Protocol (TCP) connection. The main purpose of this process is to ensure that the connection is reliable, both parties’ sending and receiving capabilities are normal, and data transmission can begin. Below is a detailed explanation of each step:
- First Handshake: Client Sends SYN
- Process: The client (A) sends a synchronization segment (SYN, Synchronize Sequence Number) to the server (B), indicating that it wants to establish a connection with the server.
- Purpose: To notify the server that the client intends to establish a connection, and at the same time, inform the server of an initial sequence number (Seq) for ordering data packets in subsequent data transmission.
- Message Structure: In this message, the SYN flag is set to 1, and Seq is set to a randomly generated initial sequence number, denoted as Seq = x. Note that at this point, the client enters the SYN-SENT state, waiting for the server’s response.
- Second Handshake: Server Responds with SYN-ACK
- Process: After receiving the client’s SYN message, the server (B) knows that the client is requesting to establish a connection. It will send an acknowledgment message (SYN + ACK) to the client to confirm that it has received the client’s SYN request.
- Purpose:
- Confirm that both parties can communicate through SYN, and the server also generates its own initial sequence number to prepare for establishing the connection.
- ACK is used to confirm that the client’s initial sequence number has been received and inform the client that the server can communicate normally.
- Message Structure:
The SYN flag is also set to 1 because the server also needs to synchronize its own initial sequence number to the client.
The ACK flag is set to 1, indicating that the server has acknowledged the client’s SYN message.
The server’s message will contain Seq = y (the server’s own initial sequence number) and ACK = x + 1 (the client’s sequence number plus 1, indicating that the client’s SYN has been received).
- State: The server enters the SYN-RECEIVED state, waiting for the client’s final confirmation.
- Third Handshake: Client Confirms with ACK
- Process: After receiving the server’s SYN + ACK message, the client confirms that the server’s response is valid, and then the client will send a pure ACK (acknowledgment) message to the server, indicating that the handshake is successful.
- Purpose: The client confirms the server’s sequence number through ACK and notifies the server that the connection establishment is complete, so both parties can perform data transmission.
- Message Structure:
The ACK flag is set to 1, indicating that the server’s SYN has been received.
The sequence number in the message is Seq = x + 1 (the client’s own sequence number), and ACK = y + 1 (acknowledging the server’s sequence number plus 1).
- State:
After sending this ACK message, the client enters the ESTABLISHED state, the connection is formally established, and data transmission can proceed.
After receiving the ACK message, the server also enters the ESTABLISHED state, and the connection establishment is complete.
Functions of the Three-Way Handshake
- Confirm the receiving and sending capabilities of both parties: Each message transmission in the three-way handshake ensures that both parties can receive and send data.
- Prevent interference from old connection requests: The three-way handshake can prevent old duplicate connection requests from re-establishing connections, thereby ensuring the uniqueness and correctness of the connection.
Why Three-Way Handshake Instead of Two?
A two-way handshake cannot guarantee the receiving capabilities of both parties. For example, suppose there is only a two-way handshake:
The client sends a SYN message, the server responds with SYN + ACK, and the client thinks the connection is established. However, the server may not receive the client’s ACK due to network reasons, making the server unable to determine whether the client is ready to receive data.
The third step in the three-way handshake (client confirming ACK) ensures that the server can receive the client’s confirmation message, making both parties clear about the connection status and avoiding potential problems.
Summary
- First Handshake: The client sends SYN, indicating the desire to establish a connection and sending its own sequence number.
- Second Handshake: The server sends SYN + ACK, confirming receipt of the client’s request, and sending its own sequence number and the acknowledgment number for the client.
- Third Handshake: The client sends ACK, confirming receipt of the server’s sequence number and making the final confirmation for the establishment of the connection.
In this way, through the three-way handshake, the TCP connection can be reliably established, and data transmission can proceed.