ℹ️ WHAT IS TCP?CH.1
1
The Reliable Postman

TCP (Transmission Control Protocol) is a connection-oriented, reliable transport protocol that ensures data is delivered correctly and in order. It's one of the core protocols of the Internet, sitting in Layer 4 of the OSI model.

Reliable

  • ✅ Guarantees data delivery
  • ✅ Error checking built-in
  • ✅ Automatic retransmission
  • ✅ Data never silently lost

Connection-Oriented

  • 📞 Handshake before sending
  • 📞 Dedicated connection maintained
  • 📞 Proper teardown when done
  • 📞 Both sides aware of state

Ordered

  • 🔢 Sequence numbers on packets
  • 🔢 Receiver reorders if needed
  • 🔢 App gets data in right order
  • 🔢 No out-of-sequence delivery

Flow Controlled

  • 🚦 Sliding window protocol
  • 🚦 Prevents overwhelming receiver
  • 🚦 Congestion control (slow start)
  • 🚦 Adapts to network conditions
🤝 THREE-WAY HANDSHAKECH.2
2
SYN-SYN/ACK-ACK

Before any data is sent, TCP establishes a connection using the famous three-way handshake. Think of it as knocking on the door and waiting to be invited in.

1
➡️
CLIENT → SERVER: SYN (seq=x)
"Hey server, I want to connect! I'll use sequence number x. Are you listening?"
2
⬅️
SERVER → CLIENT: SYN-ACK (seq=y, ack=x+1)
"Got it! I acknowledge your x. Here's my sequence number y. Ready to connect!"
3
➡️
CLIENT → SERVER: ACK (seq=x+1, ack=y+1)
"Acknowledged your y! Connection established. Let's send data now!"
Why three steps? Two would only confirm one direction works. Three ensures BOTH client→server AND server→client channels are working properly before data flows!
🧠 KEY TCP FEATURESCH.3
3
Under the Hood

Flow Control

Prevents overwhelming the receiver by controlling data transmission rate using sliding window protocol.

// Sliding Window Protocol Window Size = min(receiver_buffer, network_capacity) Sender waits for ACK before sending more data

Error Detection

Uses checksums to detect corrupted data and requests retransmission automatically.

// Checksum Calculation checksum = sum_of_all_bytes % 65536 if (checksum != received) { request_retransmission(); }

Congestion Control

Manages network traffic to prevent congestion collapse using slow-start and AIMD algorithms.

// Slow Start Algorithm cwnd = 1; // Congestion Window while (cwnd < ssthresh) { cwnd *= 2; // Exponential } // Then: linear growth

Ordered Delivery

Sequence numbers ensure packets arrive at the application in correct order, regardless of network routing.

// Sequence Numbers seq_num = initial_seq + bytes_sent Receiver buffers out-of-order packets, delivers in sequence
📦 TCP HEADER STRUCTURECH.4
4
The Packet Anatomy

Every TCP packet has a minimum 20-byte header carrying critical control information:

FieldSizePurpose
Source Port16 bitsSender's port number (identifies sending application)
Dest Port16 bitsReceiver's port number (identifies destination app)
Sequence Number32 bitsPosition of this segment in the byte stream
Acknowledgment32 bitsNext expected byte from the other side
Header Length4 bitsLength of the TCP header in 32-bit words
Flags (Control)6 bitsSYN, ACK, FIN, RST, PSH, URG control bits
Window Size16 bitsNumber of bytes receiver can accept (flow control)
Checksum16 bitsError detection over header and data
Urgent Pointer16 bitsPoints to urgent data (when URG flag set)
OptionsVariableOptional: MSS, window scaling, timestamps
DataVariableApplication payload
🔄 TCP CONNECTION STATESCH.5
5
The State Machine

TCP connections move through well-defined states during their lifecycle:

CLOSED
No connection exists
LISTEN
Server waiting for SYN
SYN_SENT
Client sent SYN, waiting
SYN_RCVD
Server got SYN, sent SYN-ACK
ESTABLISHED
✅ Data transfer active
FIN_WAIT_1
Sent FIN, closing
FIN_WAIT_2
Got ACK for FIN
CLOSE_WAIT
Got FIN, sending data
LAST_ACK
Sent FIN, await ACK
TIME_WAIT
Waiting 2xMSL timeout
CLOSING
Both closing simultaneously
CLOSED
✅ Connection ended
⚔️ TCP VS UDP COMPARISONCH.6
6
Choose Your Fighter
FeatureTCPUDP
ConnectionConnection-oriented (handshake)Connectionless (fire & forget)
ReliabilityHigh — guaranteed deliveryLow — best effort only
OrderingGuaranteed in-orderNot guaranteed
SpeedSlower (overhead of ACKs)Faster (minimal overhead)
Header Size20 bytes minimumOnly 8 bytes
Flow ControlYes (sliding window)No
Congestion ControlYes (slow start, AIMD)No
Use CasesWeb, email, file transfer, SSHGaming, streaming, DNS, VoIP
Mental model: TCP is like a certified mail service — slow but every package arrives and is confirmed. UDP is like dropping a flyer in people's mailboxes — fast, but some might end up in the wind!
🔑 COMMON TCP PORTSCH.7
7
Port Directory
Web Services
  • 80 ─ HTTP
  • 443 ─ HTTPS
  • 8080 ─ HTTP Alternate
  • 8443 ─ HTTPS Alternate
Email
  • 25 ─ SMTP
  • 110 ─ POP3
  • 143 ─ IMAP
  • 587 ─ SMTP Submission
File Transfer / Remote
  • 21 ─ FTP Control
  • 22 ─ SSH / SFTP
  • 23 ─ Telnet
  • 3389 ─ RDP
Databases
  • 3306 ─ MySQL
  • 5432 ─ PostgreSQL
  • 1433 ─ SQL Server
  • 27017 ─ MongoDB