TCP Three-Way Handshake: Ensuring Reliable Connection

By SB •  Updated: 09/12/24 •  7 min read

The TCP three-way handshake is the process that initiates a connection between a client and a server in TCP communication.

This handshake ensures that both parties are synchronized and ready for reliable data exchange.

Here’s a detailed look at each step, its purpose, and the states involved in the process.

(Further down is a simplified version of the process that might be easier to remember.)


The Steps in the TCP Three-Way Handshake

  1. SYN (Synchronize) – Client Sends Connection Request
    • Purpose: The client initiates the connection by sending a SYN (synchronize) packet to the server. This packet requests to open a connection and synchronizes the sequence numbers (used to track data transmission).
    • State Change (Client):
      • The client enters the SYN-SENT state after sending the SYN packet.
    • State Change (Server):
      • The server, upon receiving the SYN packet, moves from the LISTEN state to the SYN-RECEIVED state. At this point, it acknowledges the client’s request by preparing to accept the connection.
  2. SYN-ACK (Synchronize-Acknowledge) – Server Acknowledges the Request
    • Purpose: After receiving the SYN from the client, the server responds with a SYN-ACK packet. This packet serves two purposes:
      1. ACK (Acknowledge): Acknowledges the receipt of the client’s SYN request.
      2. SYN (Synchronize): Sends its own synchronization request, allowing the server to synchronize its sequence numbers with the client.
    • State Change (Client):
      • The client, upon receiving the SYN-ACK, remains in the SYN-SENT state until it sends its final acknowledgment.
    • State Change (Server):
      • The server remains in the SYN-RECEIVED state until it gets the client’s acknowledgment (the final ACK).
  3. ACK (Acknowledge) – Client Confirms Connection
    • Purpose: After receiving the SYN-ACK from the server, the client sends an ACK packet back to the server, acknowledging the server’s SYN. This confirms that both parties are synchronized and ready to exchange data.
    • State Change (Client):
      • The client moves from the SYN-SENT state to the ESTABLISHED state, indicating that the connection is successfully established and ready for data transmission.
    • State Change (Server):
      • Upon receiving the final ACK, the server transitions from the SYN-RECEIVED state to the ESTABLISHED state.

TCP States Involved in the Three-Way Handshake

  1. LISTEN (Server Side):
    • The server is waiting for incoming connection requests. It listens for SYN packets from clients. This is the initial state on the server side.
  2. SYN-SENT (Client Side):
    • After the client sends the initial SYN packet, it enters the SYN-SENT state. The client is now waiting for the SYN-ACK response from the server to proceed.
  3. SYN-RECEIVED (Server Side):
    • Once the server receives the client’s SYN request, it sends a SYN-ACK and enters the SYN-RECEIVED state. Here, the server is waiting for the client’s final ACK to fully establish the connection.
  4. ESTABLISHED (Both Client and Server):
    • After the final ACK is exchanged, both the client and server enter the ESTABLISHED state. In this state, the connection is active, and data can be transmitted between the two parties.

Why Each Step Is Important

  1. SYN:
    • Establishes initial contact between the client and server and synchronizes the initial sequence numbers, which helps in tracking data segments.
  2. SYN-ACK:
    • Confirms that the server received the client’s request and that it’s ready to synchronize its own sequence numbers. This step ensures both sides are prepared for data transmission.
  3. ACK:
    • Finalizes the handshake by confirming that the client received the server’s acknowledgment and is ready to proceed with data exchange. This last step ensures that the connection is fully established and reliable.

Summary: Purpose of the TCP Three-Way Handshake

The TCP three-way handshake ensures that both the sender and receiver are synchronized and ready to exchange data, with mechanisms in place to ensure data integrity and reliability. Each step—SYN, SYN-ACK, and ACK—plays a crucial role in guaranteeing that both parties are in agreement before starting communication.

Another Way to Think About the TCP Three-Way Handshake: The Friendly Knock on the Door

Imagine this process as if you’re knocking on your friend’s door to see if they’re home and ready to chat. Here’s the step-by-step breakdown:


1. Knock-Knock! (SYN – Client Says Hi)


2. Who’s There? (SYN-ACK – Server Says Hi Back)


3. Let’s Talk! (ACK – Client Confirms the Chat)


Visualizing the TCP Three-Way Handshake

Client:

  1. Sends a SYN (knock-knock!) → SYN-SENT state
  2. Receives SYN-ACK (who’s there?)
  3. Sends ACK (let’s talk!) → ESTABLISHED state

Server:

  1. Gets SYN (knock-knock!) → LISTEN state
  2. Sends SYN-ACK (who’s there?) → SYN-RECEIVED state
  3. Receives ACK (let’s talk!) → ESTABLISHED state

Why the Handshake Matters:


Memory Tip: “SYN, SYN-ACK, ACK = Knock, Who’s There, Let’s Talk!”

This simple sequence ensures both devices are perfectly in sync and ready to talk. Just like you wouldn’t start talking until your friend opens the door and says they’re ready, the TCP handshake makes sure both sides are good to go before they begin.


Why It’s Super Reliable

Unlike other protocols, the TCP handshake ensures both parties are fully ready. The sequence numbers are like keeping track of whose turn it is to talk, and if anyone misses something, they can easily ask to hear it again.


Quick Summary:


By thinking of the handshake as a simple conversation where both sides must confirm they’re ready, you can easily recall the purpose and steps of this process.

SB

I've been practicing OSINT and utilizing Linux as my daily operating system for over twenty years. The tools are always changing and so I'm always learning, but helping you understand the value of protecting your own data remains at the forefront of everything I do.

Keep Reading