Handsome ground: write carefully each article!

preface

How do two computers far apart communicate? Of all the thousands of computers, how can one accurately find another and send data to it?

Most people have probably heard of the 5-layer model of network communication, but may not be quite sure why it is needed, and the tasks it is responsible for may often be confused. The following is a five-tier model of network communication

To tell the truth, the specific contents of the five layer model is very complicated, but this article today, I will use the most concise model, the five layers of communication through the network model to explain how a computer find another computer and send data to another computer, even if you have not learned the computer network, can also listen to understand.

1. The physical

What is the first thing a computer does to communicate with another computer? Of course, we need to connect this computer to other computers so that we can transmit data. For example, they can be connected by optical fiber, cable, twisted-pair and other media, and then they can communicate.

In other words, the physical layer is responsible for connecting two computers and sending electrical signals like 0,1 between them at high and low frequencies.

2. Data link layer

As I said, the physical layer is simply responsible for connecting computers and transmitting electrical signals like 0,1 between computers. If the 0s and 1s are sent randomly, the computer can’t read them. Lots of zeros and ones for god knows what.

Therefore, we need a set of rules for 0,1 transmission. For example, how many electrical signals are in a group, how should each group of signals be identified so that the computer can understand them, and so on.

Hence the Ethernet protocol.

1. Ethernet protocol

The Ethernet protocol states that a group of electrical signals constitutes a packet, which we call a frame. Each frame consists of two parts: Head and Data.

Frame sizes typically range from 64 to 1518 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 what the packet specifically wants to give to the receiver.

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.

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?

So, the MAC address appears.

The article was first published on the public account “helpless and painful code farmers”. More frequent articles are welcome to search for attention, and there have been more than 150 original articles.

2. The MAC address

Each computer connected to the network has a network interface card, and each network card has a unique address, called a MAC address. Data transmission between computers is uniquely searched and transmitted by MAC address.

A MAC address consists of 48 bytes and is uniquely identified during nic production.

3. Broadcast and ARP

(1) the radio

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 does not know which side line computer B is distributed on. In order to solve this problem, there is the appearance of broadcasting.

In the same subnet, computer A sends A packet to computer B that contains the MAC address of the recipient. When computer A sends the packet through broadcast, computers C and D in the same subnet will also receive the packet. Then the computer receiving the packet will take out the MAC address of the packet and compare it with its own MAC address. If they are the same, it will accept the packet. Otherwise, the packet is discarded. We call this a broadcast, just like we call someone in the square by broadcast, if the name is you, you take notice, if not you, you don’t hear.

(2) ARP protocol.

So how does computer A know the MAC address of computer B? That’s the ARP guy, but ARP deals with 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.

3. The 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 subnetwork, computer A sends A packet to computer B through broadcasting, 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, then, there is IP protocol.

1. The IP protocol

IP protocol, it defines the address, we call it 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-bit 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 number of bits occupied by the network part and the host part is 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 the 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 is the network part and which is the host part? 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: ———— subnet mask. The subnet mask, 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 bits, the host part is 8 bits, that their subnet mask for 11111111.11111111.11111111.00000000, namely 255.255.255.0.

So with a subnet mask, how do you determine if the IP addresses are in the same subnet? Obviously, if we know the subnet mask, we know the number of bits in the network part and the number of bits in the host part. We only need to calculate and (and) the IP address and its subnet mask, 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.

2. The ARP protocol

With that knowledge, let’s go back to ARP.

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, 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

3. 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.

Therefore, the function of the network layer is to let us find out where another computer is in the vast sea of people, whether it belongs to the same subnet and so on.

The article was first published on the public account “helpless and painful code farmers”. More frequent articles are welcome to search for attention, and there have been more than 150 original articles.

4. The transport layer

Through the mutual help of the physical, data link, and network layers, we have successfully transferred data from Computer A to computer B. However, computer B has A variety of applications in it. 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.

In other words, only with IP and port, we can communicate accurately. 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 two most common protocols at 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.

5. 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. For example, in our most common Http packet, we specify what format the packet is in.

The article was first published on the public account “helpless and painful code farmers”. More frequent articles are welcome to search for attention, and there have been more than 150 original articles.

conclusion

So much for the five-tier model. For some of the layers of the more concise, casual overview. Because if I go into a little bit more detail, it’s going to be really, really long, and I’ve done my best to make it as concise as possible. If you want to learn more about it, you can buy computer networking materials. Computer Networking: Top down is highly recommended. Hopefully this has given you a general idea of how data is transferred between computers.

In addition, I am compiling a list of computer books, just to make it more convenient for everyone to find the books they want, so far I have collected hundreds of books, and contribute them to those who need them: Computer books are expensive? The most complete compilation of computer e-books in history (updated continuously)

Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article

1, give me a thumbs up, can let more people see this article, by the way inspire me, hee hee.

2, old friends, follow my original wechat public account “Shuaidi Play Programming”, focusing on writing algorithm + basic knowledge of computer (computer network + operating system + database +Linux).

Save it so you can read it. Hit me.