The five-layer model of simplified network communication

foreword

How do two computers on different sides of the sky communicate? Among thousands of computers, why can one computer accurately find another computer and send data to it?

Many people may have heard of the 5-layer model of network communication, but it may not be very clear why the five-layer model is needed, and the tasks responsible for the five-layer model may often be confused. The following is the five-layer model of network communication

insert image description here

To be honest, the specific content of the five-layer model is still extremely complicated, but in today's article, I will use the most concise model to explain how a computer finds another computer and transfers the data through the five-layer model of network communication . If you send it to another computer , you can understand it even if you haven't studied computer networks.

1. Physical layer

What is the first thing a computer needs to do to communicate with another computer? Of course, it is necessary to connect this computer with other computers, so that we can transfer data there. For example, they can be connected by media such as optical fiber, cable, twisted pair, etc., and then they can communicate.

insert image description here

In other words, the physical layer is responsible for connecting two computers, and then transmitting electrical signals such as 0,1 between the computers through high and low electrical frequencies .

2. Data Link Layer

As mentioned earlier, the physical layer is simply responsible for connecting computers and transmitting electrical signals such as 0 and 1 between computers. If the transmission of these 0, 1 combinations is irregular, the computer will not be able to interpret it. A lot of 0, 1 who knows what the hell.

insert image description here

Therefore, we need to formulate a set of rules for the transmission of 0, 1. For example, how many electrical signals form a group, how should each group of signals be marked so that the computer can understand it, and so on.

Thus, there is the Ethernet protocol.

1. Ethernet protocol

The Ethernet protocol stipulates that a group of electrical signals constitutes a data packet, and we call this data packet a frame . Each frame consists of two parts: header (Head) and data (Data) .

insert image description here

The frame size is typically 64 – 1518 bytes. If the data to be transmitted is large, it is divided into multiple frames for transmission.

For the two parts of header and data, what kind of data do they store? There is no doubt that we at least need to know who sent this frame, who sent it to and other information, right? So the header part is mainly some descriptive data , such as sender, receiver and other information. The data part is the specific content of the data packet to be transmitted to the receiver .

Everyone thinks about a question, the length of a frame is 64~1518 bytes, that is to say, the length of the frame is not fixed, so do you think the byte length of the header part is fixed? Of course it is fixed. If it is not fixed, and each frame is sent separately, how does the computer know how many bytes the header is and how many bytes the data is. So the bytes of the header section are fixed and fixed at 18 bytes .

To send the data of one computer to another computer through the physical layer and the link layer, who sent it to whom, and how to distinguish between computers, you have to give them a unique identifier, right?

Thus, the MAC address appeared.

2. MAC address

Every computer connected to the network will have a network card interface , and each network card will have a unique address, which is called a MAC address. The data transmission between computers is uniquely searched and transmitted through the MAC address.
insert image description here

The MAC address consists of 48 bytes and is uniquely identified when the network card is produced.

3. Broadcast and ARP protocol

(1). Broadcast
insert image description here

As shown in the figure, if computer A knows the MAC address of computer B, and then computer A wants to send data to computer B, although computer A knows the MAC address of computer B, how does it send data to it? Computer A is not only connected to computer B, but computer A is also connected to other computers. Although computer A knows the MAC address of computer B, computer A does not know which route computer B is distributed on. In order to solve this problem, broadcasting appears .

In the same subnet , computer A wants to send a data packet to computer B, and this data packet will contain the recipient's MAC address. When sending, computer A sends it by broadcasting . At this time, computers C and D in the same subnet will also receive this data packet, and then the computer that receives this data packet will send the MAC address of the data packet Take it out and compare it with its own MAC address. If the two are the same, accept the data packet, otherwise discard the data packet. We call this transmission method broadcasting, just like we usually call someone in the form of broadcasting in the square, if the name is you, you just ignore it, if it is not you, you treat it as if you cannot hear it.

(2). ARP protocol.

So here comes the question, how does computer A know the MAC address of computer B? At this time, it has to be solved by the guy of the ARP protocol, but the ARP protocol will involve the IP address, and we will talk about the IP address below. So let's put it aside for now, as if there is such an ARP protocol, through which we can know the MAC addresses of other computers in the subnet.

3. Network layer

We mentioned the keyword subnet above. In fact, the network we are in is composed of countless subnets. When broadcasting, only computers in the same subnet can receive it.

If there is no such division of subnets, computer A sends a data packet to computer B through broadcasting, and all other computers can also receive this data packet, and then compare and discard. There are so many other computers in the world, and every computer can receive packets from all other computers, which is terrible. That mustn't crash. Therefore, such a thing as a subnet was born .

So the question is, how do we distinguish which MAC addresses belong to the same subnet? If it is the same subnet, then we will send the data to the other party in the form of broadcast, if it is not the same subnet, we will send the data to the gateway and let the gateway forward it.

In order to solve this problem, there is the IP protocol.

1. IP protocol

The IP protocol, the address it defines, we call it an IP address. There are two versions of the IP protocol, one is IPv4 and the other is IPv6. However, most of us currently use IPv4, and we only discuss the protocol of this version of IPv4.

This IP address is composed of 32-bit binary numbers, and we generally divide it into 4-segment decimal representation, and the address range is 0.0.0.0~255.255.255.255.

Every computer that wants to be connected to the Internet will have an IP address. This IP address is divided into two parts, the former part represents the network part, and the latter part represents the host part. And the binary digits occupied by the network part and the host part are not fixed.

If the network portion of the two computers is exactly the same, we say that the two computers are on the same subnet. For example 192.168.43.1 and 192.168.43.2, assuming the network part of these two IP addresses is 24 bits and the host part is 8 bits. Well their network part is both 192.168.43, so they are on the same subnet.

But here comes the question, how do you know how many digits are occupied by the network part and how many digits are occupied by the host part? In other words, from the IP addresses of two computers alone, we cannot tell whether they are in the same subnet.

This leads to another keyword --- subnet mask. The subnet mask is also a 32-bit binary number like the IP address, but its network part is all 1, and the host part is all 0. That is to say, if the network part of the above two IP addresses is 24 bits, the host If part is 8 bits, then their subnet mask is 11111111.11111111.11111111.00000000, that is, 255.255.255.0.
insert image description here

With the subnet mask, how to judge whether the IP address is in the same subnet. Obviously, knowing the subnet mask is equivalent to knowing how many bits are in the network part and how many bits are in the host part. We only need to perform an AND operation on the IP address and its subnet mask, and then compare the respective results. If the comparison results are the same, it means the same subnet, otherwise it is not the same subnet.

For example, the subcode masks of 192.168.43.1 and 192.168.43.2 are both 255.255.255.0. By ANDing the IP and the subcode masks, it can be obtained that they are both 192.168.43.0, and then they are in the same subnet.

2. ARP protocol

With the knowledge of the above IP protocol, let's talk about the ARP protocol.

With the IP addresses and subnet masks of the two computers, we can determine whether they are in the same subnet.

If they are in the same subnet, when computer A wants to send data to computer B. We can get the MAC address of computer B through the ARP protocol.

The ARP protocol also sends a data packet to each computer in the same subnet in the form of broadcast (of course, this data packet will contain the IP address of the receiver). After receiving the data packet, the other party will compare the IP address with itself. If they are the same, they will reply their own MAC address to the other party, otherwise they will discard the data packet. In this way, computer A can know the MAC address of computer B.

insert image description here

Some people may ask, after knowing the MAC address, sending data is sent in the form of broadcast, and asking the other party's MAC address is also sent in the form of broadcast, so how do other computers know whether you want to send data or ask for the MAC address? In fact, in the data packet asking for the MAC address, a special MAC address is filled in the column of the other party's MAC address. After other computers see this special MAC address, they can know what the broadcast wants to do.

If the IPs of the two computers are not in the same subnet , at this time, we will send the data packet to the gateway, and then let the gateway let us forward and transmit

3. DNS server

Here is another question, how do we know the IP address of the other party's computer? When we want to visit a website, we can enter the IP to access, but most people enter a URL domain name, for example, to visit Baidu, enter the domain name www.baidu.com. In fact, when we enter this domain name, there will be a guy called a DNS server to help us resolve this domain name, and then return the IP corresponding to this domain name to us.

Therefore, the function of the network layer is to allow us to find out where another computer is, whether it belongs to the same subnet, etc. in the vast crowd .

4. Transport layer

Through the mutual help of the physical layer, data link layer, and network layer, we have successfully transmitted data from computer A to computer B. However, there are various application programs in computer B. How does the computer know that the data is Who is it for?

At this time, the guy **Port (Port)** will come into play, that is to say, when we transfer data from computer A to calculation table B, we have to specify a port for a specific application to accept processing .

In other words, the function of the transport layer is to establish port-to-port communication . The function of the network layer is to establish host-to-host communication.

In other words, only with IP and port can we communicate accurately. At this time, some people may say that I did not specify a port when I entered the IP address. In fact, for some transport protocols, some default ports have already been set. For example, the default port for http transmission is 80, and these port information will also be included in the data packet.

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

5. Application layer

Finally, when it comes to the application layer, the application layer is the closest to our users.

Although we have received the data from the transport layer, these data are various, including html format, mp4 format, etc. Are you sure you can understand?

Therefore, we need to specify the format rules of these data, so that we can interpret and render them after receiving them. For example, in our most common Http data packet, the file format of the data packet will be specified.

Summarize

So far, the five-layer model has been mentioned here. For some layers, it is relatively concise, so I just gave a casual overview. Because if I talk about it in detail, it will definitely be very, very long. I have tried my best to talk about it in the most concise way. If you want to learn more about it, you can buy the corresponding information on computer networks. I strongly recommend the book "Computer Networks: Top-Down". I hope my explanation can give you a general understanding of the transmission of data between computers.

Guess you like

Origin blog.csdn.net/baidu_33438652/article/details/125034767