TCP Handshakes and the 3-Way Handshake


The Transmission Control Protocol (TCP) is one of the foundational protocols in the suite of Internet protocols. TCP ensures reliable, ordered, and error-checked delivery of data between applications running on devices in a network. A cornerstone of TCP's reliability lies in its handshake mechanism. This article delves into the TCP handshake process, with a focus on the 3-way handshake, which establishes a robust connection between a client and a server.
What is a TCP Handshake?
A TCP handshake is a process that establishes and maintains a connection between two devices (typically a client and a server) in a network. It is essential for ensuring that both parties are ready to communicate and agree on the parameters of the connection. The handshake also facilitates synchronization of sequence numbers, which are used to track data packets during communication.
The most common handshake mechanism in TCP is the 3-way handshake, which ensures the connection is ready before data transmission begins.
The TCP 3-Way Handshake
The 3-way handshake is a three-step process involving the exchange of packets between the client and server. Here’s how it works in detail:
SYN (Synchronization)
The client initiates the connection by sending a TCP packet with the SYN (synchronize) flag set to 1. This packet contains an initial sequence number (ISN), which is a randomly chosen value that helps track the data packets sent by the client.
Purpose:
To indicate the client’s desire to establish a connection and to synchronize sequence numbers with the server.
Client state after sending SYN:
SYN_SENT
SYN-ACK (Synchronization-Acknowledgment)
Upon receiving the SYN packet, the server responds with a TCP packet that has both the SYN and ACK (acknowledgment) flags set to 1. The server’s packet contains:
A SYN flag indicating the server’s agreement to establish the connection.
An acknowledgment number, which is the client’s ISN incremented by 1 (ISN+1).
The server’s own initial sequence number (Server ISN).
Purpose:
To acknowledge the client’s request and share the server’s own sequence number.
Server state after sending SYN-ACK:
SYN_RECEIVED
ACK (Acknowledgment)
The client completes the handshake by sending a TCP packet with the ACK flag set to 1. This packet:
Acknowledges the server’s ISN by setting the acknowledgment number to the Server ISN incremented by 1.
Confirms the connection is established and ready for data transfer.
Purpose:
To acknowledge receipt of the server’s SYN-ACK and finalize the connection setup.
Client state after sending ACK:
ESTABLISHED
Server state after receiving ACK:
ESTABLISHED
At this point, the connection is fully established, and data can flow between the client and the server.
Diagram of the 3-Way Handshake
Diagram of the 3-Way Handshake
Synchronization: Both the client and server share and acknowledge each other’s sequence numbers, ensuring data packets are tracked properly.
Reliability: The process ensures that both parties are ready to communicate and that no packets are lost during the connection setup.
Error Detection: By using sequence and acknowledgment numbers, TCP can detect missing or out-of-order packets.
Why is the 3-Way Handshake Necessary?
Establishes a Reliable Channel: Without the handshake, there would be no guarantee that both parties are ready to communicate or that they agree on the communication parameters.
Prevents Connection Hijacking: The handshake ensures that each party explicitly acknowledges the other’s request to connect.
Synchronization of Sequence Numbers: By exchanging initial sequence numbers, the client and server can accurately track the flow of data packets.
The TCP 3-way handshake isn’t just a dry technical process—it’s like the courteous bow before a dance of data! It ensures both devices are on the same page, ready to exchange information reliably and efficiently. From synchronizing sequence numbers to confirming readiness, this handshake is the unsung hero of our digital lives.
So, next time you enjoy a seamless browsing experience, remember this article and give a nod to the trusty 3-way handshake that made it all possible. Who knew a simple handshake could be the backbone of modern communication?
Subscribe to my newsletter
Read articles from Sumit Ghugare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
