A switch learns where everyone is.
You print a page from your laptop to the printer in the next room, and a second later you send the same page on WhatsApp to a friend in Chennai. The first trip crosses one room. The second crosses half of India and a dozen machines that have never heard of you.
Who decided where each one went?
See it#
Here are five machines, A to E, joined by wires. The number on each wire is its cost: how slow or busy that link is. A packet starts at A, addressed to E. Watch what each machine knows when the packet lands on it, and what happens when one wire suddenly breaks.
No machine knew the whole road. Each one looked at the address, checked its own small table, and passed the packet one hop on. (Each table keeps the cheapest next hop: going via B to reach C costs 1 + 2 = 3, cheaper than A's own wire to C at 4.) When the B to D wire snapped, only B changed its answer, and the packet still reached E by a longer road. Nobody told A anything.
Name it#
Those five machines were routers. A different, simpler box does the same job inside one room or one office. A switch moves data between devices inside one network, and a router moves it from one network to the next, one hop at a time.
How it works#
There are two kinds of trips: inside one network, like your laptop to the printer, and between networks, like your phone to Chennai. Each has its own box and its own kind of address.
1. Inside one network, the switch reads MAC addresses#
A built-in name for every device.
Every network card in every device has a MAC address built in at the factory: its hardware name, 48 bits long, written like a1:b2:c3:d4:e5:f6. Inside one network, each packet travels inside a frame: an envelope for one hop, carrying the MAC address of who it's from and who it's for. (The Layers lesson shows how packets and frames fit inside each other.)
A switch has a row of ports, with one device cabled to each. It keeps a small table of which MAC address sits behind which port, and it fills that table by itself:
- It learns from the sender. Every frame that comes in carries its source MAC, so the switch notes "this device is on this port".
- It forwards by the receiver. To decide where a frame goes, it looks up the destination MAC in its table and sends the frame out of that one port only.
- When it doesn't know yet, it floods. A brand new switch has an empty table. If host A (a host is any device on the network) on port 3 sends a frame to host B, the switch records A on port 3, then sends the frame out of every other port, because B could be anywhere. When B replies, the switch learns B's port too, and from then on their frames go straight there.
2. Hub, switch and router are three different boxes#
Everyone, one device, another network.
These three get mixed up, so here they are side by side:
- A hub is the oldest and simplest. It reads no address at all. Whatever arrives on one port, it repeats out of every other port, so every device hears everything. You'll rarely see one today.
- A switch reads the MAC address and sends each frame only to the device it's for.
- A router reads the IP address, the number from the last lesson, and sends the packet to a different network.
The box your ISP gave you at home is usually all of these in one: a router, a small switch for the cable ports, and a Wi-Fi access point. (In the Layers lesson, you'll see why a hub is called a layer 1 device, a switch layer 2 and a router layer 3.)
3. Between networks, routers go hop by hop#
Routers know only the next hop.
A packet for Chennai can't be switched there: it has to leave your network. Every router keeps a routing table. For each destination network, the table says which neighbour to hand the packet to next, the next hop. That is all a router knows. It never knows the whole road, just the next step, and so does the next router, and the next. That is exactly what the scene showed.
When a link breaks, routers that used it update their tables, and packets take another road without the sender knowing. That's also why, back in lesson 1.1, the pieces of one message could take different roads: a table can change while they're on the way. (How routers build those tables is Phase 4, Routing.)
Every hop listed, with its time.
You can watch the hops yourself. The traceroute command (tracert on Windows) lists every router your packets pass through on the way to a site, with how long each hop took. A trip to a server in another country often shows 10 to 20 of them.
4. The default gateway, for anything not local#
How does your phone know whether to talk to the printer directly or hand the packet to a router? It checks whether the destination is inside its own network. (At home that mostly means: does the address start like mine, 192.168.1.something?) If it is, the frame goes straight through the switch. If not, the phone sends it to one fixed router, its default gateway, which at home is your Wi-Fi router, often at 192.168.1.1. Your phone never needs to know the road to Chennai, only the way to its gateway.
Routers use the same trick. A small office router with just one link to its ISP doesn't need a routing table full of the world. It needs one line, a default route: "anything I don't know, send to the ISP" (written 0.0.0.0/0). Typing that one route in by hand, a static default route, is the simplest setup: there's only one way out, so there's nothing for a routing protocol such as RIP or OSPF to choose between. (Those are in Phase 4, Routing.)
Why it matters#
Every message you send is switched inside your home or college network and routed between networks the rest of the way. When "the Wi-Fi works but the internet doesn't", the problem is usually at the gateway or beyond it, and traceroute shows which hop fails. Data centres, office networks and ISPs are built out of exactly these boxes.
In interviews these come up as:
| Interviews call it | What you saw |
|---|---|
| Hub | repeats to every port, reads no address |
| Switch | reads the destination MAC, learns ports from source MACs, floods when unsure |
| Router | reads the IP address, forwards to the next hop between networks |
| Routing table | each router's list of next hops |
| Default gateway | where a device sends anything not on its own network |
| Default route | one route for "everything else", 0.0.0.0/0 |
What to remember#
- A switch moves frames inside one network by MAC address; a router moves packets between networks by IP address.
- A switch learns from the source MAC, forwards by the destination MAC, and floods when the destination is unknown.
- A hub reads no address and repeats everything to everyone.
- Routers go hop by hop: each knows only the next hop, from its routing table.
- Anything not local goes to the default gateway; a router with one way out needs just one default route.