Networking
The networking fundamentals behind sockets — the OSI and TCP/IP models, MAC/IP/port addressing, TCP vs UDP, and DNS.
Before we write network programs, we need the concepts underneath them: how machines are addressed, how data is layered as it crosses a network, and the difference between TCP and UDP. This week is the groundwork for sockets.
What is a network?
A network is a set of interconnected devices (nodes) — computers, routers, switches — that exchange data over some medium (copper, fiber, or wireless). The Internet is a global network of networks. To make devices from different vendors interoperate, networking is described as a stack of layers, each handling one concern and using the layer below it.
Two layered models
The OSI model is the conceptual 7-layer reference; the TCP/IP model is the practical 4-layer stack the Internet actually runs on:
| OSI layer | PDU | TCP/IP layer | Examples |
|---|---|---|---|
| 7 Application / 6 Presentation / 5 Session | data | Application | HTTP, DNS, SSH |
| 4 Transport | segment | Transport | TCP, UDP |
| 3 Network | packet | Internet | IP, ICMP, ARP |
| 2 Data Link / 1 Physical | frame / bits | Network access | Ethernet, Wi-Fi |
As data goes down the stack it is encapsulated — each layer wraps the one above:
[ application data ] (L7)
[ TCP header | data ] segment (L4)
[ IP header | segment ] packet (L3)
[ Eth header | packet | CRC ] frame (L2)Addressing at each level
Each layer identifies things its own way:
- MAC address (L2) — a 48-bit hardware id like
00:1A:2B:3C:4D:5E, used within one LAN. - IP address (L3) — a logical, routable address. IPv4 is 32 bits (
192.168.1.100);127.0.0.1is loopback,0.0.0.0means "any interface". Private ranges (10/8,172.16/12,192.168/16) live behind NAT; IPv6 is the 128-bit successor. - Port (L4) — a 16-bit number identifying an application. An IP plus a port is a
socket:
192.168.1.100:8080. Well-known ports include HTTP80, HTTPS443, SSH22, DNS53.
TCP vs. UDP
The transport layer offers two very different services:
| TCP | UDP | |
|---|---|---|
| Connection | connection-oriented | connectionless |
| Reliability | guaranteed, in order | best-effort |
| Overhead | higher | minimal |
| Typical use | HTTP, SSH, file transfer | DNS, streaming, games, VoIP |
TCP establishes a connection with the three-way handshake — SYN → SYN-ACK → ACK —
then guarantees ordered, reliable delivery with flow and congestion control. UDP just fires
datagrams and leaves reliability to the application.
Names: DNS
People use names, not numbers. DNS resolves a domain like github.com to an IP address
through a hierarchy (root → TLD → authoritative servers). Common record types: A (IPv4),
AAAA (IPv6), CNAME (alias), MX (mail). Inspect it with dig or nslookup.
Practice / lab
- Inspect your setup:
ip addr,ip route,ip link; find your IP, gateway, and MAC. - Resolve names with
dig google.comanddig +trace google.com; read/etc/resolv.conf. - Test reachability with
pingand the path withtraceroute. - List listening sockets with
ss -tuln; capture a TCP handshake withtcpdump.
References & resources
Required
- OSI model explained (video)
- TCP vs UDP (video)
- How DNS works (video)
- Beej's Guide to Network Programming
Optional / enrichment
- Computer Networking: A Top-Down Approach (Kurose & Ross)
- Stevens, Unix Network Programming, Vol. 1 — the Sockets API
ip(8)·ss(8)·tcpdump(8)
Self-check
- What is the difference between a MAC address and an IP address?
- At which layer do switches operate, and at which do routers?
- What makes up a socket, and what is a port for?
- Describe the TCP three-way handshake. When would you choose UDP instead?
- What does DNS do, and what is an
Arecord?