Do you really understand the TCP/IP network model of the bad street?

2022.10.08

Do you really understand the TCP/IP network model of the bad street?


What is the DNS address? Domain Name System. Because we generally locate a communication target by ip address + mac address + port number, but if you enter a www.baidu.com on the browser, what will you do? At this time, first send www.baidu.com to the DNS server, and then the DNS server will tell you the IP address corresponding to www.baidu.com. ​

1. Interview questions

Can you talk about the four-layer network model of TCP/IP and the seven-layer network model of OSI?

2. Psychological analysis of the interviewer

Why ask this?

Frankly speaking, in some large companies, computer basics must be covered, especially for engineers with a salary of less than 30k, because the salary is less than 30k, you still have to work, and you can design the architecture before it reaches the level. Haven't reached that height yet.

So as long as you work, you will inevitably have to deal with machines, networks, cpu, disks, and memory all day long. In the online system, some basic computing things, such as network, cpu, disk, and memory, are all closely related. For example, will your online system cause some problems due to network failures? What should I do if the cpu load reaches 100%? What should I do if the disk is very slow to read and write? What if the memory usage is too high?

You must at least have a set of your own computer skills to support your online system. So many people say that the basics of computers are useless, so this statement is true and false. The right thing is that if you graduate to do simple crud, you really don't need these things; the wrong thing is that if you are a senior engineer and bring a few younger brothers to work on high-concurrent and stressful online systems, the machine load is very high, It's easy to go wrong, and as a result, you don't even dare to touch the machine, or you don't know how to touch it. Isn't that embarrassing.

Therefore, computer basics, network, disk, cpu, memory, still have to be a little basic

As an interviewer in a large company, you will definitely be looking at these things

3. The beginning of the dinner - analysis of interview questions

First of all, we can talk about the four-layer model and the seven-layer model together.

(1) First of all, I ask to understand why there is an agreement

Imagine that various computer manufacturers, such as IBM, Apple, etc., all make their own protocols. As a result, Apple computers can communicate with Apple computers themselves, but they cannot communicate with IBM computers. Isn't this embarrassing. So it's not a good idea to create an internationally accepted agreement, and everyone should follow this, so that all computers can communicate with each other.

At this time, we must develop a standard network model, everyone should follow this, and everyone should abide by the unified norms. This is the so-called OSI seven-layer model, they are: application layer, presentation layer, session layer, transport layer, network layer, data link layer, physical layer. Then on this basis, the TCP/IP four-layer model is simplified, which is the data link layer, the network layer, the transport layer, and the application layer.

So what does each layer represent, I will explain to you one by one

(2) Bottom-up network layering

1) Physical layer

The physical layer, what does the physical layer do, that is, the computers need to be connected to the Internet, what do you usually do? Similar to saying, you have a computer, and now you want to connect to the Internet, how to connect? A few years ago, everyone remembered that they plugged a root cable into the computer, and then they could access the Internet, but now it is enough to connect to a wifi connection. In addition, China and the United States used to rely on submarine optical cables to connect to the Internet. So the physical layer refers to this, which is how to connect various computers to form a network. This is the meaning of the physical layer. The physical layer is responsible for transmitting circuit signals of 0 and 1. For students who have studied computers, the bottom layer of computers is 0/1, electrical signals. As shown below:

2) Data link layer

The data link layer and the physical layer are connected to each computer, and also transmit the bottom 0 and 1 circuit signals. The key is not good. You have to define which 0 and 1 are grouped together. What do these signals mean? This enables communication. So the data link layer does this, and defines how the circuit signals are grouped.

00000011 (from computer 1 to computer 2)

00101 (from computer 1 to computer 3)

0101 (triggered from computer 2, to go to computer 4)

01 (start from computer 3, go to computer 5)

Many years ago, each company defined its own way of grouping circuit signals, but then came the Ethernet protocol, Ethernet. A group of electrical signals is a data packet, called a frame, each frame is divided into two parts, the header (head) and the data (data), the header contains some descriptive things, such as sender, receiver and data types, etc.

Each computer needs to send data to another computer, a bunch of 0/1 circuit signals, encapsulated into data packets, including header and data, the header contains where to go, must be from a network card of one computer, It is sent to a network card of another computer, so the data packet sent by Ethernet must specify the mac address of the network card of the target computer.

Ethernet stipulates that each network card must contain a mac address, and the mac address is the unique identifier of the network card.

The Ethernet protocol stipulates that all devices connected to the network must have a network card. The data packet in the Ethernet protocol and the data packet transmitted at the data link layer must be transmitted from the network card of one computer to another computer. The network card, and this network card address is called the so-called mac address. When each network card leaves the factory, it has a unique mac address, a 48-bit binary, but it is generally represented by 12 hexadecimal numbers. The first 6 hexadecimal numbers are the manufacturer's number, and the last 6 hexadecimal numbers are the network card flow. No.

On windows, ipconfig /all, look at the physical address, it is the mac address, 7C-67-A2-20-AB-5C

Therefore, when transmitting data packets in Ethernet, the mac address of the receiver must be specified to transmit data.

But how are ethernet packets sent from one mac address to another mac address? This is not an accurate push. In Ethernet, if a computer sends a data packet out, it will broadcast to the network cards of all computer devices in the local area network, and then each computer will obtain the recipient's mac address from the data packet and share it with its own. Compare the mac addresses. If they are the same, it means that this is a packet sent to you.

However, the above broadcast method is only for computers in a subnet (LAN), and will broadcast, otherwise a computer cannot broadcast data packets to all other computers in the world, it is only broadcast to computers in a subnet .

As shown below:

3) Network layer

As mentioned above, the computer in the subnet sends a data packet through the Ethernet, which is broadcast to the computer in the local area network. So how do you know which computers are in a subnet? This depends on the network layer. There is a set of IP addresses here. The IP address allows us to distinguish which computers belong to a subnet.

There is an IP protocol in the network layer, and the address defined by the IP protocol is called an IP address. IP addresses have two versions, IPv4 and IPv6. Currently, IPv4 is widely used, which is composed of 32 binary digits, but is generally represented by 4 decimal digits, ranging from 0.0.0.0 to 255.255.255.255.

Each computer is assigned an IP address. The first 24 digits of the IP address (the first 3 decimal digits) represent the network, and the last 8 digits (the last 1 decimal digits) represent the host.

If several computers are on a subnet, the first three decimal numbers must be the same. For example, you usually do experiments, play with virtual machines, open several linux virtual machines on win, you will find that the ip address on win may be 192.168.0.103, and then the ip addresses of several virtual machines are 192.168 .0.182, 192.168.0.125, 192.168.0.106, something like this.

In this win machine and several virtual machines, the first three decimal numbers are 192.168.0, which means that everyone is in a subnet, and the last number is the number of different hosts in this subnet.

But in fact, the above is just an example. In fact, it is impossible to tell which machines belong to a subnet from the IP address alone, because it cannot be judged from the decimal system. It needs to be judged by the binary of the ip address, combined with a concept called subnet mask.

For example, the ip address is 192.168.56.1 and the subnet mask is 255.255.255.0. After knowing the subnet mask, if you want to determine whether two IP addresses belong to the same subnet, perform binary AND operation on the two IP addresses and your own subnet mask respectively. After the AND operation, compare the representative network. that part.

192.168.56.1 and 192.168.32.7, to determine whether it is a subnet, take the subnet mask 255.255.255.0, and do the binary AND operation with the two ip addresses

11000000.10101000.00111000.00000001

11111111.11111111.11111111.00000000

The binary of the subnet mask is: 11111111.11111111.11111111.00000000, and then it is done with the binary of the ip address, and the address of the network part is compared by binary to see if it is exactly the same.

After having the IP address of the network layer, the two computers in the subnet can finally transmit data packets for communication through broadcast + mac address judgment.

But if it is found that the computer to receive the data packet is not in the subnet, then the data packet cannot be sent by broadcasting, and the data packet needs to be sent by routing.

When I see a router, I think of a router. By the way, everyone is familiar with routers, and I usually buy them. The router is responsible for connecting multiple subnets, because for example, if you are in your own home, you are actually just one of your own subnets. If you visit a website or something, you communicate with the subnet where the website machine is located.

Each computer can have multiple network cards, not just one network card. Generally, laptops have Ethernet network cards and wifi network cards. When sending data packets, it is necessary to decide which network card to go. A router is actually a dedicated device configured with multiple network cards, which can access different networks through different network cards.

A gateway is actually a type of router. It operates at the network layer. This concept is not explained much. You can think of the ip address on the router as a gateway. Each network card on the router has a mac address and a corresponding ip address. Although the router has a mac address, it cannot be addressed by the mac address, but must be addressed by the ip address, so the router is actually a device working at the network layer.

A network switch, also a device, works at the data link layer, and a router works at the network layer.

Network switches address and transmit packets through mac addresses; routers address and transmit packets through ip addresses. The network switch is mainly used in the communication of the local area network. Generally, you set up a local area network, and the computer communication in it is to send data packets through the data link layer and broadcast through the mac address. When broadcasting, the network switch is used to broadcast the data to the device. from other machines on the LAN; routers are generally used to allow you to connect to the Internet.

LAN is the local area network, which is the local area network; WAN is the wide area network, which is the wide area network. WLAN is a wireless local area network, which is a wireless local area network, that is, wifi. In the local area network, you can directly connect to the Internet through wifi.

The router at home contains two functions of switch and routing. If it is connected to the device in the LAN, plug the cable into the LAN; if it is connected to the Internet, plug the cable into the WAN.

Here is an example for you, that is, if the communication between two local area networks is carried out through a router, how is it done?

The general process is that the router is configured with two network cards, each of which can be connected to a local area network.

The computer in LAN 1 needs to send a data packet to the computer in LAN 2, and write its own IP address and the IP address of the other party in the data packet. But the two of them are not in the same local area network, so the computer in local area network 1 first sends the data packet to the router through the switch. In this process, the mac address corresponding to the ip address of a network card of the router needs to be written to the head of the data packet. It is broadcasted through the switch, and the router compares the mac address of its own network card after receiving it, and knows that it is coming to find itself.

Then, after the router receives the data packet, it will write the mac address corresponding to the ip address of the target machine into the header in LAN 2, and then send a broadcast notification through the switch again to the computer in LAN 2.

Each machine in a local area network has its own ARP cache. This ARP is used to let each device know the correspondence between the ip address and the mac address of each device in a local area network. Generally, a certain machine sends a broadcast notification. The corresponding relationship between its own ip address and mac address, and then each machine will give him a response. By analogy, everyone broadcasts to each other like this. Don't everyone know the correspondence between IP addresses and mac addresses?

So as you can see above, the communication between machines in a subnet is to write the other party's mac address in the data packet, and then the switch broadcasts it out ok; but if it is cross-subnet communication, it is to write the other party's mac address. The ip address is broadcast to the router through the mac address, and then the router converts it to a mac address according to the ip address of another subnet, and broadcasts it through the switch of another subnet. That's what it means.

As shown in the figure:

4) Transport layer

Above we have roughly understood how to divide the subnets one by one through the ip address of the network layer, and then how to broadcast communication through the mac address within the subnet; when crossing the subnet, how to pass the ip address -> mac address -> switch- > router -> ip address -> mac address -> switch to communicate through the router.

But there is another problem here, that is, on a machine, many programs use a network card for network communication, such as browsers, QQ, and live video. These software all use a network card to send data to the outside, and then send data from the network card. Receive data, right.

Therefore, the concept of a port number is also needed, that is, you have to send data packets to a certain port of a network card of a certain machine, and then the program on that machine that listens to that port can extract the data sent to this port, knowing that it is own data. The port number is in the range of 0~65536, 0~1023 is occupied by the system, and other applications use ports above 1024 and it is ok.

Computer 1, which is listening on port 48362, sends a piece of data through the network card -> the port 20386 of the ip address of computer 2 -> a QQ on the top of computer 2, listening on port 20386 -> the network card of computer 2 receives After I got a piece of data, I found that the port they were looking for was 20386, so I went to find which buddies were listening on port 20386, and QQ was listening. I would pass the data from this network card to QQ, and know through the port, which data is for you

So in fact, you will find that the network layer is based on the ip protocol to address and communicate between hosts and hosts, and then the transport layer actually establishes a certain port of a certain host to a certain port of another host. connection and communication.

This communication is achieved through sockets. Through sockets, a series of the above mentioned above can be completed based on the tcp/ip protocol, such as ip address and mac address conversion and addressing, communication through routers, etc., and will Establish a connection from one port to another port.

Both udp and tcp are transport layer protocols. The function is to add a port number to the data packet, so that point-to-point communication can be carried out through the port number. The udp protocol is unreliable, and you will not know if it is sent to others; the tcp protocol is reliable, requiring a three-way handshake, and requires that people must reply to you when they receive data.

The tcp protocol of the transport layer only specifies a set of port-based point-to-point communication protocols, including how to establish a connection, how to send and read messages, but in fact, if you want to develop based on the tcp protocol, you generally use socket, java socket network programming, as shown below:

5) Application layer

Data can be transmitted through the tcp protocol of the transport layer, but how do people interpret the data after receiving it? For example, what do you do when you receive an email? What do you do when you receive a web page? Similar to this meaning, different application layer protocols are defined for various applications, such as emails and web pages. For this application layer, we assume that the session layer, presentation layer and application layer are integrated, and 3 layers are combined into 1 layer.

Computer 1 sends something over the tcp protocol and sends it to port 20386 of computer 2;

  • GET http://localhost:8080/ http/1.1
  • key:valuel
  • key:value

Computer 2 uses the tcp protocol to read a piece of data belonging to its own port 20386;

  • GET http://localhost:8080/ http/1.1
  • key:valuel
  • key:value

sent a response;

  • 200
  • key;value
  • key:value

It is sent out through the underlying tcp, port 30987 of computer 1, ip

Computer 1, network card, take the Ethernet protocol to receive a data packet

  • 200
  • key;value
  • key:value

For example, the most common protocol at the application layer is the http protocol for network communication.

Then we look at our network settings, which generally include ip address, subnet mask, gateway address, and DNS address. We actually know what the first three mean. IP addresses and subnet masks are used to divide subnets and determine which IP addresses are in a subnet. At the same time, your ip address and mac address are associated, which uniquely locates your network card. The gateway address, you think it is the ip address of the network card on the router, the router's network card also has a mac address, and the mac address corresponds to an ip address.

What is the DNS address? Domain Name System. Because we generally locate a communication target by ip address + mac address + port number, but if you enter a www.baidu.com on the browser, what will you do? At this time, first send www.baidu.com to the DNS server, and then the DNS server will tell you the IP address corresponding to www.baidu.com.