Skip to main content

TCP 3-Way Handshake

TCP 3 Way Handshake

TCP 3 Way Handshake

The TCP 3-Way Handshake is a process used to establish a reliable connection between a client and a server. This connection-oriented handshake ensures that both parties are ready to communicate before any data is exchanged.

Why Do We Need the 3-Way Handshake?​

  1. Connection Establishment: TCP is a connection-oriented protocol, meaning a connection must be established between the client and server before any data can be sent.

  2. Synchronization: Both the client and server need to synchronize their sequence numbers and initial window sizes to manage data flow and ensure ordered data delivery.

  3. Reliability: The handshake process ensures that both parties are ready to communicate, thus enabling reliable data transfer.

  4. Flow Control: The initial handshake helps establish flow control parameters, preventing either side from overwhelming the other with too much data at once.

  5. Resource Allocation: Establishing a connection allows both the client and server to allocate necessary resources (e.g., memory buffers, processing power) for handling the communication session.

Connection Establishment​

The process is called a 3-Way Handshake because it involves three key steps:

SYN (Synchronize)​

  • The client initiates the connection by sending a TCP segment with the SYN (synchronize) flag set.

  • This segment includes an initial sequence number, which is a randomly chosen value to start the communication.

SYN-ACK (Synchronize-Acknowledgment)​

  • Upon receiving the SYN segment, the server responds with a segment that has both the SYN and ACK (acknowledgment) flags set.

  • The server acknowledges the client's sequence number by setting the acknowledgment number to the client’s sequence number + 1.

  • The server also chooses its own initial random sequence number to send back to the client.

Note

In reality, the acknowledgment number is equal to the client's sequence number plus the size of the data that was sent. In this case, the data size is 1, often referred to as a ghost byte.

ACK (Acknowledgment)​

  • The client responds with a final segment that has the ACK flag set.

  • The acknowledgment number in this segment is set to the server’s sequence number + 1.

  • This final acknowledgment confirms that both parties are ready to start data transfer.

Data Transfer​

  • Once the handshake is complete, the client can begin sending data with the SYN flag set and using the sequence number that was acknowledged by the server.

  • The server responds with an acknowledgment (ACK), confirming receipt of the data. The acknowledgment number is set to the client's sequence number plus the size of the data received.

    Note

    The acknowledgment number in a TCP segment indicates the next byte the sender of the acknowledgment expects to receive from the other side. It acknowledges receipt of all bytes up to (but not including) this number.

  • This process of sending data and receiving acknowledgments continues, ensuring reliable and ordered data transfer.

Connection Termination​

The termination of a TCP connection involves a similar handshake process, known as the 4-Way Handshake:

FIN (Finish)​

  • The client initiates the connection termination by sending a TCP segment with the FIN (finish) flag set.

  • This indicates that the client has no more data to send.

FIN-ACK (Finish-Acknowledgment)​

  • The server acknowledges receipt of the FIN segment by sending an ACK with the acknowledgment number set to the client’s sequence number + 1.

  • The server also sends a FIN segment with its sequence number, indicating it also wants to close the connection.

ACK (Acknowledgment)​

  • The client acknowledges the server’s FIN segment by sending a final ACK with the acknowledgment number set to the server’s sequence number + 1.

  • At this point, the connection is closed, and both the client and server can release the resources associated with the connection.