The three-way TCP handshake

The three-way TCP handshake is the initial process used by a client and a server to establish a reliable connection before data transmission can begin. This handshake is a fundamental part of the TCP (Transmission Control Protocol) communication protocol, which ensures reliable and orderly data transfer between devices over a network.

Here’s how the three-way TCP handshake works:

Step 1: SYN (Synchronize):
The process begins when the client (usually an application or device) initiates a connection to the server. The client sends a TCP packet with the SYN (synchronize) flag set to the server. This packet contains a randomly chosen sequence number (a starting point for numbering the data bytes) and a window size (the amount of data the sender is willing to accept at a time).

This packet’s purpose is to request the server’s acknowledgment and also to inform the server about the client’s initial sequence number. The client is now in the “SYN_SENT” state.

Step 2: SYN-ACK (Synchronize-Acknowledge):
When the server receives the SYN packet from the client, it acknowledges the request by sending a TCP packet with both the SYN and ACK (acknowledge) flags set. The acknowledgment flag (ACK) indicates that the server received the client’s SYN packet successfully. Like the client, the server also generates a random sequence number for itself and includes it in the packet. The server also acknowledges the client’s sequence number by incrementing it by one.

At this point, the server is in the “SYN_RECEIVED” state.

Step 3: ACK (Acknowledge):
In response to the server’s SYN-ACK packet, the client sends an acknowledgment (ACK) back to the server. This final ACK packet is sent with the ACK flag set and acknowledges the receipt of the server’s SYN-ACK packet. The client also increments the server’s sequence number by one.

After receiving this ACK, the server transitions to the “ESTABLISHED” state, and the client, upon receiving the ACK for its SYN, also moves to the “ESTABLISHED” state. At this point, the connection is established, and both client and server can now start transmitting data reliably over the established connection.

It’s essential to have this three-way handshake process because it allows both client and server to agree on initial sequence numbers, window sizes, and ensures that both parties are ready to communicate. If any of the parties do not receive the appropriate responses during the handshake, they will assume there is a problem and attempt to re-establish the connection by initiating another three-way handshake.