The body of the
How do two computers, miles apart on the Internet, transfer data? Out of thousands of computers, how does one correctly find the other and pass data to it?
Those of you who have studied computer networks probably know that in the Internet, data transfer between computers is mainly based on various “protocols”. Today, however, I’m not going to talk about protocols in detail, but I’m going to give you a general idea of how data is transmitted between protocols through various simplifications.
model
The transmission of data in the Internet is actually divided into several layers to process data, each layer has its own specific function. For example, it is like an assembly line. Some people take charge of this part of the work, and then leave the rest of the work to another part of the people.
For the layered model of Internet data transmission, there are seven layers, five layers and four layers. For example, the seven-tier model is as follows (from top to bottom) :
- The application layer
- The presentation layer
- The session layer
- The transport layer
- The network layer
- Data link layer
-
The physical layer
In the seven layers, the lower you go, the closer you are to the bottom of the computer and the higher you go, the closer you are to the user.
But what we’re going to talk about today is a model with five layers. The layers are shown as follows:
It is equivalent to treating the application layer, presentation layer and session layer as one layer. So let’s go from the bottom up, step by step, to see exactly how to get from one computer to another.
I. Physical layer
What is the first thing a computer does to communicate with another computer? Of course, it is necessary to connect this computer to other computers, such as optical fiber, cable, twisted-pair, etc. That is, the physical layer is responsible for connecting two computers and sending electrical signals like 0,1 between them.
Data link layer
As I said, the physical layer is simply responsible for sending electrical signals like 0,1 between computers. If the 0s and 1s are delivered randomly, the computer can’t read them. Therefore, we need a set of rules for 0,1 transmission. For example, how many electrical signals into a group, how to identify each group of signals so that the computer can read ah.
The data link layer, working above the physical layer, rules the transmission of the 0s and 1s, which are then interpreted by the other side.
1. Ethernet protocol
According to the Ethernet protocol, a group of electrical signals constitute a packet, which is called “frame”. Each frame consists of two parts: Head and Data. As follows:
The maximum length of this frame is 1518 bytes and the minimum length is 64 bytes. If the data to be transmitted is very large, it is divided into multiple frames for transmission.
What kind of data do they store in the header and the data? I bet you can squint and see what data they should put in there. No doubt, we should at least know who sent this message, to whom and so on, right? So the header part is mostly descriptive data, such as sender, receiver, etc. The data part is the specific content that the packet is intended to receive.
A frame length is 64 to 1518 bytes, which means that the frame length is not fixed. Do you think the header length is fixed? Of course it is fixed. If it is not fixed and each frame is sent separately, how can the computer know how many bytes the header and data are? So the bytes in the header are fixed, and fixed at 18 bytes.
2. The MAC address
When you send data from one computer to another through the physical layer and the link layer, who sends it to whom, and how to distinguish the computers from each other, you have to give them a unique identification, right?
This is a MAC address. Every computer connected to the network has a network interface card, and every network card has an address, which is called a MAC address. Data transmission between computers is uniquely searched and transmitted by MAC address. MAC addresses are uniquely identified during nic production.
Broadcast and ARP
Suppose 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 can it send data to computer B? 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 has no way of knowing on which side computer B is distributed. In fact, Computer A broadcasts data to computer B. In the same subnet, computer A sends A packet to computer B that contains the MAC address of the recipient. In this case, computers C and D in the same subnet will also receive the packet. Then, the computer receiving the packet will extract the MAC address of the packet and compare it with its own MAC address. If they are the same, the packet will be accepted; otherwise, the packet will be discarded. We call this a broadcast, just like we call someone in the square by broadcast.
So how does computer A know the MAC address of computer B? It’s up to the ARP protocol guy to solve the problem, but THE ARP protocol involves IP addresses, which we’ll get to next. So let’s just say that there’s an ARP protocol that we can use to figure out the MAC addresses of the other computers in the subnet.
Network layer
We talked about the keyword subnet above, but in fact, the network we are in is made up of countless subnetworks. When broadcasting, only computers in the same subnet can receive it. If there is no subnet, computer A sends A packet to computer B, and all other computers can receive this packet, and then compare and discard it. There are so many other computers in the world that each one can receive data packets from all the others. That is not to collapse. Hence the creation of subnets.
So how do we tell which MAC addresses belong to the same subnet? If it is the same subnet, we send the data to each other in the form of broadcast. If it is not the same subnet, we send the data to the gateway for forwarding.
To solve this problem, we introduced a new address protocol that can help us distinguish MAC addresses in the same subnet. This is the problem that the network layer is responsible for solving.
1. The IP protocol
This protocol is the IP protocol, and the address that it defines is called an IP address. There are two versions of the IP protocol, IPv4 and IPv6. But we’re mostly using IPv4, and that’s the version of the protocol we’re talking about right now.
This IP address consists of 32 binary numbers, which are generally divided into four decimal segments, and the address range is 0.0.0.0 to 255.255.255.255
Every computer that wants to connect to the Internet has an IP address. The IP address is divided into two parts, with the front part representing the network part and the back part representing the host part. And the bits of the network and the host are not fixed.
If the network parts of two computers are identical, they are said to be in the same subnet. For example, at 192.168.43.1 and 192.168.43.2, the network part of these two IP addresses is 24 bits and the host part is 8 bits. So their network part is 192.168.43, so they are in the same subnet.
But the question is, how do you know which slots are in the network segment? That is, it is impossible to tell whether two computers are in the same subnet from their IP addresses alone.
This leads to another key word: ———— subcode mask. The submask, like the IP address, is a 32-bit binary number, except that the network part specifies all 1s and the host part specifies all 0s. That is to say, if the two IP addresses on the network is divided into 24 for main machine for 8, that their subcode is 11111111.11111111.11111111.00000000 mask, namely 255.255.255.0.
So with the subscript mask, how to determine whether the IP address is in the same subnet. Obviously, if we know the submask, we know how many bits the network part is, and how many bits the host part is. We only need to calculate and (and) between the IP address and its submask, and then compare their results. If the comparison results are the same, it means that the subnet is the same, otherwise it is not the same subnet.
For example, the submasks of 192.168.43.1 and 192.168.43.2 are 255.255.255.0. If you add the IP address to the submask, you can obtain that they are both 192.168.43.0, and they are in the same subnet.
ARP protocol
With that knowledge, let’s go back to ARP. With the IP addresses of the two computers, we can determine whether they are in the same subnet. If they are in the same subnet, computer A wants to send data to computer B. We can get the MAC address of computer B through ARP protocol. ARP broadcasts a packet (containing the IP address of the recipient) to each computer on the same subnet. After receiving the packet, the peer compares its IP address with its own. If the IP address is the same, the peer replies with its MAC address. Otherwise, the peer discards the packet. In this way, computer A knows the MAC address of computer B.
Some people may ask, after you know the MAC address, you send data in the form of broadcast, ask for the 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 packet asking for the MAC address, in the field of the MAC address of the other computer, it is filled with a special MAC address. After seeing this special MAC address, other computers can know what the broadcast is trying to do.
If the IP addresses of the two computers are not in the same subnet, we will send the packet to the gateway, and then the gateway will let us forward the packet
The DNS server
Here’s another question: How do we know the IP address of each other’s computers? This may sound like an idiotic question, thinking, of course, the computer operator is doing the typing. This is true, when we want to visit a website, we can enter IP to visit, but I believe that the vast majority of people are to enter a website domain name, such as baidu is to enter the domain name www.baidu.com. In fact, when we enter the domain name, there will be a guy called DNS server to help us resolve the domain name, and then return the corresponding IP address of the domain name to us.
Iv. Transport layer
Although we have successfully transferred data from Computer A to computer B, there are various applications in computer B. How does the computer know to whom the data is destined?
This is where the Port guy comes in. That is, when we pass data from computer A to table B, we have to specify A Port for that particular application to process. That is, the transport layer’s function is to establish port-to-port communication. The function of the network layer is to establish host-to-host communication.
That is, with IP and port, we can communicate. At this time, some people may say, I entered the IP address did not specify a port ah. In fact, for some transport protocols, there are some default ports. For example, the default HTTP transmission port is 80, which is also included in the packet.
The application layer
Finally, the application layer, the application layer is closest to our users.
Although we receive data from the transport layer, there are all kinds of data coming in, from HTML to MP4. Are you sure you can read this?
So we need to specify formatting rules for this data so that we can interpret the render once it’s received. The function of the application layer is to specify the data format of the application.
So much for the five-tier model. For some of the layers of the more concise, casual overview. If you want to know more about it, you can buy computer networks. Hopefully this has given you a general idea of how data is transferred between computers.
:
The article on https://juejin.cn/post/6844903648552550413 (read the HTTPS is how to ensure the security of data transmission)