DEV Community

Cover image for Hubs, Switches, and Routers Explained Like You're Debugging a Real Network
Tahsin Abrar
Tahsin Abrar

Posted on

Hubs, Switches, and Routers Explained Like You're Debugging a Real Network

Have you ever connected your laptop to Wi-Fi, opened a browser, typed a URL, and wondered:

"How does my message actually reach the right computer?"

Diagram

As developers, we often work with APIs, servers, ports, IP addresses, localhost, Docker networks, cloud servers, and routers. But networking can feel confusing because many explanations jump straight into heavy terms.

So let's slow down and make it simple.

In this post, we'll understand three important network devices:

  • Hub
  • Switch
  • Router

We'll also see how data moves from one computer to another inside a local network, and how your computer decides whether to send data directly to another device or through the router.

This article is based on the provided networking class transcript about hubs, switches, routers, MAC tables, and internal communication.


First, Imagine a Small Room Full of Computers

Imagine five computers connected in the same room.

One computer wants to send a message to another computer.

For example:

Computer A wants to send data to Computer D
Enter fullscreen mode Exit fullscreen mode

That sounds simple. But the network has to answer some important questions:

Who sent the data?
Who should receive it?
Are both devices in the same network?
Should the data go to another device first?
Enter fullscreen mode Exit fullscreen mode

This is where hubs, switches, and routers come in.


The Hub: The Loud but Not-So-Smart Device

A hub is one of the simplest network devices.

You can think of it like a loudspeaker.

When one computer sends data to a hub, the hub does not check who the data is for. It simply forwards the data to every other connected port.

So if Computer A sends data through a hub, the hub sends it to:

Computer B
Computer C
Computer D
Computer E
Enter fullscreen mode Exit fullscreen mode

Even if the message was only meant for Computer D.

The other computers receive the data, check it, and then ignore it if it is not meant for them.

Why is a hub considered "not smart"?

Because a hub works at the physical layer, also called Layer 1.

It only understands electrical signals or bits.

It does not understand:

MAC addresses
IP addresses
Ports
Applications
Actual message content
Enter fullscreen mode Exit fullscreen mode

So a hub cannot make smart forwarding decisions.

It just repeats the signal.

Simple way to remember a hub

A hub says:

"I don't know who needs this, so I'll send it to everyone."

That is why hubs are rarely used in modern networks.


The Switch: A Smarter Device That Learns

A switch is much smarter than a hub.

A switch works at the data link layer, also called Layer 2.

That means a switch can understand MAC addresses.

A MAC address is like the hardware address of a network device. Every network interface has one.

For example, imagine five devices connected to a switch:

Port 1 -> Computer A
Port 2 -> Computer B
Port 3 -> Computer C
Port 4 -> Computer D
Port 5 -> Computer E
Enter fullscreen mode Exit fullscreen mode

Each computer has a MAC address:

Computer A -> MAC A
Computer B -> MAC B
Computer C -> MAC C
Computer D -> MAC D
Computer E -> MAC E
Enter fullscreen mode Exit fullscreen mode

Now Computer A wants to send data to Computer D.

The Ethernet frame may look like this in a simplified way:

Source MAC: A
Destination MAC: D
Data: Hello
Enter fullscreen mode Exit fullscreen mode

When the switch receives this frame, it checks the source MAC address.

It says:

MAC A came from Port 1
Enter fullscreen mode Exit fullscreen mode

Then it saves that information in a table.

This table is commonly called:

MAC address table
CAM table
Forwarding database
Bridge table
Enter fullscreen mode Exit fullscreen mode

Different books and vendors may use different names, but the idea is the same.

The switch keeps a record like this:

MAC Address    Port
A              1
Enter fullscreen mode Exit fullscreen mode

What Happens When the Switch Does Not Know the Destination?

Now the switch checks the destination MAC address:

Destination MAC: D
Enter fullscreen mode Exit fullscreen mode

But imagine this is the first time the switch has seen Computer D.

So the switch does not yet know which port leads to MAC D.

What does it do?

It floods the frame.

That means it sends the frame to every port except the port it came from.

So the frame goes to:

Port 2
Port 3
Port 4
Port 5
Enter fullscreen mode Exit fullscreen mode

Computer B receives it and says:

This is not for me.
Enter fullscreen mode Exit fullscreen mode

Computer C says the same.

Computer E says the same.

But Computer D checks the destination MAC and says:

This is mine.
Enter fullscreen mode Exit fullscreen mode

So Computer D accepts the frame.


How the Switch Learns Over Time

Now Computer D replies to Computer A.

The reply frame looks like this:

Source MAC: D
Destination MAC: A
Data: Hello back
Enter fullscreen mode Exit fullscreen mode

The switch receives the frame from Port 4.

It checks the source MAC address and learns:

MAC D came from Port 4
Enter fullscreen mode Exit fullscreen mode

Now the switch table becomes:

MAC Address    Port
A              1
D              4
Enter fullscreen mode Exit fullscreen mode

Next time Computer A sends data to Computer D, the switch does not need to flood the frame.

It already knows:

MAC D is on Port 4
Enter fullscreen mode Exit fullscreen mode

So it sends the frame only to Port 4.

That is why a switch is more efficient than a hub.

Simple way to remember a switch

A switch says:

"I’ll learn where devices are, then send data only where it needs to go."


Hub vs Switch in One Simple Example

Imagine a classroom.

A hub is like a student who receives a note and shouts it to the whole class.

A switch is like a helpful class monitor who learns where everyone sits and quietly gives the note to the right person.

That is the main difference.


Where Does the Router Fit In?

A router works at the network layer, also called Layer 3.

That means a router understands IP addresses.

A switch mainly cares about MAC addresses inside a local network.

A router cares about moving data between different networks.

For example:

Your laptop -> your home network
Your home network -> the internet
The internet -> another server
Enter fullscreen mode Exit fullscreen mode

The router is the device that connects your local network to other networks.

Simple way to remember a router

A router says:

"This data needs to go outside this network. I know where to send it next."


Your Home Router Is Usually More Than Just a Router

This part is important.

The device we casually call a "router" at home is usually not just a router.

It often contains multiple things inside one box:

Router
Switch
Wi-Fi access point
DHCP server
Firewall/NAT features
Enter fullscreen mode Exit fullscreen mode

So when you plug an Ethernet cable into your home router, you are usually plugging it into the switch part of the device.

When your laptop connects through Wi-Fi, it connects through the wireless access point part.

The real routing happens when data needs to leave your local network and go somewhere else, like the internet.


LAN Interface and WAN Interface

A home router usually has two sides:

LAN side

This is your local network.

Your laptop, phone, printer, TV, and other home devices live here.

Example local IP addresses:

192.168.1.10
192.168.1.11
192.168.1.12
Enter fullscreen mode Exit fullscreen mode

Your router may have a local IP like:

192.168.1.1
Enter fullscreen mode Exit fullscreen mode

This is often called the default gateway.

WAN side

This is the side connected to your internet provider.

The router gets a public or provider-side IP address on this side.

So your router sits between:

Your private home network
The outside internet
Enter fullscreen mode Exit fullscreen mode

How Does a Computer Decide Where to Send Data?

Now comes a very important question.

Suppose your computer wants to send data to another IP address.

How does it know whether the destination is inside the same local network or outside?

It uses:

Its own IP address
The destination IP address
The subnet mask
The default gateway
Enter fullscreen mode Exit fullscreen mode

Usually, your computer gets these from DHCP.

A DHCP server may give your device:

IP address:       192.168.1.10
Subnet mask:      255.255.255.0
Default gateway:  192.168.1.1
Enter fullscreen mode Exit fullscreen mode

Now your computer can make a decision.


Example 1: Sending Data Inside the Same Network

Let's say:

Computer A IP: 192.168.1.10
Computer C IP: 192.168.1.11
Subnet mask:   255.255.255.0
Enter fullscreen mode Exit fullscreen mode

Computer A wants to send data to Computer C.

It checks whether both IP addresses belong to the same network.

With this subnet mask, both addresses are part of:

192.168.1.0/24
Enter fullscreen mode Exit fullscreen mode

So Computer A understands:

Computer C is in my local network.
Enter fullscreen mode Exit fullscreen mode

That means Computer A does not need to send the data to the router for routing.

Instead, it sends an Ethernet frame directly toward Computer C's MAC address.

Simplified frame:

Source MAC: A
Destination MAC: C
Source IP: 192.168.1.10
Destination IP: 192.168.1.11
Enter fullscreen mode Exit fullscreen mode

The switch receives the frame and forwards it to Computer C.

The router part does not need to be involved.


Example 2: Sending Data Outside the Network

Now imagine Computer A wants to send data to:

8.8.8.8
Enter fullscreen mode Exit fullscreen mode

That IP is not inside the local network 192.168.1.0/24.

So Computer A understands:

This destination is outside my network.
Enter fullscreen mode Exit fullscreen mode

Now something interesting happens.

The destination IP stays as:

8.8.8.8
Enter fullscreen mode Exit fullscreen mode

But the destination MAC address becomes the MAC address of the router.

Simplified frame:

Source MAC: A
Destination MAC: Router
Source IP: 192.168.1.10
Destination IP: 8.8.8.8
Enter fullscreen mode Exit fullscreen mode

Why?

Because inside the local network, the computer can only deliver frames to local devices by MAC address.

To reach the outside world, it sends the frame to the default gateway first.

The router receives it, looks at the destination IP, and then decides where to send it next.


Important Detail: MAC Address Changes, IP Address Stays

This is one of the most useful networking ideas to understand.

When data moves across networks, the IP packet is trying to reach the final IP address.

But at each local network hop, the MAC address may change.

For example:

Your laptop -> Router
Router -> ISP device
ISP device -> next router
Next router -> destination server
Enter fullscreen mode Exit fullscreen mode

Each step may use a different source and destination MAC address.

But the destination IP is still the final server's IP.

That is why:

Switches care about MAC addresses.
Routers care about IP addresses.
Enter fullscreen mode Exit fullscreen mode

A Developer-Friendly Example

Imagine you are building a backend API.

Your laptop is running a frontend app:

http://localhost:5173
Enter fullscreen mode Exit fullscreen mode

Another computer on the same Wi-Fi is running an API server:

http://192.168.1.25:3000
Enter fullscreen mode Exit fullscreen mode

When your laptop calls that API, it checks:

Am I in the same network as 192.168.1.25?
Enter fullscreen mode Exit fullscreen mode

If yes, your laptop sends the frame toward that device's MAC address.

The switch or Wi-Fi access point handles local delivery.

The router does not need to send the traffic to the internet.

Now imagine your app calls:

https://api.github.com
Enter fullscreen mode Exit fullscreen mode

That server is outside your local network.

So your laptop sends the frame to the router's MAC address.

The router then forwards the packet toward the internet.

This is happening all the time while you code.


Quick Comparison: Hub vs Switch vs Router

Device Layer Understands Main Job
Hub Layer 1 Bits/signals Sends data to all ports
Switch Layer 2 MAC addresses Sends frames inside a local network
Router Layer 3 IP addresses Sends packets between networks

Common Terms You Should Remember

Frame

A frame belongs to the data link layer.

It contains MAC address information.

Source MAC
Destination MAC
Payload
Enter fullscreen mode Exit fullscreen mode

Packet

A packet belongs to the network layer.

It contains IP address information.

Source IP
Destination IP
Payload
Enter fullscreen mode Exit fullscreen mode

Segment

A segment usually belongs to TCP at the transport layer.

It contains port information.

Source port
Destination port
Data
Enter fullscreen mode Exit fullscreen mode

MAC address table

A switch uses this table to remember which MAC address is connected to which port.

MAC Address    Port
A              1
C              3
D              4
Enter fullscreen mode Exit fullscreen mode

Default gateway

The default gateway is usually your router's local IP address.

Your computer sends data to the default gateway when the destination is outside the local network.


A Simple Mental Model

Here is the easiest way to remember everything:

Hub:
"I will send it to everyone."

Switch:
"I will send it to the correct local device if I know where it is."

Router:
"I will send it to another network."
Enter fullscreen mode Exit fullscreen mode

That's it.

Once you understand this, many networking topics become easier:

DHCP
ARP
Subnetting
NAT
Docker networking
Kubernetes networking
Cloud VPCs
Firewalls
Load balancers
Enter fullscreen mode Exit fullscreen mode

Top comments (0)