~/learn

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 layerPDUTCP/IP layerExamples
7 Application / 6 Presentation / 5 SessiondataApplicationHTTP, DNS, SSH
4 TransportsegmentTransportTCP, UDP
3 NetworkpacketInternetIP, ICMP, ARP
2 Data Link / 1 Physicalframe / bitsNetwork accessEthernet, 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.1 is loopback, 0.0.0.0 means "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 HTTP 80, HTTPS 443, SSH 22, DNS 53.

TCP vs. UDP

The transport layer offers two very different services:

TCPUDP
Connectionconnection-orientedconnectionless
Reliabilityguaranteed, in orderbest-effort
Overheadhigherminimal
Typical useHTTP, SSH, file transferDNS, streaming, games, VoIP

TCP establishes a connection with the three-way handshakeSYNSYN-ACKACK — 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.com and dig +trace google.com; read /etc/resolv.conf.
  • Test reachability with ping and the path with traceroute.
  • List listening sockets with ss -tuln; capture a TCP handshake with tcpdump.

References & resources

Required

Optional / enrichment

Self-check

  1. What is the difference between a MAC address and an IP address?
  2. At which layer do switches operate, and at which do routers?
  3. What makes up a socket, and what is a port for?
  4. Describe the TCP three-way handshake. When would you choose UDP instead?
  5. What does DNS do, and what is an A record?

On this page