TCP Working: 3-Way Handshake & Reliable Communication Explained
Understand SYN, SYN-ACK, ACK, reliable data transfer, retransmissions, and how TCP connections close — in a beginner-friendly way.

Hi, I’m Shubham 👋 A learner sharing my journey in programming, tech, and self-growth. Learning every day and writing what I learn.
What is TCP and why is it needed?
TCP (Transmission Control Protocol) is a transport-layer protocol that makes sure data sent between two machines (like your browser and a web server) is:
delivered reliably (nothing silently lost),
received in order, and
free of duplication.
Think of TCP like a registered courier service: it tracks every parcel, confirms delivery, and asks for re-delivery if something goes missing. Without TCP, data would be “fire-and-forget” (like shouting across a stadium) — fast but unreliable.
Problems TCP is designed to solve
Without rules, these problems appear:
Packet loss — packets may disappear in the network.
Out-of-order delivery — packets may take different routes and arrive shuffled.
Duplication — the same packet could arrive twice.
Network congestion — too much traffic leads to dropped packets and delays.
TCP provides mechanisms (handshake, sequence numbers, ACKs, retransmission, flow & congestion control) to handle all of the above.
The 3-Way Handshake — what it is (high level)
Before any real data flows, TCP establishes a connection with a 3-step handshake so both sides agree:
Client asks to open a connection (SYN).
Server agrees and also signals readiness (SYN-ACK).
Client confirms (ACK).
After that, they can exchange data.
Analogy: Client knocks on the door (SYN). Server opens a little and says “I hear you, I’m ready” (SYN-ACK). Client says “Great, I’m in” (ACK). Now they start talking.
Step-by-step with a tiny example
Let’s use simple sequence numbers to follow the handshake:
Client → Server:
SYN, Seq = 1000
(Client picks an initial sequence number 1000, meaning "my first byte will have index 1001".)Server → Client:
SYN-ACK, Seq = 5000, Ack = 1001
(Server picks ISN 5000 and acknowledges client’s SYN by settingAck = client_ISN + 1.)Client → Server:
ACK, Seq = 1001, Ack = 5001
(Client acknowledges server’s SYN byAck = server_ISN + 1.)
Connection is now established — both sides know each other’s sequence space and can send data.
ASCII diagram (handshake):
Client Server
| ------ SYN (Seq=1000) ---> |
| <--- SYN-ACK (Seq=5000, Ack=1001) --- |
| ------ ACK (Seq=1001, Ack=5001) ---> |
(connection established)
How data transfer works in TCP (high level)
Once established, data is sent in segments. Each segment carries:
a sequence number (so the receiver can order bytes), and
the receiver sends ACKs to tell the sender which data it has received.
Key ideas:
Sequence numbers identify byte positions in the stream.
Acknowledgements (ACKs) are usually cumulative (
Ack = next expected byte).Sliding window: receiver advertises a window size — how many bytes it can accept without further ACKs. This controls flow (prevents overwhelming the receiver).
Example flow (simplified):
Client sends bytes 1001–2000 (Seq=1001, Len=1000)
Server receives and replies: ACK Ack=2001 (meaning "I got up to 2000; next I expect 2001")
How TCP ensures reliability, order, and correctness
Sequence numbers let the receiver reorder out-of-order segments.
Cumulative ACKs tell the sender what has been received; missing bytes are detected as gaps.
Retransmission timers: if the sender does not get an ACK within a timeout, it retransmits the unacknowledged data.
Fast retransmit: multiple duplicate ACKs for the same byte can trigger a faster retransmit without waiting the full timeout.
Flow control (windowing): the receiver tells the sender how much data it can accept (receiver’s advertised window).
Congestion control (TCP algorithms): TCP adjusts its send rate (window) based on network conditions to avoid overwhelming the network (slow start, congestion avoidance, etc.).
Checksums: every TCP segment has a checksum to detect corruption; corrupted segments are discarded and will be retransmitted.
All these together make TCP reliable and ordered.
Handling packet loss (simple)
If a segment is lost:
Receiver will not ACK the missing bytes (or will ACK up to the last contiguous byte it has).
Sender notices missing ACKs (or sees duplicate ACKs) and retransmits the missing segment.
Once the missing segment arrives, receiver can deliver bytes in order to the application.
Connection termination (closing a TCP connection)
TCP uses FIN and ACK to close a connection. Commonly this is a four-step process (each side closes independently):
Client → Server:
FIN, Seq = X(client has finished sending)Server → Client:
ACK, Ack = X+1(server acknowledges client's FIN)Server → Client:
FIN, Seq = Y(server finishes sending)Client → Server:
ACK, Ack = Y+1(client acknowledges server's FIN)
After the final ACK, the connection transitions to TIME_WAIT (on the side that closed first) to ensure last packets are handled and late duplicates are discarded.
ASCII diagram (close):
Client Server
| ------ FIN (Seq=X) -----> |
| <----- ACK (Ack=X+1) ----- |
| <----- FIN (Seq=Y) ------- |
| ------ ACK (Ack=Y+1) ---> |
(connection closed)
Why TIME_WAIT? To ensure any delayed packets from the old connection are not mistaken for a new connection if ports/sequence numbers are reused.
Quick summary / cheat-sheet
TCP = reliable, ordered, connection-oriented.
3-way handshake (SYN, SYN-ACK, ACK) establishes a connection and synchronizes sequence numbers.
Data transfer uses sequence numbers, ACKs, sliding windows, retransmission timers, and checksums.
Reliability is achieved via ACKs, retransmission, and ordering.
Close uses FIN/ACK (four-step) and TIME_WAIT to safely terminate.
Small analogies recap
3-way handshake = polite knocking + handshake before entering.
Sequence + ACK = numbered pages of a book; receiver tells which page it expects next.
Retransmission = resend a page if the courier lost it.
Flow control = receiver saying “send me 3 pages at a time” so it can keep up.
Congestion control = courier slowing down deliveries when the road is crowded.




