🔗 Understanding the TCP 3-Way Handshake:

The TCP 3-Way Handshake is a fundamental process that establishes a reliable connection between two devices over a TCP/IP network. It involves three steps: SYN (Synchronize), SYN-ACK (Synchronize-Acknowledge), and ACK (Acknowledge). During the handshake, the client and server exchange initial sequence numbers and confirm the connection establishment.


What Is TCP?

TCP (Transmission Control Protocol) is a foundational internet protocol designed to:
✅ Deliver data reliably (no lost or duplicated packets)

✅ Ensure in-order delivery (packets arrive in the order they were sent)

✅ Provide error-checking (detect and fix corrupted data)

Unlike UDP (which just throws data out, hoping it arrives), TCP carefully sets up, manages, and closes connections.


What’s Inside a TCP Segment?

Before understanding the handshake, it’s useful to know what each TCP packet (called a segment) contains:
The header of a TCP segment can range from 20-60 bytes. 40 bytes are for options. If there are no options, a header is 20 bytes else it can be of upmost 60 bytes.

TCP Header

Header fields:

Field Explanation
Source Port 16-bit number identifying the sending application’s port (e.g., your browser’s port)
Destination Port 16-bit number identifying the receiving application’s port (e.g., web server port 80)
Sequence Number 32-bit number marking the first byte in this segment; helps reorder packets and track delivery
Acknowledgment Number 32-bit number showing the next expected byte; confirms successful receipt of earlier data
Header Length (HLEN) 4-bit number indicating header size (in 4-byte words); minimum 5 (20 bytes), maximum 15 (60 bytes)
Control Flags 6 key flags controlling the connection: URG, ACK, PSH, RST, SYN, FIN
Window Size Communicates how much data the receiver can accept at once (flow control)
Checksum Ensures data integrity; calculated by sender, verified by receiver
Urgent Pointer If URG is set, points to urgent data requiring immediate processing

Key Flags Overview:

  • SYN → Start a connection (Synchronize)
  • ACK → Acknowledge received data
  • FIN → Finish/close the connection
  • RST → Reset the connection
  • PSH → Push data immediately to the application
  • URG → Urgent data included

Step-by-Step: TCP 3-Way Handshake

The handshake synchronizes sequence numbers and ensures both client and server are ready to communicate.

Image description


1. SYN → Client → Server

The client sends:

  • SYN flag set
  • Picks an initial Sequence Number (e.g., 1000)

🗣 Client says:

"I want to connect. Here’s my starting number (1000)."


2. SYN-ACK → Server → Client

The server responds:

  • SYN + ACK flags set
  • Picks its own Sequence Number (e.g., 5000)
  • Acknowledges the client’s number (ACK = 1001)

🗣 Server says:

"Got your request (ack 1001). My number is 5000."


3. ACK → Client → Server

The client confirms:

  • ACK flag set
  • Acknowledges the server’s number (ACK = 5001)

🗣 Client says:

"Got your number (ack 5001). Let’s start!"

Connection is now fully established — data can start flowing both ways.


Real-World Example

Imagine Alice wants to chat with Bob:

1️⃣ Alice: “Hi Bob! I want to start a conversation. Here’s message #1000.” (SYN)

2️⃣ Bob: “Hi Alice! Got your hello (ack 1001). My next message is #5000.” (SYN-ACK)

3️⃣ Alice: “Great! Got your hello (ack 5001). Let’s talk!” (ACK)

Now they’re ready to exchange full conversations.


Puzzle Piece Analogy for Sequence Numbers

Imagine you and your friend are mailing puzzle pieces back and forth:

  • You (client) send piece #1000.
  • Your friend (server) says: “Okay, I got #1000, send me #1001 next.”

Meanwhile, your friend sends you #5000, and you say:

“Okay, got it, send #5001 next.”

👉 Each side tracks its own sequence counter:

  • You track what you’re sending and what you expect back.
  • They track what they’re sending and what they expect back.

Why Are Sequence and Acknowledgment Numbers Important?

These numbers:
✅ Ensure no data is lost or duplicated

✅ Allow both sides to reassemble packets correctly, even if they arrive out of order

✅ Provide a reliable stream over an unreliable network

Without them, TCP wouldn’t know if pieces were missing or mixed up.


Final Notes

  • The 3-Way Handshake runs once per connection (unless restarted).
  • Sequence numbers keep increasing with each data exchange.
  • TCP underpins reliable services like websites, emails, video streaming, and file transfers.

Summary Table

Step Who Action
1 → SYN Client Requests connection; sends initial sequence number
2 → SYN-ACK Server Acknowledges client; sends its own sequence number
3 → ACK Client Confirms receipt; connection fully established