DEV Community

Cover image for Understanding TCP 3-Way Handshake: The Heartbeat of Internet Communication
Siddhant Bansal
Siddhant Bansal

Posted on

Understanding TCP 3-Way Handshake: The Heartbeat of Internet Communication

๐Ÿ”— 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

Top comments (4)

Collapse
 
priyankeshh profile image
Priyankesh

Definitely adding this to my to-read list!

Collapse
 
kartik_26d6e2b5a209e66e45 profile image
Kartik

Amazing content

Collapse
 
krishna_bansal_4905b3822f profile image
Krishna Bansal

Usefull content

Collapse
 
kamalmost profile image
KamalMostafa

good article, simple and easy to follow. I suggest to consider adding the full trace for client and sever communication lets say sending a file of size 1MB including the closing of the socket.