COMPUTER NETWORKS / 3. TRANSPORT LAYER — TCP & UDP
Transport Layer — TCP & UDP
Reliable ordered delivery (TCP) vs fast fire-and-forget (UDP)
EXPLANATION
The Transport Layer provides end-to-end communication between applications on different hosts. While IP gets packets to the right machine, the transport layer gets them to the right application using port numbers. Port numbers (0–65535): • 0–1023: Well-known ports (root required on Linux): HTTP=80, HTTPS=443, SSH=22, FTP=21, DNS=53, SMTP=25 • 1024–49151: Registered ports (apps register with IANA): PostgreSQL=5432, MySQL=3306, MongoDB=27017 • 49152–65535: Ephemeral/dynamic ports — OS assigns these to client connections TCP (Transmission Control Protocol) — reliable, ordered, connection-based: • Connection-oriented: must establish a connection before sending data (3-way handshake) • Reliable: every segment is acknowledged. Lost segments are retransmitted • Ordered: segments arrive in the exact order sent (TCP reorders if needed) • Flow control: receiver tells sender how much buffer it has (Window Size) — prevents overwhelming slow receivers • Congestion control: slows down when the network is congested (CWND) • Used by: HTTP, HTTPS, SSH, FTP, SMTP, database connections 3-Way Handshake (TCP connection establishment): • SYN → client says "I want to connect, my starting sequence number is X" • SYN-ACK → server says "OK, my starting sequence is Y, I acknowledge X" • ACK → client says "I acknowledge Y" — connection established! • 4-Way Termination: FIN → ACK → FIN → ACK (each side closes independently) UDP (User Datagram Protocol) — fast, connectionless, unreliable: • No handshake, no acknowledgment, no ordering guarantees • Just send datagrams and hope they arrive • Much lower latency — no round trips for setup or acknowledgments • Used by: DNS (quick request/response), video streaming (a dropped frame is fine), VoIP, gaming, QUIC (HTTP/3 uses UDP underneath!) TCP vs UDP choice: if losing a packet means corrupted data, use TCP. If losing a packet is recoverable (next video frame comes along, or you retry yourself), use UDP. Sequence numbers and ACKs: TCP numbers every byte sent. The receiver ACKs the next byte it expects. If sender doesn't receive an ACK in time, it retransmits. This is how TCP achieves reliability.
DIAGRAM
TCP 3-WAY HANDSHAKE:
Client Server
│──── SYN (seq=100) ──────────→│ "I want to connect"
│←─── SYN-ACK (seq=200,ack=101)│ "OK, ready"
│──── ACK (ack=201) ──────────→│ "Connected!"
│ │
│═══ Data transfer begins ════│
TCP 4-WAY TERMINATION:
│──── FIN ──→│ "I'm done sending"
│←── ACK ────│ "OK"
│←── FIN ────│ "I'm done too"
│──── ACK ──→│ "Goodbye" → TIME_WAIT state
TCP vs UDP:
┌─────────────────────┬──────────────────────┐
│ TCP │ UDP │
├─────────────────────┼──────────────────────┤
│ Connection required │ Connectionless │
│ Reliable (ACKs) │ Unreliable (no ACKs) │
│ Ordered │ Unordered │
│ Slower (overhead) │ Faster (minimal) │
│ HTTP, SSH, DB │ DNS, Video, Games │
└─────────────────────┴──────────────────────┘CODE