You are A computer and your name is A

A long, long time ago, you weren’t connected to any other computer, you were alone.



Until one day, you want to communicate with another computer B, so you each open a network port, connected by a network cable.



How can you “communicate” with a network cable? I can talk to you about IO, I can talk about interrupts, I can talk about buffers, but that’s not what you should worry about when you’re studying networks.

If you’re struggling, either study how the operating system handles network IO, or how packets are converted into electrical signals by the network card and sent out, or just think of it as a computer with a little man firing a gun



Anyway, you’re connected and you can communicate.

The first layer

One day, a new partner C joined, but the smart you soon found that you can each open two network ports, with a total of three network cables, connected to each other.



As more and more people join in, you find that there are too many network ports on your body, and the network cables are too dense and chaotic. (In fact, a computer can’t have that many network ports, so this connection is only theoretically possible, so I use a red dotted line to indicate the ones that can’t be connected. That’s exactly how it works.)



So you invent an intermediate device, where you plug all the network cables into this device, and this device forwards them, and you can communicate with each other, essentially the same as before, but with fewer network ports and fewer network cables, it’s not so chaotic.



You call it a hub, it is just a brainless electrical signal to all outlets (broadcast), do not do any processing, you think it is not intelligent, so they are defined in the physical layer.



Since the packets are forwarded to all the outlets, how do the four BCDE machines know if the packets are destined for them?

First of all, you have to give a name to all the devices that are connected to the switch. You used to call it ABCD, but now you need a more specialized, globally unique name for identification, and you call this higher-end name a MAC address.

Your MAC address is AA-AA-AA-AA-AA-AA, your partner B’s MAC address is bb-bb-bb-bb-bb-bb, and so on.

In this way, when A sends A packet to B, it only needs to concatenate A data structure in the header.



After receiving the packet, USER B checks that the packet is destined for him according to the target MAC address in the header and accepts it.

After receiving the packet, other Cdes determine that the packet is not destined for them based on the target MAC address in the header and discard it.



While the hub makes the layout much cleaner, what I used to send to COMPUTER B now I send to all computers connected to the hub, which is not secure and does not save network resources.

The second floor

It would be nice to make the hub smarter and send it only to the computer to which the target MAC address is directed.



It’s just a little bit more than a hub, but it looks smart. You call it a switch. And because of that little bit of intelligence, you put it at another level, the data link layer.



As you can see in the picture above, this is how you design it.

The switch maintains a MAC address table, which records the port on which each device with a MAC address is connected.



Suppose you still want to send a packet to B, construct the following data structure out of the network port.



When reaching the switch, the switch finds that the MAC address bb-bb-bb-bb-bb-bb of destination machine B is mapped to port 1 through the MAC address table maintained by the switch. Therefore, the switch sends the data to PORT B from port 1

You give this small network of networks that are organized in this way, called Ethernet.

Of course, at the beginning, the MAC address table is empty. How do you build it up?

If the MAC address table is empty, you send B the following data



Since this packet enters the switch on port 4, the switch can now record the first data in the MAC address table:

MAC: aa – aa – aa – aa – aa – aa – aa

Port: 4

The switch sees that the destination MAC address (BB-bb-bb-bb-bb-bb) is not mapped in the address table, so it sends this packet to all ports, and therefore to all machines.

After that, only machine B receives the packet that is actually destined for it and responds. The response data enters the switch from port 1, and the switch updates the second data in the address table:

MAC: bb – bb – bb – bb – bb – bb

Port: 1.

The process is as follows



Through the continuous communication of the machines in the network, the switch finally establishes the MAC address table

As the number of machines increases, the number of switch ports becomes insufficient, but you are smart enough to realize that this problem can be easily solved by connecting multiple switches



You don’t need to design anything extra, you just need to follow the previous design and rules, according to the above wiring way can complete the interconnection of all computers, so the switch design of this rule, really clever. Think about why (for example, A sends data to F).

Note, however, that the red line above is not a record in the MAC address table, but the mapping between the four EFGH machines and this port (port 6) is recorded in the table.

Finally, the two switches record the mapping records of all machines from A to H respectively.

Switch on the left



Switch on the right



That’s fine with eight computers, or even a few hundred, so the switch design has held up well for a while.

But unfortunately, people are greedy animals, and soon, the number of computers developed to thousands, tens of thousands, hundreds of thousands.

The third layer

The switch is no longer able to record such a large mapping.

At this point, you do a bit of thinking, and you realize that the root of the problem is that the red network cable is constantly connected to the number of devices behind it, which makes the address list bigger and bigger.

Can I connect that red cable to a new device that has its own MAC address, just like a computer, and can also forward packets for me?

This device is the router, and its function is, as an independent device with a MAC address, and can do a packet forward for me, you set it at the network layer.



Note that each port on the router has its own MAC address

Ok, now in the MAC address table of the switch, you only need to add a mapping between the MAC address ABAB and its port to successfully forward the packet to the router. That’s done.

So how do you do that, send to C and D, or even send to DEFGH…. Are all the packets sent to the router first?

It’s not hard to imagine if the MAC addresses of computers C and D shared a common prefix, such as, respectively

MAC address of C: FFFF-FFFF-CCcc

MAC address of D: FFFF-FFFF-DDdd

So we could say, well, let’s set the target MAC address to ffFF-FFFF -? Anything that starts, it’s sent to the router first.

Is this feasible? The answer is no.

Let’s start with the structure of a MAC address. A MAC address is also called a physical address or a hardware address. The length of a MAC address is 48 bits


It is produced by network equipment manufacturers when burned in the NETWORK card’S EPROM (a flash memory chip, usually can be written programmatically). The first 24 bits (00-16-EA) represent the network hardware manufacturer’s number, and the last 24 bits (AE-3C-40) are assigned by the manufacturer themselves, generally indicating the series number. As long as you do not change your MAC address, the MAC address is unique in the world. Figuratively speaking, a MAC address is like the id number on an ID card. It is unique.

So if you want to say ffFF-FFFF -? Beginning, unified out from a router to a group of devices (later referred to the concept of this is actually a subnet), then you need to request a subnet under all buy a device manufacturers, or you will need to request the manufacturer is in manufacturing network equipment burn MAC address, in advance according to your planned subnet structure to set the MAC address, And the structure of the network cannot be easily changed in the future.

This is clearly unrealistic.

So you invent a new address and give each machine a 32-bit number, such as:


You think it’s a little unclear, so you divide it into four sections, with dots in the middle.


You still think it’s not clear, so you convert it to base 10.

Finally you give the address a loud name, IP address. Now every computer has its own MAC address and its own IP address, but the IP address is on the software level, can be changed at any time, MAC address generally cannot be changed.

So an IP address that can be changed at any time can be adjusted according to the network topology that you plan.



As shown above, if I wanted to send a packet to one of the ABCD devices, no matter which one it was, I could say, “Send all IP addresses starting with 192.168.0 to the router, and then forward them to it!” “, clever.

How does the router accurately forward the packet to the specified device?

Don’t rush. Let’s take our time.

We first add the IP address of each device in the above networking mode



Now the transmission between the two devices, in addition to adding the data link layer header, but also add a network layer header.

If A sends data to B, as they are directly connected to the switch, A can send the following data packets directly. In fact, the network layer does not play A role.



But if A sends data to C, A needs to forward it to the router, which then forwards it to C. Since the lowest level of transport still relies on Ethernet, packets are split in two.

The packets in the router segment are as follows:



The packet from router to C is as follows:



A->C, A->C, A->C, A->C, A->C

When A sends A packet to C, how does it know whether to forward it through A router?

Answer: Subnet

If the source and destination IP addresses reside on the same subnet, packets are directly sent through the switch.

If the source IP address and destination IP address are not on the same subnet, the IP address is assigned to the router.

Okay, so now we just have to figure out what it means to be in a subnet. and reside on the same subnet and are on different subnets

We specify these two artificially, that is, we want to express, for

192.168.0. XXX, even if in one subnet, otherwise in a different subnet.

How do you express that for a computer? So they invented the concept of a subnet mask

Suppose that the subnet mask of a machine is set to

This means that if the source IP address and the destination IP address are equal to the subnet mask, they are on the same subnet, and if they are not equal, they are on different subnets, and that’s it.

Such as



C: & =


A and B are in the same subnet, C and D are in the same subnet, but A and C are not in the same subnet, nor are THEY in the same subnet as D, and so on.



Therefore, if A sends A message to C, and the IP addresses of A and C are found to be different from the subnet mask configured by A, A thinks that C and C are not on the same subnet, and sends the packet to the router. Then, A does not care about how to forward the packet.

How does A know which device is A router?

Answer: Set the default gateway on A

In the previous step, A determines whether it should send the packet to the router based on whether it is in the same subnet as C. What is the IP address of the router?

A will send the packet to the default gateway.

For A, A can only directly send the packet to an IP address under the same subnet, so it doesn’t care whether the packet is sent to A router or A computer, as long as the device has an IP address.

Therefore, the default gateway is an IP address configured by USER A on his computer so that user A can send this IP address to machines in different subnets.



That’s all!

How does the router know where C is?

Answer: Routing table

Now that A wants to send A packet to C, it has been successfully sent to the router. The last question is, how does the router know which port of the received packet should go out from so that it can directly (or indirectly) finally reach the destination C?

When a router receives a packet that has a destination IP address, C’s IP address, and needs to be converted to which port it’s going out, it’s easy to imagine that there should be a table, like a MAC address table.

This table is called a routing table.

As for the routing table is how to come out, there are many routing algorithms, this article will not expand, because I will not ha ha ~

Unlike a MAC address table, a routing table does not have a one-to-one relationship. Let’s look at the structure of a routing table.

Destination Address Subnet mask Next-hop port 0 0 1 1

We learned a new way to express a subnet mask. Since the first number of bits indicates the network segment of the subnet, for example, ( can also be abbreviated to

Destination ADDRESS Next-hop port 0 0 to 1 1

This makes a lot of sense. The routing table says, “192.168.0. XXX” is forwarded to port 0, “192.168.1. The next hop column doesn’t have a value yet, so let’s ignore that

The subnet mask and the default gateway have been completed.

The data link layer that sends packets needs to know the MAC address, but I only know the IP address.

Answer: arp

If you (A) don’t know your partner B’s MAC address, and you only know its IP address, how can you send the packet to B accurately?

The answer is simple. At the network layer, I need to find the MAC address that corresponds to the IP address, that is, somehow find the MAC address BBBB that corresponds to

This is the ARP protocol. Computers A and B also have an ARP cache table, which records the mapping between IP and MAC addresses.



Computer A will broadcast an ARP request in order to know the MAC address of computer B ( After receiving the request, Computer B will send A response with its MAC address. In this case, A updates its OWN ARP table.

In this way, we continue to broadcast ARP requests, and eventually all computers inside the ARP cache table updated complete.

To summarize

All right, so to summarize, just a few rules so far

From the point of view of each node

Computer perspective:

First I need to know my IP address and the IP address of the other party

Determine if we are on the same subnet by the subnet mask

If you are on the same subnet, you can obtain the MAC address of the peer through ARP and throw it out directly

If they are not on the same subnet, they obtain the MAC address of the default gateway through ARP and throw it out

Switch Perspective:

The packet I receive must have a destination MAC address

Check the mapping in the MAC address table

I find it and send it from my designated port according to the mapping

If not, send it to all ports

Router perspective:

The packets I receive must have a destination IP address

Check the mapping in the routing table

If it is found, it will be sent from my specified port according to the mapping relationship (not in any subnet range, the default gateway of the router is also found).

If no, an unreachable packet is returned

If you have a keen sense of smell, you should be able to detect the following sentence:

The network layer (IP protocol) itself does not transmit packets; the actual transmission of packets is delegated to the data link layer (switch in Ethernet).

The three tables involved are

The switch has a MAC address table that maps MAC addresses to their ports

Routers have routing tables that map IP addresses (segments) to their ports

Both computers and routers have ARP cache tables to cache the mapping between IP and MAC addresses

Where did these three lists come from

The MAC address table is improved by the continuous communication between nodes in Ethernet through switches.

The routing table is gradually improved by various routing algorithms and manual configuration.

The ARP cache table is gradually improved through the arp protocol requests.

With this in mind, the process of how two nodes on the current network send packets can be completely explained.



So let’s put up the last network topology of this chapter, please prepare for battle!



Now router 1 is connected to Router 2, so its routing table has an idea of the next address, so its routing table looks something like this. If an item with a next-hop address is matched, you need to match the item again to find the port and the MAC address of the next-hop IP address.

That is to say, search and search, eventually must be able to map to a port number, and then send packets from that port number.



If A sends A packet to F, can it pass? And if it works, what’s the whole process like?



Think for a minute…

Detailed process animation description:



Detailed process text description:

1. First, A ( calculates that it and F ( are not in the same subnet by using the subnet mask (, and decides to send the IP address to the default gateway (

2. User A uses ARP to find the MAC address of the default gateway

3.A encapsulates the source MAC address (AAAA) and gateway MAC address (ABAB) in the header of the data link layer, and adds the source IP address ( and destination IP address ( Both IP addresses remain the same throughout the packet, only the MAC address changes.) Encapsulates the packet in the network layer header and sends the packet



4. After receiving the packet, switch 1 finds that the destination MAC address is ABAB and forwards the packet to router 1

5. The packet is sent to router 1 and its destination IP address is The next hop is

6. In this case, router 1 needs to do two things. The first thing is to match the routing table again and find that port 2 is matched, and then encapsulate it to the data link layer.

7. Router 2 receives the packet and finds that the destination IP address of the packet is Router 2 queries the routing table and matches port 1 to send the packet out.

8. Router 2 needs to know the MAC address of, checks its ARP cache, finds FFFF, encapsulates it in the header of the data link layer, and sends the packet from port 1.

9. Switch 3 receives the data packet and finds that the destination MAC address is FFFF. After querying its MAC address table, switch 3 sends the data packet through port 6.

10. F finally receives the packet! And realized that the destination MAC address was his, so he took the package

A more detailed and precise process:

Read this I believe you are very tired, understand the above process is basically the network layer of the following part of the main process on the basic dredge.



Each step of packet transmission will have the raw data of each layer, as well as the professional process description



You can also design your own network topology and conduct experiments to improve your understanding of the network transmission process.


At this point, through the protocol of the first three layers of physical layer, data link layer and network layer, as well as a variety of network equipment (network cable, hub, switch, router) designed according to these protocols, theoretically, as long as the IP address of the other party, it has connected two nodes in any position on the earth.



This article has been revised for many times and deleted a lot of contents that affect the main process, just to let readers grasp the real core idea of the first three layers of network transmission.

This article is reprinted from Blog Park: Sun Flash, if there is infringement, please contact delete.

For those of you who are ready to become a good programmer, if you want to improve your core programming ability (internal work) and become a good programmer with real material, you might as well start now! C/C++, the timeless programming language

Wechat official account: C language programming Learning base

Organize and share (years of learning source code, project actual combat video, project notes, basic introduction tutorial)

Welcome to change careers and learn programming partners, use more information to learn and grow faster than their own thinking oh!