TCP's Three Way Handshake

TCP's Three Way Handshake

·

3 min read

Understanding the 3-Way Handshake in TCP

TCP is the backbone of reliable communication over the Internet. It ensures a secure and dependable connection between devices using a process known as the 3-way handshake. This handshake establishes a connection before any data is transmitted and synchronizes both devices for seamless communication.

What is the 3-Way Handshake?

A 3-way handshake is the process by which two devices - the client and the serve could establish a reliable connection. This ensures that both are actually ready to send and receive data as their sequence numbers are synchronized for proper tracking of data.

The actual process has three essential steps:

  • SYN (Synchronization)

  • SYN-ACK (Synchronization and Acknowledgment)

  • ACK (Acknowledgment)

    Steps in Connection Establishment Here is the process in simple terms:

  1. SYN: The Client Opens the Connection

The client sends a message with the SYN flag set to 1 to the server, indicating that it requires establishing a connection.

This message contains a randomly generated sequence number, for example, 500, along with some other details such as MSS(Maximum segment size) and Window size.

Window size
The window size refers to the amount of data (measured in bytes) that can be sent by the sender without receiving an acknowledgment from the receiver.
Maximum Segment Size
MSS (Maximum Segment Size) defines the largest amount of application data (payload) that a device can send in a single TCP segment, excluding the TCP and IP headers.

This step is to intimate the server that the client is ready to start communications.

  1. SYN-ACK: Response from the Server

After getting the SYN message from the client, the server gets back to the client with an acknowledgement message where its SYN and ACK flags are set as 1.

The Server acknowledges the client's sequence no by making the acknowledgement no (ACK no) equal to client sequence no +1, i.e., 501.

In addition, it sends its sequence no. i.e. 700 and advertises its MSS and window size.

This step ensures the server has been acknowledged and accepts the client's request.

  1. ACK: The Client Confirms

After the client gets the SYN-ACK from the server, the client sends an acknowledgment number with the ACK flag at 1.

The client's ACK number will be the server's sequence number + 1 (e.g., 701).

At this point, both the devices can exchange data.

Process to Terminate a Connection

Closing a connection in TCP is also an orderly process that ensures a clean shutdown:

  1. FIN: The Client Requests to Close

The client sends a FIN (finish) message to the server, requesting to close the connection.

  1. FIN-ACK: The Server Acknowledges

The server responds with both FIN and ACK messages to confirm the request and to indicate readiness to terminate the connection.

  1. ACK: The Client Confirms

The client sends a final ACK to the server, which completes the termination process and closes the connection.

Key Takeaways

  • Sequence Numbers: Each packet has a unique sequence number to track data segments.

  • Header and Data: TCP packets consist of a header (containing control information) and data (the actual payload).

  • Reliability: The 3-way handshake ensures data is sent and received reliably, preventing issues like lost packets or synchronization errors.

Why is the 3-Way Handshake Important?

The 3-way handshake is the foundation of reliable communication in TCP, as it:

  • Ensures that both devices are ready to communicate.

  • Synchronizes sequence numbers for proper tracking and reassembly of data.

  • Establishes a secure and reliable connection before the transfer of data.

Conclusion

In essence, the 3-way handshake is a formal introduction between two devices, creating a smooth and efficient exchange of information. Through this structured process, TCP ensures there is robust and reliable communication.