Skip to main content

Command Palette

Search for a command to run...

Understanding TCP: How Reliable Communication Works on the Internet

Updated
8 min read
Understanding TCP: How Reliable Communication Works on the Internet

When you open a website, send a message, or watch a video online, data travels across the internet to reach you.
But have you ever wondered:

How does the internet make sure data arrives correctly, in order, and without missing parts?

The answer is TCP — Transmission Control Protocol.

In this article, we’ll understand what TCP is, why it exists, and how it works, step by step — no deep networking background required.

What Happens If Data Is Sent Without Rules?

Imagine this situation:

  • You send a long message to a friend

  • The message is broken into small pieces

  • These pieces travel through different paths

  • Some pieces:

    • Arrive late

    • Arrive out of order

    • Never arrive at all

Your friend might receive:

“world Hello you are how”

That’s exactly what would happen without rules.

The internet needs a system that ensures:

  • All pieces arrive

  • They arrive in the correct order

  • Missing pieces are resent

That system is TCP.

What Is TCP and Why Is It Needed?

TCP (Transmission Control Protocol) is a communication protocol that ensures reliable data transfer between two devices over the internet.

TCP is needed because it guarantees:

  • Data is delivered correctly

  • Data arrives in the correct order

  • Missing data is retransmitted

  • Sender and receiver agree before communication starts

TCP is used by:

  • Web browsing (HTTP/HTTPS)

  • Emails

  • File transfers

  • Most internet applications

Problems TCP Is Designed to Solve

TCP exists to solve these major problems:

ProblemSolution by TCP
Data lossRetransmission
Out-of-order packetsSequence numbers
Duplicate dataAcknowledgements
Unknown sender/receiver stateHandshake
Sudden disconnectionGraceful termination

What Is the TCP 3-Way Handshake?

Before sending any data, TCP first establishes a connection.

This process is called the 3-Way Handshake.

Think of it like starting a phone call :

  1. Client: “Can I talk?”

  2. Server: “Yes, I’m listening”

  3. Client: “Great, let’s talk”

Only after this agreement does data transfer begin.

Step-by-Step TCP 3-Way Handshake

Step 1: SYN (Client → Server)

The client sends a SYN message.

  • Means: “I want to start a connection”

  • Includes a sequence number

  • Client sends a packet with:

    • SYN = 1

    • Initial sequence number (say x)

SYN = Synchronize

Step 2: SYN-ACK (Server → Client)

The server responds with SYN-ACK.

  • SYN: “I agree to connect”

  • ACK: “I acknowledge your request”

  • Includes its own sequence number

  • This one packet contains both flags:

    • SYN = 1 → server’s own sequence number (y)

    • ACK = 1 → acknowledging client’s SYN (ACK = x + 1)

    • Important:
      The server does NOT send:

    • one packet with SYN

    • then another packet with ACK

It sends both together.

Step 3: ACK (Client → Server)

The client sends ACK.

  • Means: “I acknowledge your response”

  • Client sends:

    • ACK = y + 1
  • Connection is now established

Now both sides are ready to send data.

Why SYN and ACK Are Combined?

Efficiency and correctness:

  • Reduces extra packets

  • Confirms both directions of communication

  • Saves time and network overhead

What are x and y?

  • xClient’s initial sequence number

  • yServer’s initial sequence number

Each side chooses its own starting sequence number.

Correct Step-by-Step (with x and y)

1️⃣ Client → Server : SYN

  • Client sends:

    • SYN = 1

    • Sequence Number = x

Meaning:

“I want to start a connection, and my first byte will start at x.”

No ACK yet, because the server hasn’t sent anything.

2️⃣ Server → Client : SYN-ACK

Server does two things in the same packet:

  1. Acknowledges the client’s SYN

  2. Sends its own SYN

So the server sends:

  • SYN = 1

  • ACK = 1

  • Sequence Number = y

  • Acknowledgement Number = x + 1

Meaning:

“I received your SYN (x), and I’m ready too.

My sequence numbers start at y.”

The x + 1 acknowledges the SYN itself, not data.

3️⃣ Client → Server : ACK

Now the client acknowledges the server’s SYN.

Client sends:

  • ACK = 1

  • Sequence Number = x + 1

  • Acknowledgement Number = y + 1

Meaning:

“I received your SYN (y). We are fully connected.”

Visual Summary

Client                                  Server

SYN, Seq = x  ------------------------>
                    SYN-ACK, Seq = y
                    ACK = x + 1  <------
ACK = y + 1 ------------------------>

Diagram Idea: TCP 3-Way Handshake

Client                    Server
  | -------- SYN --------> |
  | <----- SYN + ACK ----- |
  | -------- ACK --------> |

Note: The 3-way handshake is performed once when the connection is created, not for every packet.

The 3-way handshake happens once per TCP connection to exchange initial sequence numbers and establish a reliable session; subsequent data uses the same connection.

  • The initial sequence numbers (x and y) are exchanged only during the handshake

  • After that, sequence numbers continue increasing as data is sent

  • They are not re-chosen again unless a new connection is created

  • Handshake:

    • Client picks x

    • Server picks y

  • Data transfer:

    • Client sends data starting from x + 1, x + 2, …

    • Server sends data starting from y + 1, y + 2, …

No new handshake, no new starting numbers.

How Data Transfer Works in TCP

Once the connection is established:

  • Data is split into segments

  • Each segment has:

    • Sequence number

    • Acknowledgement number

Sequence Numbers

  • Every piece of data is numbered

  • Helps the receiver:

    • Reassemble data in order

    • Detect missing pieces

Acknowledgements (ACKs)

  • Receiver sends ACKs saying:

    “I received data up to this point”

Diagram Idea: Data Transfer

Client → Server: Seq = 1 (Data)
Server → Client: ACK = 2
Client → Server: Seq = 2 (Data)

How TCP Handles Packet Loss

What if a packet is lost?

  1. Sender sends data

  2. Receiver does not send ACK

  3. Sender waits

  4. Timer expires

  5. Sender retransmits the missing data

This is how TCP ensures no data is lost.

Diagram Idea: Packet Loss & Retransmission

Client → Server: Packet 3 (lost)
Client waits...
Client → Server: Packet 3 (resent)
Server → Client: ACK

How TCP Ensures Reliability, Order, and Correctness

TCP achieves reliability through:

  1. Reliability

  • Retransmission of lost packets
  1. Order

  • Sequence numbers ensure correct order
  1. Correctness

  • Checksums detect corrupted data
  1. Flow Control

  • Prevents sender from overwhelming receiver

How a TCP Connection Is Closed

TCP closes connections gracefully, not abruptly.

Connection Termination Steps

  1. FIN – “I’m done sending data”

  2. ACK – “I acknowledge”

  3. FIN – Other side finishes

  4. ACK – Final confirmation

TCP is full-duplex (both sides send data independently), so each direction must be closed separately.

Why 4 Steps Are Needed

  • One side finishes sending data first

  • The other side may still be sending data

  • So TCP closes one direction at a time

TCP 4-Way Termination (Step by Step)

1️⃣ FIN (Client → Server)

“I’m done sending data.”

  • Client sends FIN = 1

2️⃣ ACK (Server → Client)

“I acknowledge your FIN.”

  • Server sends ACK

Connection is half-closed now.

3️⃣ FIN (Server → Client)

“I’m done sending data too.”

  • Server sends FIN = 1

4️⃣ ACK (Client → Server)

“I acknowledge your FIN.”

  • Client sends ACK

Connection is fully closed.

Diagram You Can Add to the Article

Client                          Server

FIN  ------------------------>
       <--------------------- ACK

       <--------------------- FIN
ACK  ------------------------>

Connection closed

Closing a TCP Connection (4-Way Handshake)

Unlike connection establishment, TCP connection termination requires four steps. This is because TCP allows both sides to send data independently. One side sends a FIN to indicate it has finished sending data, which is acknowledged by the other side. When the other side is also done, it sends its own FIN, which is again acknowledged. After this exchange, the TCP connection is safely closed.

  • Connection start → 3-way handshake (SYN, SYN-ACK, ACK)

  • Connection end → 4-way handshake (FIN, ACK, FIN, ACK)

Do TCP Sequence Numbers Reset for New Connections?

Each TCP connection is independent.

When a TCP connection is closed, all its sequence numbers and connection state are discarded.
If the client and server create a new connection, they perform a new 3-way handshake and choose new initial sequence numbers.

Sequence numbers do not continue from the previous connection — they start fresh for every new TCP connection.

How Sequence Numbers Behave

  • The initial sequence numbers (x and y) are exchanged only during the handshake

  • After that, sequence numbers continue increasing as data is sent

  • They are not re-chosen unless a new connection is created

Simple Example

Handshake:

  • Client picks x

  • Server picks y

Data transfer:

  • Client sends data starting from x + 1, x + 2, …

  • Server sends data starting from y + 1, y + 2, …

i. No new handshake
ii. No new starting numbers
iii. Same connection, continuous data flow

Final Takeaway

TCP establishes a connection once using a 3-way handshake, exchanges initial sequence numbers, transfers data reliably, and then closes the connection. Any new communication requires a new connection with new sequence numbers.

TCP Connection Lifecycle

ESTABLISH → DATA TRANSFER → CLOSE

More from this blog

From Basics to Binary

13 posts

A learning-focused tech blog documenting my journey from fundamentals to real-world software engineering, covering version control, programming basics, system thinking, and lessons learned.