While learning about networking, many people will be confused by the difference and connection between a router and a switch, because the two devices seem to function the same. However, a router is different from a switch. Here are some of my insights that I hope will help you.
Where do they work?
According to the OSI model of network architecture, from the bottom up, routers work at Layer 3 (the network layer) and switches work at layer 2 (the link layer) (there are now more advanced Layer 3 switches, layer 4 switches, and even Layer 7 switches)
How they work
Their main tasks are as follows: Router: addressing and forwarding (by IP address) Switch: filtering and forwarding (by MAC address)
We can see that the main work of these two is to forward data, but the difference is that they rely on different addresses, which is a fundamental difference!
The router has a routing table that contains its addressing information (like a map). Once it receives a network-layer datagram, it forwards the datagram to the next station (possibly router, switch, destination host) based on the routing table and routing algorithm
The switch has a MAC table, which stores the MAC addresses of all devices connected to it. It will look up the MAC address in its own table according to the destination MAC address in the header information of the data frame received. If there is a MAC address, it will forward, if not abandon
Let’s look at an example of a network topology:
From the topology, we should know that each router that connects to the device underneath it is actually a LAN. The switch works under the router, that is, the switch works within the LAN. The switch is used for forwarding data within the LAN and the router connects the LAN to the Internet
For example: each of us is equal to the host, the router is equal to the Courier, the dormitory administrator uncle is equal to the switch, the school is a local area network (LAN) Courier address (IP) according to the school to school, the parcel according to apartment number subnet (IP) to express to the apartment dormitory administrator big ye, big ye dormitory administrator according to your name (MAC) to you
Can they have one less?
A switch works on a local area network (LAN) and forwards data based on MAC addresses. Without a router addressing the network layer, our data cannot be sent to other network terminals
The router integrates the functions of the switch, and the host and the router can also realize data forwarding. However, the disadvantages are as follows: the extensible interface is not as good as the switch
Actual network data forwarding process
Here for reference: Wechat public account: Mainong Fobing, author: Liu Xin
Through an actual network data forwarding process, we can better understand the difference between routers and switches
Suppose you use a computer to access www.baidu.com
The process is roughly as follows:
Your computer first packages an HTTP packet at the application layer, then a TCP packet at the transport layer, then an IP datagram at the network layer based on the IP found by DNS, and then an Ethernet data frame at the link layer and sends it to your switch:
Your switch receives the data frame, rewraps it, and sends it to your router:
Your router uses Network Address Translation (NAT) to translate your host IP (LAN IP) into an external IP Address. It also changes the port number to completely hide your host from the outside world, and then selects an appropriate path to forward the packets according to the routing table:
(Thanks @yC2503 for correcting me.)
In the process that follows, each node simply changes its MAC address and sends it all the way across the network to its destination
NAT:
NAT is a network concealment technology that hides internal networks by establishing IP address mapping. Its main functions are as follows:
- Improve internal network security
- Sharing network addresses reduces address consumption
NAT can be implemented in three main ways:
- Static NAT (Basic NAT) : a Basic implementation of network translation. It only translates IP addresses and establishes one-to-one MAPPING between IP addresses. It does not support port translation
- Network Address port translation (NAPT) : This mode supports port mapping and allows multiple hosts to share a public IP address
- Port Address Translation (PAT) : Changes the source Port of outgoing packets and performs Port Translation, namely, Port address Translation. Port multiplexing is adopted.