Tommy

Tommy

写代码是热爱,但也是生活 !
github

Easy-to-understand overview of the computer five-layer model

Introduction#

Computers are our most familiar tools, and there are many computers on Earth. So how do our two computers communicate with each other? Many people would say it's because of the five-layer model of computers, but do you know their functions? Here are the five layers of the model:
Five-layer model of network communication

To be honest, this five-layer model is quite complex, but today I will explain how two computers communicate in the simplest language possible, so even if you haven't studied computer networks, you can understand.

1. Physical Layer#

To communicate between one computer and another, they must be connected.

Mermaid Loading...

In other words, the physical layer connects the two computers and then transmits electrical signals such as 0s and 1s through high and low voltage levels.

As mentioned earlier, the physical layer simply connects two computers and transmits electrical signals between them. But if these electrical signals are not standardized, who can understand them?

Mermaid Loading...

Therefore, there is a set of rules that allow computers to understand them, and thus the Ethernet protocol was born.

2.1 Ethernet Protocol#

The Ethernet protocol defines that a group of electrical signals is a data packet, and we call this data packet a 'frame'. Each frame consists of a header and data.

HeaderData

The header contains information about the sender and receiver, while the data contains the data to be sent. To send data from one computer to another through the physical and data link layers, you need to know who is sending to whom and how to distinguish between computers. So, the MAC address appeared.

2.2 MAC Address#

Every computer connected to a network has a network interface card (NIC), and each NIC has a unique address called a MAC address. The transfer of data between computers is done by uniquely identifying and transmitting through MAC addresses.

2.3 Broadcasting and ARP Protocol#

2.3.1 Broadcasting#

Let's think about another situation: Computer A is not only connected to Computer B, but also to other computers. Although Computer A knows the MAC address of Computer B, it does not know which route Computer B is on. To solve this problem, broadcasting was introduced.

Mermaid Loading...

In the same subnet, A will send a data packet to B, which will include B's MAC address. When sending, A sends it in the form of broadcast. At this time, B, C, and D will all receive this data packet. The receiving computers will extract the MAC address of the data packet and compare it with their own MAC address. If they match, they will accept the data packet; otherwise, they will discard it. This is similar to when mothers call their children home for dinner by broadcasting. If the name is yours, you pay attention; if it's not, you pretend not to hear.

2.3.2 ARP Protocol#

Now let's think about how A knows the MAC address of B. This is where the ARP protocol comes in. However, the ARP protocol involves IP addresses, which we will discuss later. So let's put it aside for now and assume that there is an ARP protocol that allows us to know the MAC addresses of other computers in the subnet.

3. Network Layer#

As mentioned above, we mentioned the term subnet. The network we are in is composed of countless subnets. Broadcasting can only be done within the same subnet.

If there were no subnets, when Computer A sends a data packet to Computer B via broadcasting, all other computers would also receive this data packet and then compare and discard it. There are so many computers in the world, and each computer can receive data packets from all other computers. That would be incredible. It would be overwhelming. Therefore, the concept of subnet was introduced.

So the question is, how do we determine which subnet a MAC address belongs to? If it is the same subnet, we use broadcasting to send data to each other. If it is not the same subnet, we send the data to the gateway for forwarding. To solve this problem, the IP protocol was introduced.

3.1 IP Protocol#

The IP protocol defines addresses, which we call IP addresses. There are two versions of the IP protocol: IPv4 and IPv6. However, most of us still use IPv4, so we will only discuss this version of the protocol for now.

Every computer connected to the network has an IP address, which is divided into two parts: the network part and the host part. The number of binary digits occupied by the network part and the host part is not fixed.

So how do we determine how many bits the network part and the host part occupy? In other words, how do we determine if two IP addresses are in the same subnet? This brings us to a keyword: subnet mask.

The subnet mask is a 32-bit binary number, just like an IP address. The network part is all 1s, and the host part is all 0s. For example, for IP addresses 192.168.43.1 and 192.168.43.2, if the network part is 24 bits and the host part is 8 bits, then their subnet masks are both 11111111.11111111.11111111.00000000, which is 255.255.255.0.

So now that we know the subnet mask, how do we determine if an IP address is in the same subnet? We just need to perform a bitwise AND operation between the IP address and its subnet mask, and then compare the results. If the results are the same, it means they are in the same subnet; otherwise, they are not.

For example, the subnet masks for 192.168.43.1 and 192.168.43.2 are both 255.255.255.0. By performing the AND operation between the IP address and the subnet mask, we get 192.168.43.0 for both. Therefore, they are in the same subnet.

3.2 ARP Protocol#

Continuing from where we left off, when Computer A wants to send data to Computer B, A does not yet know B's MAC address. At this time, A will send a data packet containing the IP address of the receiver to every computer in the subnet. When the recipient receives this data packet, it will extract the IP address and compare it with its own. If they match, the recipient will reply with its MAC address to A; otherwise, it will discard the data packet. This way, Computer A can know the MAC address of Computer B.

3.3 DNS Server#

So how do we know the IP address of the recipient? When we want to access a website, we can enter the IP address for access, but I believe that most people enter a domain name, such as www.baidu.com. When we enter this domain name, there is a guy called the DNS server who helps us resolve this domain name and returns the corresponding IP address to us.

Therefore, the function of the network layer is to allow us to find another computer in the vast sea of people, determine if it belongs to the same subnet, and so on.

4. Transport Layer#

With the help of the physical layer, data link layer, and network layer, we have successfully transmitted data from Computer A to Computer B. But Computer B has various applications, so how does the computer know who the data is for? This is where the port comes in. When we send data from Computer A to Computer B, we need to specify a port for a specific application to receive and process.

The most common protocols in the transport layer are TCP and UDP. The biggest difference between TCP and UDP is that TCP provides reliable transmission, while UDP provides unreliable transmission.

In other words, the function of the transport layer is to establish communication between ports. Compared to the network layer, which establishes communication between hosts.

5. Application Layer#

Finally, we come to the last layer, the application layer, which is the closest layer to us users.

Although we have received data from the transport layer, these data packets come in various formats, such as HTML, MP4, and so on. Are you sure you can understand them?

Therefore, we need to specify the format rules for these data packets so that they can be interpreted and rendered after receiving them. For example, in the most common HTTP data packets, the format of the file is specified.

End#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.