TCP Three-Way Handshake Process

TCP Three-Way Handshake Process

TCP three-way handshake is the process of establishing a TCP connection, used to synchronize sequence numbers, exchange window sizes, and other information between the client and server, ensuring both parties can send and receive data properly. This process occurs when the client attempts to establish a connection with the server.

Step 1: Client sends a SYN segment

  • The client selects an initial sequence number (ISN), say x, and sets the SYN flag to 1, indicating this is a connection request segment.
  • The client sends the SYN segment to the server and enters the SYN_SENT state.
  • At this point, the client tells the server: "I want to establish a connection, my sequence number starts from x."

Step 2: Server replies with a SYN-ACK segment

  • Upon receiving the SYN segment, if the server agrees to establish the connection, it replies with a SYN-ACK segment.
  • The server selects its own initial sequence number (ISN), say y, and sets the SYN flag to 1; meanwhile, it sets the ACK flag to 1 and the acknowledgment number to x+1 (indicating it expects the next data byte sequence number from the client to be x+1).
  • The server enters the SYN_RCVD state.
  • At this point, the server tells the client: "I agree to connect, my sequence number starts from y, and I have acknowledged your sequence number x."

Step 3: Client sends an ACK segment

  • Upon receiving the SYN-ACK segment, the client sends an ACK segment in response.
  • The client sets the ACK flag to 1, the acknowledgment number to y+1 (indicating it expects the next data byte sequence number from the server to be y+1); and sets the sequence number to x+1 (because the SYN segment in Step 1 consumed one sequence number).
  • The client enters the ESTABLISHED state, indicating the connection is established.
  • After the server receives the ACK, it also enters the ESTABLISHED state.
  • At this point, the client tells the server: "I have acknowledged your sequence number y, the connection is established."

Why is a three-way handshake necessary?

  • The main purpose is to prevent old duplicate connection requests from causing connection errors (e.g., delayed SYN segments being mistaken for new requests) and to ensure both parties can confirm each other's sending and receiving capabilities. A two-way handshake is insufficient because the server cannot know whether the client received its SYN-ACK response; the three-way handshake, through the client's final acknowledgment, guarantees the reliability of the connection.

The entire process ensures the foundation for bidirectional communication in a TCP connection. After sequence numbers are synchronized, both parties can begin data transmission.