TCP Three-Way Handshake Explained

Why TCP Needs a Handshake Before Sending Data

TCP is a connection-oriented protocol - before any actual data is exchanged, both sides need to agree that a connection exists and synchronize their starting sequence numbers. This agreement happens through a three-step exchange.

The Three Steps

Client                          Server
  |------- SYN ----------------->|   "I want to connect, my sequence starts at X"
  |<------ SYN-ACK ---------------|   "Okay, and my sequence starts at Y"
  |------- ACK ------------------>|   "Confirmed, let's begin"

  (connection now established - data can flow)

What Each Flag Actually Means

StepFlag(s)Purpose
1SYNClient requests a connection and proposes an initial sequence number
2SYN-ACKServer acknowledges the client's request and proposes its own sequence number
3ACKClient confirms the server's sequence number - connection is now open

Why Both Sides Need Their Own Sequence Number

Sequence numbers let TCP track exactly which bytes have been sent and received, in what order, and detect any missing or duplicate data. Because data can flow in both directions simultaneously, each side needs its own independent sequence number - this is why the handshake exchanges two, not one.

Ending a Connection: A Similar, Longer Exchange

Closing a TCP connection uses a comparable exchange (often called a "four-way handshake," using FIN and ACK flags) since either side may still have data left to send even after deciding to close - the same careful, agreed-upon coordination that opens a connection also applies when ending one.

Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.