Lesson 4 of 7

TCP & UDP — Reliable vs Fast

IP can lose, duplicate or reorder packets. TCP adds reliable, ordered delivery on top, while UDP skips those guarantees for speed, and you'll learn when each is used.

Text
TCP opens with a handshake (one round trip):

  you  --- SYN ------->  server    "want to talk, seq=X"
  you  <-- SYN-ACK ----  server    "sure, seq=Y, got X"
  you  --- ACK ------->  server    "got Y — we're on"

Then every byte is numbered:
  - receiver ACKs what arrived
  - sender retransmits anything unACKed (loss!)
  - sequence numbers let receiver reorder & dedupe
  - flow control: receiver says how much it can take
  - congestion control: sender probes how fast the
    NETWORK can take it, backs off when loss appears

Result: a reliable byte stream over an unreliable network.

Ports let one machine hold many conversations: a connection is the 4- (source IP, source port, destination IP, destination port). Servers listen on well-known ports — 443 HTTPS, 80 , 22 SSH, 53 DNS — while your side uses a random high port per connection. A 'connection refused' means the machine answered but nothing was listening on that port.

Bash
# See your machine's TCP conversations right now:
netstat -an | head -20      # (or: ss -t on Linux)
# ESTABLISHED = active   LISTEN = server waiting

# UDP: no handshake, no ACKs, no retransmit, no order.
# Just send the packet. Good for when late data is
# worthless — a lost frame of a video call shouldn't
# be replayed 2 seconds later.
#   DNS queries    UDP (tiny, just re-ask on loss)
#   video calls    UDP (skip the glitch, keep going)
#   games          UDP (old positions are useless)
#   web, email     TCP (every byte must arrive)
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.