Labs ICT
⭐ Pro Login

TCP (Transmission Control Protocol)

The reliable, ordered delivery protocol that powers the internet.

The Reliable Protocol

TCP (Transmission Control Protocol) is the workhorse of the internet. It provides reliable, ordered, error-checked delivery of data. When you load a webpage, send an email, or transfer a file, TCP makes sure every byte arrives correctly.

TCP is like a certified mail service β€” every piece of data is tracked, acknowledged, and retransmitted if lost. It's slower than UDP, but you can trust it with your data.

The Three-Way Handshake

Before TCP can send data, it establishes a connection using the famous three-way handshake:


  Client                          Server
    β”‚                               β”‚
    │──── SYN ─────────────────────→│  Step 1: Client says "I want to connect"
    β”‚                               β”‚
    │←─── SYN-ACK ─────────────────│  Step 2: Server says "OK, I'm ready too"
    β”‚                               β”‚
    │──── ACK ─────────────────────→│  Step 3: Client says "Great, let's start"
    β”‚                               β”‚
    │═══════════════════════════════│
    β”‚     Connection Established    β”‚
    │═══════════════════════════════│

This handshake ensures both sides are ready and agree on initial sequence numbers. It's like two people confirming they're both on the phone before starting a conversation.

How TCP Ensures Reliability

  • Sequence Numbers β€” Every byte of data is numbered. The receiver can detect missing data and reorder out-of-order segments.
  • Acknowledgments (ACKs) β€” The receiver sends ACKs to confirm which data it has received. If the sender doesn't receive an ACK within a timeout period, it retransmits the data.
  • Checksum β€” Each segment includes a checksum. If the data is corrupted in transit, the receiver detects it and discards the segment.
  • Flow Control β€” The receiver tells the sender how much buffer space it has available (the window size). This prevents the sender from overwhelming the receiver.
  • Congestion Control β€” TCP monitors network conditions and slows down when congestion is detected. Algorithms like Slow Start, Congestion Avoidance, Fast Retransmit, and Fast Recovery manage this.

TCP Segment Structure


  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Src Port   β”‚ Dst Port   β”‚                          β”‚
  β”‚ (16 bits)  β”‚ (16 bits)  β”‚                          β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      TCP Header          β”‚
  β”‚ Sequence Number          β”‚      (20-60 bytes)      β”‚
  β”‚ (32 bits)                β”‚                          β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                          β”‚
  β”‚ Acknowledgment Number    β”‚                          β”‚
  β”‚ (32 bits)                β”‚                          β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                          β”‚
  β”‚Header β”‚Flagsβ”‚  Window    β”‚                          β”‚
  β”‚Length β”‚SYN  β”‚  Size      β”‚                          β”‚
  β”‚       β”‚ACK  β”‚  (16 bits) β”‚                          β”‚
  β”‚       β”‚FIN  β”‚            β”‚                          β”‚
  β”‚       β”‚RST  β”‚            β”‚                          β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
  β”‚ Checksum    β”‚ Urgent Ptrβ”‚        Data              β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

TCP Connection Termination

When a TCP connection is done, it's closed with a four-way handshake:


  Client                          Server
    β”‚                               β”‚
    │──── FIN ─────────────────────→│  "I'm done sending"
    │←─── ACK ─────────────────────│  "Acknowledged"
    │←─── FIN ─────────────────────│  "I'm done too"
    │──── ACK ─────────────────────→│  "Acknowledged"
    β”‚                               β”‚
    β”‚     Connection Closed         β”‚

Both sides must independently close their sending direction. This ensures all data is delivered before the connection is fully terminated.

πŸ§ͺ Quick Quiz

What is the three-way handshake in TCP?