Skip to main content

Command Palette

Search for a command to run...

TCP: How the handshake establishes trust

Updated
4 min read

TCP Handshake and Reliable Communication (Explained Simply)

When I first started learning networking, one question kept coming up in my head:

What actually happens before two computers start talking?

Because if you think about it, sending data without rules sounds dangerous.

What if:

  • The receiver isn’t ready?

  • Packets get lost?

  • Messages arrive out of order?

  • Data gets corrupted?

If we just blasted data across the internet with no coordination, communication would break constantly.

That’s exactly why Transmission Control Protocol (TCP) exists.


What Is TCP and Why Is It Needed?

TCP is a transport layer protocol.

Its job is simple:

Ensure reliable communication between two machines.

Reliable means:

  • Data arrives

  • Data arrives in order

  • Data is not duplicated

  • Data is not corrupted

  • Both sides agree when communication starts and ends

Without TCP, sending important data (like website HTML or banking transactions) would be risky.


Problems TCP Is Designed to Solve

Let’s imagine sending data without rules.

You send 5 packets:

Packet 1
Packet 2
Packet 3
Packet 4
Packet 5

But the network might:

  • Deliver them as 1, 3, 2, 5, 4

  • Drop packet 3 entirely

  • Duplicate packet 2

  • Deliver packet 5 before packet 1

Networks are messy.

TCP exists to clean up that mess.


The TCP 3-Way Handshake

Before data transfer begins, TCP establishes a connection.

This process is called the 3-way handshake.

Think of it like starting a phone call.

You don’t just start talking randomly.

You confirm both sides are ready.


Simple Conversation Analogy

Imagine this:

Client: “Hey, can we talk?”
Server: “Yes, I hear you. Can you hear me?”
Client: “Yes, I hear you too.”

Now conversation begins.

That’s basically the TCP handshake.


Step-by-Step: SYN, SYN-ACK, ACK

Let’s walk through it clearly.

Step 1: SYN

Client sends:

SYN

Meaning:

“I want to start a connection.”

SYN = Synchronize.

It includes an initial sequence number (more on that soon).


Step 2: SYN-ACK

Server responds with:

SYN-ACK

Meaning:

“I received your request. I’m ready too.”

This message:

  • Acknowledges the client’s SYN

  • Sends the server’s own SYN


Step 3: ACK

Client sends:

ACK

Meaning:

“I received your response.”

Now both sides:

  • Know the other is ready

  • Have synchronized sequence numbers

Connection established.


Visual Flow

Client                          Server
  │                                │
  │ -------- SYN --------------->   │
  │                                │
  │ <------ SYN-ACK -------------  │
  │                                │
  │ -------- ACK --------------->   │
  │                                │
Connection Established

Only after this does real data flow.


What Are Sequence Numbers?

Sequence numbers are how TCP keeps track of order.

Imagine sending a book in pieces:

Chunk 1
Chunk 2
Chunk 3
Chunk 4

Each chunk has a number.

If Chunk 3 is missing, the receiver knows exactly what’s missing.

Sequence numbers:

  • Label bytes

  • Ensure correct order

  • Detect missing data


How Data Transfer Works in TCP

After the handshake:

  1. Sender sends data with sequence numbers.

  2. Receiver sends acknowledgements (ACKs).

  3. ACK indicates:

    “I have received up to byte X.”

If something is missing:

  • The receiver doesn’t acknowledge it.

  • Sender retransmits it.


How TCP Ensures Reliability

TCP guarantees reliability through:

1️⃣ Acknowledgements (ACKs)

Every chunk of data must be acknowledged.

If no ACK arrives:

  • Sender assumes packet loss

  • Retransmits


2️⃣ Retransmission

If packet 3 is lost:

Receiver acknowledges only up to packet 2.

Sender notices missing ACK for 3 and resends it.


3️⃣ Ordering

Even if packets arrive out of order:

1, 3, 2, 4

TCP rearranges them before delivering to the application.

Applications never see chaos.


4️⃣ Data Integrity

Each TCP segment includes checksums.

If corrupted:

  • Packet is discarded

  • Retransmitted


5️⃣ Flow Control

If the receiver is overwhelmed:

It can say:

“Slow down.”

TCP adjusts speed automatically.


How a TCP Connection Is Closed

Ending communication also follows rules.

TCP uses FIN and ACK.

Think of it like politely ending a call.


Connection Termination (Simplified)

Client                          Server
  │                                │
  │ -------- FIN --------------->   │
  │                                │
  │ <------- ACK ---------------   │
  │                                │
  │ <------- FIN ---------------   │
  │                                │
  │ -------- ACK --------------->   │
  │                                │
Connection Closed

FIN = “I’m done sending data.”

Each side closes independently.

It’s not instant — it’s coordinated.


Putting It All Together

Here’s the full lifecycle:

1️⃣ Handshake (SYN → SYN-ACK → ACK)
2️⃣ Data Transfer (Sequence + ACKs)
3️⃣ Retransmissions if needed
4️⃣ Graceful Termination (FIN + ACK)

TCP ensures:

  • Reliable delivery

  • Ordered data

  • Error detection

  • Controlled speed

  • Clean connection setup

  • Clean shutdown


Final Mental Model

If I simplify everything:

Without TCP:
Data = chaotic, unreliable, unpredictable.

With TCP:
Data = ordered, confirmed, guaranteed.

TCP is like a structured conversation:

  • Introduce

  • Confirm readiness

  • Speak carefully

  • Confirm understanding

  • End politely

And that’s why almost everything important on the internet — including web traffic, APIs, and file transfers — depends on TCP.

Reliable communication doesn’t happen by accident.

TCP enforces it.