Episode 1 — Fundamentals / 1.1 — How The Internet Works

1.1.b — How Computers Communicate With Each Other

In one sentence: Computers communicate by breaking data into small packets and sending them through layers of protocols (agreed-upon rules) that handle everything from the physical cable to the application you see on screen.


Table of Contents


1. The Big Idea — Protocols

A protocol is a set of rules that computers agree to follow when communicating. Without protocols, computers would be like people speaking different languages in the same room — lots of noise, no understanding.

Real-World Analogy

Sending a physical letter:
┌─────────────────────────────────────────────────┐
│  1. You WRITE the letter           (Application)│
│  2. You put it in an ENVELOPE      (Transport)  │
│  3. You write the ADDRESS          (Network)    │
│  4. The POST OFFICE sorts & routes (Data Link)  │
│  5. A TRUCK carries it physically  (Physical)   │
└─────────────────────────────────────────────────┘

Each step has its own rules — what size envelope,
how to write the address, which truck route to take.
That's exactly how computer protocols work.

2. The OSI Model (7 Layers)

The Open Systems Interconnection (OSI) model was created by the International Organization for Standardization (ISO) in 1984. It divides network communication into 7 layers, each with a specific job.

Memory Aid: "Please Do Not Throw Sausage Pizza Away"

┌─────────────────────────────────────────────────────────────────────┐
│  Layer 7 │ APPLICATION   │ What the user interacts with            │
│          │               │ Protocols: HTTP, HTTPS, FTP, SMTP,      │
│          │               │           DNS, SSH, WebSocket           │
│          │               │ Example: Your web browser, email client │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 6 │ PRESENTATION  │ Data translation, encryption,           │
│          │               │ compression                             │
│          │               │ Protocols: TLS/SSL, JPEG, PNG, GIF,     │
│          │               │           ASCII, Unicode, MPEG          │
│          │               │ Example: HTTPS encryption, image        │
│          │               │          encoding                       │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 5 │ SESSION       │ Opens, manages, and closes              │
│          │               │ communication sessions                  │
│          │               │ Protocols: NetBIOS, RPC, SOCKS          │
│          │               │ Example: Keeping your login session     │
│          │               │          alive                          │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 4 │ TRANSPORT     │ End-to-end delivery, error recovery,    │
│          │               │ flow control                            │
│          │               │ Protocols: TCP, UDP, QUIC               │
│          │               │ PDU: Segment                            │
│          │               │ Example: TCP ensures all packets arrive │
│          │               │          in order                       │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 3 │ NETWORK       │ Logical addressing (IP) and routing     │
│          │               │ across multiple networks                │
│          │               │ Protocols: IP, ICMP, OSPF, BGP          │
│          │               │ PDU: Packet                             │
│          │               │ Example: Router deciding which path     │
│          │               │          to forward a packet            │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 2 │ DATA LINK     │ Node-to-node transfer using MAC         │
│          │               │ addresses, error detection              │
│          │               │ Protocols: Ethernet, Wi-Fi (802.11),    │
│          │               │           ARP                           │
│          │               │ PDU: Frame                              │
│          │               │ Sub-layers: LLC + MAC                   │
│          │               │ Example: Your laptop talking to         │
│          │               │          your router via Wi-Fi          │
├──────────┼───────────────┼─────────────────────────────────────────┤
│  Layer 1 │ PHYSICAL      │ Raw bits transmitted over physical      │
│          │               │ media (electrical, optical, radio)      │
│          │               │ Technologies: Ethernet cables (Cat5e,   │
│          │               │   Cat6), fiber optic, Wi-Fi radio,      │
│          │               │   Bluetooth                             │
│          │               │ PDU: Bits (0s and 1s)                   │
│          │               │ Example: Electrical signals on a        │
│          │               │          copper wire                    │
└──────────┴───────────────┴─────────────────────────────────────────┘

What is a PDU (Protocol Data Unit)?

Each layer wraps the data in its own "envelope" called a PDU:

Layer 7-5:  Data
Layer 4:    Segment   (TCP) or Datagram (UDP)
Layer 3:    Packet
Layer 2:    Frame
Layer 1:    Bits

3. The TCP/IP Model (4 Layers)

The TCP/IP model (also called the Internet Protocol Suite) is what the internet actually runs on. It simplifies the OSI model into 4 practical layers:

┌─────────────────────────────────────────────────────────────────┐
│  Layer 4 │ APPLICATION    │ HTTP, HTTPS, FTP, SMTP, DNS, SSH   │
│          │                │ (Combines OSI layers 5, 6, 7)      │
├──────────┼────────────────┼─────────────────────────────────────┤
│  Layer 3 │ TRANSPORT      │ TCP, UDP, QUIC                     │
│          │                │ (Same as OSI layer 4)               │
├──────────┼────────────────┼─────────────────────────────────────┤
│  Layer 2 │ INTERNET       │ IP, ICMP, ARP                      │
│          │                │ (Same as OSI layer 3)               │
├──────────┼────────────────┼─────────────────────────────────────┤
│  Layer 1 │ NETWORK ACCESS │ Ethernet, Wi-Fi, Fiber             │
│          │ (Link)         │ (Combines OSI layers 1 & 2)        │
└──────────┴────────────────┴─────────────────────────────────────┘

4. OSI vs TCP/IP — Side by Side

        OSI Model                    TCP/IP Model
    ┌───────────────┐
    │  Application  │
    ├───────────────┤           ┌───────────────────┐
    │ Presentation  │ ────────▶│    Application     │
    ├───────────────┤           └───────────────────┘
    │   Session     │
    ├───────────────┤           ┌───────────────────┐
    │  Transport    │ ────────▶│    Transport       │
    ├───────────────┤           └───────────────────┘
    │   Network     │           ┌───────────────────┐
    │               │ ────────▶│    Internet        │
    ├───────────────┤           └───────────────────┘
    │  Data Link    │           ┌───────────────────┐
    ├───────────────┤ ────────▶│  Network Access    │
    │  Physical     │           └───────────────────┘
    └───────────────┘

OSI = Theoretical reference model (good for learning)
TCP/IP = Practical model (what actually runs the internet)

5. How Data Flows Through the Layers (Encapsulation)

When you send data (e.g., visiting a website), each layer adds its own header — like putting a letter inside envelopes inside envelopes:

Sending Data (Encapsulation — Top to Bottom)

Step 1:  Application Layer creates the DATA
         ┌──────────────────────────┐
         │         DATA             │
         └──────────────────────────┘

Step 2:  Transport Layer adds TCP/UDP HEADER
         ┌────────┬─────────────────┐
         │TCP HDR │     DATA        │
         └────────┴─────────────────┘
              = SEGMENT

Step 3:  Network Layer adds IP HEADER
         ┌────────┬────────┬────────┐
         │ IP HDR │TCP HDR │  DATA  │
         └────────┴────────┴────────┘
              = PACKET

Step 4:  Data Link Layer adds FRAME HEADER + TRAILER
         ┌─────────┬────────┬────────┬────────┬─────────┐
         │FRAME HDR│ IP HDR │TCP HDR │  DATA  │FRAME TRL│
         └─────────┴────────┴────────┴────────┴─────────┘
              = FRAME

Step 5:  Physical Layer converts to BITS
         01001010110101010010101011010110...

Receiving Data (Decapsulation — Bottom to Top)

The process reverses. Each layer strips its header and passes the payload up to the next layer until the application gets the original data.


6. TCP vs UDP — The Two Transport Protocols

TCP (Transmission Control Protocol)

┌─────────────────────────────────────────────────────────────┐
│  TCP = "Certified Mail"                                     │
│                                                             │
│  • Connection-oriented (3-way handshake before sending)     │
│  • Guarantees delivery (retransmits lost packets)           │
│  • Guarantees ORDER (packets reassembled correctly)         │
│  • Error checking (checksums)                               │
│  • Flow control (won't overwhelm the receiver)              │
│  • Slower but RELIABLE                                      │
│                                                             │
│  Used for: Web browsing (HTTP/HTTPS), Email (SMTP),         │
│            File transfer (FTP), SSH                         │
└─────────────────────────────────────────────────────────────┘

The TCP 3-Way Handshake

Before any data is sent, TCP establishes a connection:

    Client                          Server
      │                               │
      │──── SYN ─────────────────────▶│  "Hey, can we talk?"
      │                               │
      │◀──── SYN-ACK ────────────────│  "Sure, I'm ready too"
      │                               │
      │──── ACK ─────────────────────▶│  "Great, let's go!"
      │                               │
      │◀═══════ DATA TRANSFER ═══════▶│  Connection established
      │                               │

UDP (User Datagram Protocol)

┌─────────────────────────────────────────────────────────────┐
│  UDP = "Throwing a postcard in the mailbox"                 │
│                                                             │
│  • Connectionless (no handshake — just send)                │
│  • NO delivery guarantee (packets may be lost)              │
│  • NO order guarantee (packets may arrive shuffled)         │
│  • Minimal overhead                                         │
│  • FAST but UNRELIABLE                                      │
│                                                             │
│  Used for: Video streaming, online gaming, DNS lookups,     │
│            VoIP (voice calls), live broadcasts              │
└─────────────────────────────────────────────────────────────┘

When to Use What?

┌────────────────────────┬──────────────┬──────────────┐
│ Scenario               │ TCP          │ UDP          │
├────────────────────────┼──────────────┼──────────────┤
│ Loading a webpage      │ ✅           │              │
│ Sending an email       │ ✅           │              │
│ Downloading a file     │ ✅           │              │
│ Live video call        │              │ ✅           │
│ Online gaming          │              │ ✅           │
│ DNS lookup             │              │ ✅           │
│ Live streaming         │              │ ✅           │
│ IoT sensor data        │              │ ✅           │
└────────────────────────┴──────────────┴──────────────┘

Rule of thumb:
  Need accuracy? → TCP
  Need speed?    → UDP

7. Ports — The Apartment Numbers

If an IP address is a building address, a port is the apartment number. Ports tell the computer which application should receive the data.

Total ports: 65,535 (numbered 0–65535)

┌─────────────────────────────────────────────────────────┐
│  Port Range    │ Type              │ Examples            │
├────────────────┼───────────────────┼─────────────────────┤
│ 0 – 1023       │ Well-known Ports  │ Reserved for        │
│                │ (System Ports)    │ standard services   │
├────────────────┼───────────────────┼─────────────────────┤
│ 1024 – 49151   │ Registered Ports  │ Used by software    │
│                │                   │ vendors             │
├────────────────┼───────────────────┼─────────────────────┤
│ 49152 – 65535  │ Dynamic/Private   │ Temporary ports     │
│                │ (Ephemeral)       │ assigned by OS      │
└────────────────┴───────────────────┴─────────────────────┘

Common Well-Known Ports:
  Port 20/21  ── FTP (File Transfer)
  Port 22     ── SSH (Secure Shell)
  Port 25     ── SMTP (Email sending)
  Port 53     ── DNS (Domain Name System)
  Port 80     ── HTTP (Web — unencrypted)
  Port 443    ── HTTPS (Web — encrypted)
  Port 3000   ── Dev servers (React, Express — by convention)
  Port 3306   ── MySQL database
  Port 5432   ── PostgreSQL database
  Port 8080   ── Alternative HTTP
  Port 27017  ── MongoDB

How IP + Port Work Together

Full address:  192.168.1.100:443
               ─────────────  ───
               IP Address     Port

This means: "Connect to device 192.168.1.100,
             application listening on port 443 (HTTPS)"

8. The Client-Server Model

Most of the internet uses the client-server model:

┌────────────┐                         ┌────────────┐
│            │   1. REQUEST            │            │
│   CLIENT   │ ───────────────────────▶│   SERVER   │
│            │                         │            │
│ (browser,  │   2. RESPONSE           │ (web       │
│  mobile    │ ◀───────────────────────│  server,   │
│  app)      │                         │  API)      │
│            │                         │            │
└────────────┘                         └────────────┘

Key points:
• Client INITIATES the request
• Server WAITS and RESPONDS
• Server can handle MANY clients at once
• Server has a fixed, known address (IP/domain)
• Client's address can change (dynamic IP)

Real Example: Loading google.com

1. Your browser (CLIENT) sends:
   GET / HTTP/1.1
   Host: www.google.com

2. Google's server (SERVER) responds:
   HTTP/1.1 200 OK
   Content-Type: text/html
   [... HTML of the Google homepage ...]

9. Peer-to-Peer Model

In contrast to client-server, P2P networks have no central server. Every node is both a client AND a server:

┌──────┐         ┌──────┐
│Peer A│◀───────▶│Peer B│
│      │         │      │
└──┬───┘         └───┬──┘
   │                 │
   │    ┌──────┐     │
   └───▶│Peer C│◀────┘
        │      │
        └──────┘

Examples:
• BitTorrent (file sharing)
• Bitcoin/Ethereum (blockchain)
• WebRTC (video calls)

10. Key Takeaways

  1. Protocols are agreed-upon rules — without them, computers can't communicate.
  2. The OSI model (7 layers) is the theory; the TCP/IP model (4 layers) is the practice.
  3. Encapsulation wraps data in headers at each layer; decapsulation unwraps them.
  4. TCP = reliable & ordered (for web, email, files); UDP = fast & lightweight (for video, gaming, DNS).
  5. Ports identify which application on a device should receive the data.
  6. Most web apps use the client-server model; blockchains and torrents use P2P.

Explain-It Challenge

Can you explain what happens — layer by layer — when your browser loads a webpage? Bonus: explain why YouTube uses UDP for video but TCP for the chat.


Previous → 1.1.a — History of the Web Next → 1.1.c — How Data Travels Worldwide