Skip to main content

Command Palette

Search for a command to run...

Understanding How TCP Works

Understanding the 3-Way Handshake and Reliable TCP Communication

Updated
3 min read
Understanding How TCP Works

What happens if data is sent without rules?

Imagine two people trying to talk by throwing notes across a crowded room.

Some notes fall.
Some arrive late.
Some arrive twice.
Some arrive in the wrong order.

Now imagine building the internet on top of that chaos.

This is exactly what would happen if computers sent data without rules.

That is why TCP exists.

What is TCP, and why is it needed?

TCP (Transmission Control Protocol) is a set of rules that ensures data is:

  • delivered reliably

  • delivered in the correct order

  • delivered completely

TCP is used when:

  • correctness matters

  • Missing data is unacceptable

Examples:

  • websites

  • APIs

  • File transfers

  • Database connections

Without TCP, most applications would break silently.

Problems TCP is designed to solve

TCP exists to solve very real network problems:

  1. Packet loss
    Data packets can disappear on the network.

  2. Out-of-order delivery
    Packets can arrive in a different order than sent.

  3. Duplicate packets
    The same packet can arrive more than once.

  4. Unknown readiness
    The receiver may not be ready to accept data.

TCP handles all of this for you.

What is the TCP 3-Way Handshake?

Before sending actual data, TCP first establishes a connection.

This setup process is called the 3-Way Handshake.

Its goal is simple:

Make sure both sides are alive, reachable, and ready to talk.

Simple conversation analogy

Think of TCP connection setup like a polite conversation.

  1. Client: “Can we talk?”

  2. Server: “Yes, I can talk. Can you hear me?”

  3. Client: “Yes, I hear you.”

Only after this does real conversation start.

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

Step 1: SYN (Client → Server)

The client sends a SYN message.

Meaning:

“I want to start a connection.”

This also includes a starting sequence number.

Step 2: SYN-ACK (Server → Client)

The server responds with SYN-ACK.

Meaning:

“I acknowledge your request and I am ready too.”

The server sends:

  • its own sequence number

  • acknowledgement of the client’s number

Step 3: ACK (Client → Server)

The client sends a final ACK.

Meaning:

“I acknowledge your response. Let us begin.”

At this point, the connection is established.

Why are three steps needed

Two messages are not enough.

TCP must confirm:

  • client can send

  • client can receive

  • server can send

  • server can receive

The 3-way handshake confirms both directions.

How data transfer works in TCP

Once connected, data flows in segments.

Each segment has:

  • a sequence number

  • an acknowledgement number

These numbers help TCP track:

  • What was sent

  • What was received

  • What needs retransmission

How TCP ensures reliability

TCP guarantees reliability using a few core ideas.

1. Acknowledgements

Every chunk of data must be acknowledged.
No ACK means retry.

2. Sequence numbers

Data is reordered correctly even if packets arrive out of order.

3. Retransmission

Lost packets are resent automatically.

Packet loss example

The application never sees the loss.
TCP fixes it underneath.

How TCP handles order and correctness

TCP:

  • buffers out-of-order packets

  • discards duplicates

  • delivers data to the application only when complete

This is why APIs and databases trust TCP.

How a TCP connection is closed

Just like setup, closure is graceful.

TCP does not just disappear.

Connection termination using FIN and ACK

Closing a TCP connection is a four-step process.

  1. One side sends FIN

  2. The other side sends ACK

  3. The other side sends FIN

  4. First side sends ACK

Meaning:

“I am done sending.”
“I acknowledge.”
“I am done too.”
“Acknowledged.”

This ensures no data is cut off.

TCP connection lifecycle

Every TCP connection follows this lifecycle.