Back to Blogs
Web Basics

How TCP Works: Handshake, Data Transfer, and Connection Close

P
Punyansh Singla
January 30, 202613 min read
TCPNetworking3-Way HandshakeInternet BasicsBackend SystemsSystem DesignBeginner Guide

Every time you open a website, submit a form, or call an API, TCP is quietly working in the background.

You rarely see it.

But without TCP, the internet would feel broken, unreliable, and unpredictable.

Let’s understand how TCP actually works, step by step, without diving into packet dumps or protocol diagrams.

What is TCP and why is it needed?

TCP stands for Transmission Control Protocol.

TCP exists because the internet itself is unreliable.

Networks can:

  • Drop packets
  • Deliver packets out of order
  • Deliver the same packet twice
  • Deliver nothing at all

If we sent data without rules, you would experience:

  • Broken web pages
  • Corrupted downloads
  • Incomplete API responses

TCP fixes this.

Problems TCP is designed to solve

TCP is designed to answer these questions:

  • Did all the data arrive?
  • Did it arrive in the correct order?
  • Should missing data be resent?
  • When should communication start and stop?

Without TCP, every application would have to solve these problems on its own.

What is the TCP 3-Way Handshake?

Before any data is sent, TCP establishes a connection.

This process is called the 3-way handshake.

It ensures:

  • Both sides are ready
  • Both sides agree to communicate
  • Sequence numbers are synchronized

Think of it as a short conversation before talking seriously.

The handshake analogy

Imagine calling someone on the phone.

  1. You say: “Hello?”
  2. They reply: “Hello, I can hear you.”
  3. You respond: “Great, let’s talk.”

Only then does the real conversation begin.

TCP works the same way.

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

Let’s walk through each step slowly.

Step 1: SYN (Client → Server)

The client sends a SYN message.

SYN means synchronize.

What the client is saying:

> “I want to start a connection, and here is my starting sequence number.”

At this point:

  • No data is sent
  • Only intent is declared

Step 2: SYN-ACK (Server → Client)

The server responds with SYN-ACK.

This message means two things:

  1. “I acknowledge your request.”
  2. “Here is my own starting sequence number.”

The server is saying:

> “I’m ready, and I understand your sequence numbers.”

Step 3: ACK (Client → Server)

The client sends a final ACK.

This confirms:

  • The server’s sequence number
  • The connection is established

At this point:

> The TCP connection is open.

Now data can flow.

How data transfer works in TCP

Once the connection is established, data moves in segments.

Each segment includes:

  • Sequence number
  • Data payload
  • Check information

Sequence numbers (high level)

Sequence numbers help TCP answer:

  • Which piece of data is this?
  • What came before?
  • What comes next?

They allow the receiver to:

  • Reorder data
  • Detect missing segments
  • Detect duplicates

Acknowledgements (ACKs)

For every chunk of data received, the receiver sends an ACK.

The ACK tells the sender:

> “I have received everything up to this point.”

If something is missing:

  • The ACK stops advancing
  • The sender knows something went wrong

How TCP handles packet loss

Packet loss is normal on the internet.

TCP handles this calmly.

What happens when a packet is lost?

  1. Sender sends data
  2. Receiver notices a gap
  3. ACKs stop moving forward
  4. Sender waits for a timeout
  5. Missing data is resent

No panic. No guessing.

Just retry.

How TCP ensures reliability, order, and correctness

TCP achieves reliability using:

  • Sequence numbers
  • Acknowledgements
  • Retransmissions
  • Checksums
  • Flow control

Together, these ensure:

  • Data arrives once
  • Data arrives in order
  • Corruption is detected

Applications don’t need to worry about these details.

How a TCP connection is closed

Connections must end cleanly.

TCP uses a four-step close, based on FIN and ACK.

Step 1: FIN

One side sends FIN.

This means:

> “I’m done sending data.”

Step 2: ACK

The other side replies with ACK.

> “I understand you’re done.”

Step 3: FIN (other direction)

The second side sends its own FIN.

> “I’m done too.”

Step 4: ACK

Final acknowledgement is sent.

The connection is now closed.

Why TCP doesn’t close instantly

TCP waits to ensure:

  • All data is delivered
  • No delayed packets are lost
  • The connection ends gracefully

This prevents half-open connections and data loss.

The full TCP lifecycle

code
Connection Establishment (3-way handshake)
↓
Reliable Data Transfer
↓
Connection Termination

This lifecycle repeats billions of times every day.

Why backend developers should care

If you build:

  • APIs
  • Web applications
  • Distributed systems

You are building on TCP.

Understanding TCP helps you:

  • Debug timeouts
  • Understand latency
  • Design scalable systems
  • Reason about failures

Mental model to remember

  • TCP is careful
  • TCP checks everything
  • TCP retries when needed
  • TCP closes politely

It trades speed for correctness.

Final thoughts

TCP is not complicated.

It is methodical.

It assumes the network will fail and plans for it.

That mindset is why TCP has survived decades of internet growth.

Once you understand TCP’s flow, networking stops being mysterious.

It becomes logical, predictable, and debuggable.

Enjoyed this article?

Share it with your network

Punyansh's AssistantOnline
AI

Hey there! 👋 I'm Punyansh's AI assistant. Feel free to ask me anything about his skills, projects, or experience!

20 LEFT