Understanding How TCP Works
Understanding the 3-Way Handshake and Reliable TCP Communication

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:
Packet loss
Data packets can disappear on the network.Out-of-order delivery
Packets can arrive in a different order than sent.Duplicate packets
The same packet can arrive more than once.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.
Client: “Can we talk?”
Server: “Yes, I can talk. Can you hear me?”
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.
One side sends FIN
The other side sends ACK
The other side sends FIN
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.



