TCP Working: 3-Way Handshake & Reliable Communication
What is TCP and why it is needed?
TCP stands for Transmission Control Protocol. It's a set of rules that ensures data sent over the internet arrives:
In the correct order
Without errors
Without loss
Without duplicates
Think of it as: Registered mail with tracking and confirmation.
- Problems TCP is designed to solve
Out of Order Packets: TCP puts them back in sequence.
Lost Packets: TCP resends missing data.
Duplicate Packets: TCP ignores duplicates.
Corrupted Data: TCP detects and rejects bad data.
- What is the TCP 3-Way Handshake
Before sending any data, TCP establishes a connection. This is called the
3-way handshake:
You: "Can you hear me?" Friend: "Yes, I can hear you. Can you hear me?" You: "Yes, I can hear you too!" Now you both know the connection works.
- Step-by-step working of SYN, SYN-ACK, and ACK?
Step 1: SYN (Synchronize)
Client sends SYN packet to server.
Message: "Hello Server! Can we talk? My sequence number is 100."
Step 2: SYN-ACK (Synchronize-Acknowledge)
Server responds with SYN-ACK.
Message: "Yes, I got your 100! My sequence number is 200."
Step 3: ACK (Acknowledge)
Client sends ACK.
Message: "Got your 200! Ready to send data."
Connection established! Data transfer begins.
What Are Sequence Numbers?
Sequence numbers track bytes sent.
If you send 100 bytes starting at sequence 1000: - Sequence number = 1000 - Next expected ACK = 1100 This ensures order and completeness.
- How data transfer works in TCP?
Once connected, data flows with acknowledgements.
The Process:
Client sends data packet with sequence number
Server receives and sends ACK with next expected sequence
Client sends next packet
Repeat until all data sent
- How TCP ensures reliability, order, and correctness?
TCP uses multiple mechanisms:
1. Sequence Numbers:
Every byte gets a number. Receiver knows if something's missing.
2. Acknowledgements (ACKs):
Receiver confirms what it received. If sender doesn't get ACK, it resends.
3. Checksums:
Data integrity check. Corrupted data gets rejected and retransmitted.
4. Timeouts & Retransmission:
If ACK doesn't arrive within timeout, sender retransmits.
Ensuring Order
Even if packets arrive out of order, TCP uses sequence numbers to reorder them.
Example: Received: Packet 3, Packet 1, Packet 2 TCP reorders: Packet 1, Packet 2, Packet 3
Ensuring Correctness
Checksum validates data integrity. If checksum fails, packet is discarded and retransmitted.
- How a TCP connection is closed?
TCP uses a 4-way handshake to close connections gracefully.
4-Way Termination
Step 1: Client sends FIN ("I'm done sending")
Step 2: Server sends ACK ("Got it")
Step 3: Server sends FIN ("I'm also done")
Step 4: Client sends final ACK ("Goodbye")
Connection closed!
TCP Connection Lifecycle
A TCP connection goes through three phases:
Establish — 3-way handshake
]Data Transfer — Send/receive with ACKs
Terminate — 4-way close
Why TCP Matters
TCP is used everywhere:
Web browsing (HTTP/HTTPS)
Email (SMTP, IMAP)
File transfers (FTP)
SSH (remote access)
When reliability matters: Use TCP.
When speed matters more: Use UDP (no handshakes, no guarantees).
Quick Summary
TCP = Reliable communication protocol 3-Way Handshake: SYN → SYN-ACK → ACK Data Transfer: Send data → Get ACK → Send more Reliability: Sequence numbers track order - ACKs confirm receipt - Timeouts trigger retransmission - Checksums verify correctness Connection Close: FIN → ACK → FIN → ACK



