1, the introduction

The whole summer vacation go to the interview, interview a lot of companies, whether small factory or a company) asked about the depth of the different network theory is the most easy to interview questions, though we seldom go to practice it in the project, but understand its principle, makes us behind if the network communication works, can show you in front of the interviewer is the basis of a solid, We can also have more understanding of this part of the study in the future.

Many students will memorize this part when preparing for the interview, which is really difficult to master. Personally, I concluded that the best way to learn network principles is not to memorize them deliberately, but to fully understand the whole principle in combination with reality. It may be hard to learn at first, but use a little attention, read it a few times, ask yourself why, and think of yourself as a developer of network principles. It’s enough to prepare for the interview with clear logic rather than memorizing this part.

And there are many ways interviewers can ask the same questions, but many of them are just the same. One of the things I remember most is typing in the URL, going to the page and rendering it, what happens? The background of this interview question involves a lot of knowledge about network principles. We will not share all of them in this article. Instead, we will first make clear the origin and network hierarchy division to complete the purpose of this article.

(This article is simultaneously published at: www.52im.net/thread-2851…)

Related articles:

“Network programming lazy introduction (A) : Fast understanding of network communication protocol (PART 1)” (* recommended)

“Network programming lazy introduction (2) : Fast understanding of network communication protocol (Part 2)” (* recommended)

“Network programming lazy introduction (six) : The history of the most popular hub, switch, Router function principle introduction”

Web Programming for lazy People (9) : Why use MAC Addresses when you have IP Addresses?

Learning and communication:

– Im/Push technology development communication group 5:215477170[Recommended]

– Mobile IM Development primer article: Just one Primer: Developing Mobile IM from Scratch

2. About the author

Deer (Front-end Engineer) :

Wechat official account: Deer animation programming

Github address: github.com/luxiangqian…

Personal blog: Luxiangqiang.com/

3. Series of articles

This is the seventh article in a series that Outlines the following:

Learn TCP three handshakes and four waves with Animation

Introduction to Brain-dead Network Programming (II) : What are We Reading and writing when We read and write sockets?

Introduction to Brain-dead Network Programming (3) : Some must-know HTTP protocols

Introduction to Web Programming (4) : A Quick Understanding of HTTP/2 Server Push

Introduction to Brain-dead Network Programming (5) : The Ping command you Use every Day, What is it?

Introduction to Network Programming (6) : What is public IP and internal IP? What is NAT?

Introduction to Brain-dead Network Programming (7) : The Most Popular Computer Network layering in history

4. Why network hierarchy division?

Speaking of the network level division is not strange, I just touched the network level when a face meng force, so many layers, a layer is not on the line? There are many protocols between layers, and there are various packets. I gave up the first time.

When I picked up the network layer again, I was determined to understand it from its roots. First understand its principle, it must know its origin, that is, why to carry out network hierarchy division? That’s a good question.

If “the deer” is a web developer, at first thought of communication between the computer and the computer can complete the communication, only need a thread to the right, but the world so big, so many computer, distance and far, not only waste line, haven’t appear all sorts of line secretly cut by you, there is no doubt that communication between the computer wouldn’t work. (Wireless network came out later. Although the connection between gateway and route was also needed, each computer was not connected in pairs, but connected to each other in a unit of area.)

No, the boss said, “the deer” you give me a way to change, change not to come out can’t sleep tonight, “the fawn carefully wanted to think,” this is a technical process, the need for comprehensive improvement, also found that the so-called computer connection can only be transmitted between 0 and 1 signal, another computer do not know what so many 0, 1, on behalf of, And “deer” also found that the production of computers from different manufacturers since there is a connection to realize communication is also very troublesome, simply define a set of rules, no matter “master” computer or “want” computer, must obey this set of rules, in fact, this set of rules is what we often say “network protocol”.

The origin of the network layer, how to talk about the network protocol. Let’s continue, through the above question, the problem of sending 0 and 1 signals between computers through the wire although the rules of communication, but in addition to such meaningless signals like 0 and 1, there are other kinds of problems in the network, how to identify two computers? And how to find out their address? And how different computer applications know they are passing data to themselves, and how different communication data formats are specified.

Deer found that if all the questions were written down in a set of protocols that set the rules for communication between the two parties, but what? If some rules have communication problems and affect other rules, the most common one is packet. If a packet contains various protocols, it will be chaotic.

“Deer” in order to be able to design it better, decided to adopt a hierarchical structure, which can not only define the functions of different layers, but also realize the changes between layers without affecting each other, which is the benefits we often hear of network hierarchical division.

5. How is network stratification?

Now that we’ve decided to layer, how many layers should we do?

The original network layer was the standard seven layer, or OSI seven layer model.

▲ OSI reference model or seven-layer model

We also know the TCP/IP four-tier model and the TCP/IP five-tier model. In fact, the so-called TCP/IP four layer model and TCP/IP five layer model is based on the OSI seven layer optimization, merging some layers, in fact, is still essentially the same, but my personal favorite to use five layer to explain.

▲ Five layer model

6. What is the role of each layer?

This part involves many protocols and knowledge points of each layer, but we do not share them in detail in this section. Why? We must before concrete deep brain has a specific network layered structure, want to know what to do each layer is first, the relationship between layer and layer, and then in the next section into how each protocol in each layer of communication, the benefits of such learning coherent, and not when I was learning the surface also do not understand, is deeply meng finally forced state.

6.1 the physical

The physical layer, as the name suggests, connects computers physically, just as we talked about the physical connections between computers. It is mainly used to transmit 0 and 1 signals, which has been analyzed above. After all, 0 and 1 signals do not have any practical significance, so we use another layer to specify what the meaning of different combinations of 0 and 1 is.

6.2 Data Link Layer

Since the physical layer of the lower layer cannot specify what meanings the signals of different combinations of 0 and 1 represent, we can specify a set of protocols in the data link layer to specifically group 0 and 1 signals and specify what meanings the different groups represent, so that both computers can identify. This protocol is called the Ethernet protocol (the Ethernet protocol details are described in the next section).

But the question is, how do we identify each other and how do we know their address when we send them to each other’s computers?

6.2.1) MAC Address:

What do we mean by MAC addresses? It is the unique identifier of a computer device on the network. It is identified as a MAC address by a hexadecimal number since the computer is produced by the manufacturer.

Now that we know the MAC address as the identifier, how do we know the MAC address of the computer we’re communicating with?

6.2.2) Broadcast:

And I’m going to talk about broadcasting in more detail in the next video, but all you need to know in this video is that broadcasting helps us know each other’s MAC addresses. So now that we know the MAC address, we can communicate? There are two situations in broadcast. One is that a computer in the same subnetwork (in the same LAN) obtains the MAC address of the other computer through ARP. Different networks (different Lans) are assigned to the gateways (routers) of the two Lans for processing. There’s a lot of detail here that I’ll focus on in the next section, but in this section you just need to know how to identify a computer and how to get a MAC address.

6.3 the network layer

Both the physical layer and the data link layer have their work to do, which is what we covered above (many of which are not covered in detail in this section). It seems to me that the upper two layers can complete normal communication, so what is the network layer doing?

The origin of the network layer is because in the data link layer, we say that the communication between two computers is divided into the same sub-network and different sub-network, then the problem comes, how to judge whether two computers are in the same sub-network (LAN)? That’s what the network layer is all about.

6.3.1) IP protocol:

The IP address that we usually use, that’s the thing at the network layer, the protocol that’s specified is the IP protocol. The IP address must also be an address. The MAC address has a unique identifier on it. To make it easier for you to understand IP address and MAC address, we can abstract the IP address into a logical address, that is, the MAC address is a physical address, that is, fixed. IP addresses are dynamically assigned, not fixed.

We use IP addresses to determine whether two computer devices are in the same subnetwork, so how does it determine that, you ask, and who assigns it IP addresses? And how to allocate some of the questions, we are not in a hurry, here just say about the general process, a detailed follow-up to write a big.

Since we use IP addresses to determine whether two computers are in the same LAN, we need to know each other’s IP addresses first. DNS resolution As you know, you can resolve a domain name to an IP address. Ok, we know the IP addresses of two computers, how to determine whether the same LAN?

6.3.2) Subnet mask:

Hey hey, is a only heard of, but do not know this what function of a noun, have no matter, wait me to chat, you understand is to do what.

Subnet masks are used to identify IP addresses in the same LAN. Okay? What information? An IP address consists of 32 binary bits, that is, four decimal bits (for example, 255.255.255.000).

Subnet mask is made up of 32 bits, but can only use 0 or 1, such as: 11111111.11111111.11111111.00000000.

What does that mean? The 1 part represents the network part and the 0 part represents the host part. How does this relate to whether two computers are on the same LAN? Yes, it does! An operation (AND operation) is performed on the IP addresses of the two computers AND their subnet masks. If the result is the same, the two computers are on the same LAN; otherwise, they are not.

How to calculate AND, the composition of IP packets AND other issues will not be described here.

6.4 the transport layer

Well, if you think computers can communicate, then “xiaolu” congratulations to you, you have basically understood the function of the above layers, but if you are playing LOL while chatting with your friends on QQ, suddenly, the chat information of your teammates in the game appears in the QQ window, huh? What happened?

In fact, the above level is not enough, the above reason is that, although two computers can communicate, but every day the computer is running a lot of programs, who knows the information you transmit belongs to which programs, can not complain about LOL chat information ran to the QQ window.

You can guess what the transport layer is used for. Yes, the transport layer’s main function is to enable “port to port” communication. Different programs running on the computer are assigned different ports, so that data can be correctly transferred to different applications.

6.4.1) UDP:

Adding port numbers also requires a set of rules, that is, UDP protocol, but UDP protocol has a disadvantage, once the communication, do not know whether the other party has received the data, we define a set of rules, so that it can confirm with the other party, then TCP appeared.

6.4.2) TCP protocol:

We usually talk about the TCP triple handshake and the four wave, yeah, that’s what’s done in the transport layer, the TCP triple handshake involves a lot of content thieves, you could write a long article on it alone, I’m not going to state it here, you know that it’s done in the transport layer and what it does, you can recognize it.

6.5 Application Layer Protocols

“Feed, what you send me is broken data, disorderly of, my TM ability parse? Can you send it to me according to my rules? “

“Okay, not next time.”

You’ve probably guessed the application layer protocol, which dictates the data format of the application. The format of email, HTTP, and FTP data that we often use is defined at the application layer.

7. What are the functional details of each layer?

The previous chapter mainly shared the basic concepts of network layering. Why network layering? How do you stratify it? What is the basic function of each layer? Moreover, the functional details of each layer, such as the composition of packets and the use of some protocols contained in each layer, are not detailed, so this section will continue to share the protocols in each layer of the network layer and so on. (PS: some explanations may be incorrect, please point out the correction)

7.1 the physical

The most involved in the physical layer is some of the underlying hardware content, there is no need to understand the content, we directly look at the data link layer.

7.2 Data Link Layer

Last time, we talked about the “Ethernet protocol” specified in the data link layer to specify the grouping form of electrical signals. What is Ethernet and what does Ethernet packet look like?

7.2.1) Ethernet Protocol:

Ethernet dictates that each group of electrical signals is a packet, and each packet can be called a “frame.” Each frame consists of a Head and Data.

So what’s in the header, you ask? What will the Data store write? Why two parts? Wouldn’t it be good to put them together?

A) Header:

Why is there a header in the data transmission? We want to know how the receiver can determine whether the data is sent to itself or not, so it only takes the header to determine.

The header of a packet usually contains some identifying information about the packet, who the sender is and who the receiver is.

The length of the header is fixed at 18 bytes, which means that some header identification information cannot be larger than 18 bytes.

B) Data:

Data, as the name implies, whatever data you want to send to the receiver is put into the packet, that is, the contents of the whole packet, such as files, strings and so on.

The minimum length of the data part is 46 bytes and the maximum length is 1500 bytes. We might think that if less than 46 bytes is fine, what about more than 1500 bytes? Very simple, we divide into two packages (split), two packages can not store into three packages…

7.2.2) Broadcast:

As mentioned above, the purpose of broadcasting is to find the MAC address of the receiving end for further data transmission. Note that broadcast is just a form of sending data, and a computer that wants to know the MAC address of another computer is going to use ARP, and ARP is going to come after IP, because it involves a little bit of IP, and it’s going to be a little bit confusing.

If you think this is a little bit confusing, let’s just remind ourselves that if we want to send data, we first need to know the MAC address of the recipient. To know the MAC address of the recipient, we need to use ARP. Let’s say we get the MAC address of the recipient through ARP.

We start sending data, encapsulating the MAC address of the sender and the MAC address of the receiver in a packet, and then the sender sends the packet to all the computers in the same sub-network (the same LAN). After all the computers receive the packet, they extract the header of the packet. If the MAC address of the encapsulated receiver is the same as the MAC address of the receiver, the packet is sent to the receiver. Otherwise, the packet is discarded. This is a broadcast process.

One of the questions left in this area from the previous article is that it’s on the same LAN, what do we do if it’s not on the same LAN? When we use wireless network, we know that each wireless LAN has a router. We first send data to the router through the above method, and then the router forwards the data to computers in other Lans.

7.3 the network layer

The most important protocol in the network layer is IP protocol. Generally, the sending end sends data to the server using two addresses, namely IP address and MAC address, to accurately send data to the other end. Stop! stop! The above mentioned clearly know the MAC address of the other party can transmit data, why now need two addresses? You tell me clearly, say not clear take off!

The above is indeed a MAC address can communicate, but the premise is to obtain the MAC address through THE ARP protocol, and ARP protocol is the use of the IP address of the receiving end to obtain the MAC address, so these two addresses are very important, then if implemented, the following will continue.

7.3.1) IP protocol:

IP packets are placed directly into the “data” section of Ethernet packets, which has the advantage that “changes at the top are completely irrelevant to the underlying structure.” And then the packet looks like this.

IP packets are also divided into Head and Data:

  • 1) Header: The header of an IP packet is 20-60 bytes, including the version and IP address.
  • 2) Data: The maximum length of data is 65515 bytes. The maximum total length of the entire IP packet is 65535 bytes. It mainly stores the specific content of IP packets.

Ethernet data is 1500 bytes long. If you put an IP packet with a length of 65535 bytes into Ethernet packet summary, will it not be broken? Are you kidding me? That’s true. Let’s split the packet into several Ethernet packets and send them separately.

7.3.2) AND operation:

The most important function of the IP protocol in the previous article is to determine whether two devices belong to the same subnet (local area network).

The two IP addresses AND subnet masks are respectively AND (1 for both digits, 0 for none) AND compared to see if the results are the same. If so, they are in the same subnetwork; otherwise, they are not.

We can know the IP address of the other party through DNS. In addition to judging whether two computers are in the same LAN, there is also a function is to obtain the MAC address of the other party through ARP protocol. Stop! Do you really want me to take it off? How many times has ARP said TN? Is it time to elaborate?

7.3.3) ARP protocol:

Prerequisite: The IP address of the peer is known and obtained through DNS resolution.

ARP sends a packet that is contained in an Ethernet packet (including the IP address of the peer whose MAC address is FF:FF:FF:FF:FF). Each host in the subnetwork receives the packet and compares the IP address with its own. If the two IP addresses are the same, each host responds by reporting its MAC address to the other host. Otherwise, the packet is discarded.

7.4 the transport layer

The transport layer mainly involves two important protocols, UDP and TCP, which are used to determine port-to-port communication as described in the previous chapter. Different programs run in computers have different port numbers.

“Port” is an integer between 0 and 65535, exactly 16 bits. Ports from 0 to 1023 are occupied by the system, so you can only select ports larger than 1023.

7.4.1) UDP:

The UDP protocol is also divided into Head and Data:

  • 1) Header: The length of the header is 8 bytes. The main storage of the sending and receiving port number;
  • 2) Data: the total length of the data part and the header part does not exceed 65535 bytes, which is just fit into an IP packet.

As mentioned earlier, packets are contained, so UDP packets are placed in the “data” part of IP packets, and IP packets are placed in the “data” part of Ethernet packets.

7.4.2) TCP protocol:

TCP and UDP are the same. The advantages and disadvantages of UDP and TCP are discussed in the previous article. TCP ensures network reliability, and TCP three-way handshake and four-way wave are discussed in this part.

TCP packets and UDP packets are embedded in the “Data” part of the IP protocol. TCP packets are not limited in length, but must be limited to ensure transmission efficiency. Generally, the length of TCP packets cannot exceed that of IP packets, ensuring that individual TCP packets are not segmented.

7.5 the application layer

The application layer is the highest layer, directly facing the user, and its packets are placed in the “data” section of the TCP packet, so the entire layer 5 packet looks like this.

The content of the above five layers is basically finished, I write layer by layer from bottom to top, this article can let you enter the basic content of the network five layer protocol.

8. Write at the end

What if you’re still a little confused after reading this article?

Continue with the following two articles, which should make your internal forces multiply:

Network Programming Lazy Introduction part 1: Quick Understanding of Network Communication Protocols part 1

“Network Programming Lazy Introduction ii: A Quick Understanding of Network Communication Protocols (Part II)”

In addition, regarding the layers and relationships of computer network protocols, we can take a look at the following two figures:

* For clear original drawings of the above two pictures, please see “Computer Network Communication Protocol Diagram (Chinese Rare Edition) [Attachment download]”.

Appendix: More information on network programming basics

TCP/IP detail – Chapter 11 UDP: User Datagram Protocol

Chapter 17: TCP: Transmission Control Protocol

Chapter 18: Establishment and Termination of TCP Connections

TCP/IP Detail – Chapter 21: TCP Timeouts and Retransmission

Once upon a Time: The TCP/IP Protocol that Changed the World

“Understanding TCP in Depth (PART 1) : Fundamentals”

In – Depth understanding of TCP protocol (part 2) : RTT, sliding Windows, congestion Handling

Classic Theory: TCP three-way handshake and Four-way Wave

Wireshark Packet Capture Analyzing TCP Three-Way Handshake and Four-Way Wave

Computer Network Communication Protocol Diagram (Chinese Collector edition)

What is the maximum size of a packet in UDP?

P2P technical details (A) : NAT details – detailed principle, P2P introduction

Peer-to-peer NAT traversal (hole drilling)

P2P technology details (3) : P2P technology STUN, TURN, ICE details

Easy to Understand: A Quick understanding of NAT Penetration in P2P Technology

“High Performance Network Programming (1) : How many Concurrent TCP connections can a Single server Have?”

“High Performance Network Programming part II: The Famous C10K Concurrent Connection Problem in the Last Decade”

High Performance Network Programming (PART 3) : The Next 10 Years, It’s Time to Consider C10M Concurrency

“High Performance Network Programming (IV) : Theoretical Exploration of High Performance Network Applications from C10K to C10M”

High Performance Network Programming part 5: Understanding the I/O Model in High Performance Network Programming

High Performance Network Programming (6) : Understanding the Threading Model in High Performance Network Programming

Java BIO and NIO are difficult to understand?

“Unknown Network Programming (1) : A Simple Analysis of the DIFFICULT Problems in TCP Protocol (part 1)”

“Unknown Network Programming (II) : A Brief Analysis of the DIFFICULT Problems in TCP Protocol (Part II)”

TIME_WAIT and CLOSE_WAIT when closing TCP connections

“Network Programming under the radar (Part 4) : A Deep Dive into TCP’s Abnormal Shutdown.”

Hidden Network Programming part 5: UDP Connectivity and Load Balancing

Unknown Network Programming (6) : Understanding UDP in Depth and Using it Well

Unknown Network Programming (7) : How to Make unreliable UDP Reliable?

Network Programming under the Radar (Part 8) : Deep Decryption of HTTP from the Data Transport Layer

Unknown Network Programming (9) : Connecting Theory with Practice, Understanding DNS in all Directions

Network Programming Lazy Introduction part 1: Quick Understanding of Network Communication Protocols part 1

“Network Programming Lazy Introduction ii: A Quick Understanding of Network Communication Protocols (Part II)”

“Network programming lazy Introduction (3) : A quick understanding of TCP protocol is enough.”

Network Programming Slacker’s Guide (part 4) : Quickly Understand the Difference between TCP and UDP

A Quick Look at why UDP Sometimes Has an Advantage over TCP

“Network programming lazy introduction (six) : The history of the most popular hub, switch, Router function principle introduction”

Web Programming Slacker’s Guide (7) : Understand HTTP in a Nutshell

“Network programming lazy introduction (8) : How to write tcp-based Socket long connection”

Web Programming for lazy People (9) : Why use MAC Addresses when you have IP Addresses?

Web Programming For lazy People (10) : Quick Reading of the QUIC Protocol in the Time it takes to pee

Technology Literacy: A New Generation of UdP-based Low Latency Network Transport Layer Protocol — QUIC In Detail

Making the Internet Faster: Sharing the technology practice of the new generation OF QUIC protocol in Tencent

Summary of Optimization Methods for Short Connections in Modern Mobile Networks: Request Speed, Weak Network Adaptation, and Security Guarantee

Talk about long connections in iOS

Mobile IM Developers must Read (1) : Easy to Understand the “weak” and “Slow” mobile Web

Mobile IM Developers must Read (ii) : Summary of the Most Complete Mobile Weak Network Optimization Methods ever

Detailed explanation of IPv6 Technology: Basic Concepts, Application Status, Technical Practice (Part I)

Detailed explanation of IPv6 Technology: Basic Concepts, Application Status, Technical Practice (Part 2)

From HTTP/0.9 to HTTP/2: Understanding the History and Design of the HTTP Protocol

Understanding the Technical Challenges of Real-time Communication by Taking The Network Access Layer Design of Online Game Server as an Example

Going High: The Basics of networking that Good Android Programmers Must Know and Know

Comprehensive Understanding of mobile DNS Domain name Hijacking and other miscellaneous diseases: Technical principles, Root causes, Solutions, etc.

Mobile DNS Optimization practice of Meitu App: HTTPS Request Time Reduced by Nearly half

UDP and TCP: An Android Programmer’s Must-know Network Communication Transport Layer Protocol

Introduction to Zero-Base Communication Technology for IM Developers (PART 1) : 100 Years of Communication Switching Technology (Part 1)

Introduction to Zero-Base Communication Technology for IM Developers (PART 2) : 100 Years of Communication Switching Technology (Part 2)

Introduction to Zero-Base Communication Technology for IM Developers (III) : The Century-old Change of Chinese Communication Mode

Introduction to Zero-Base Communication technology for IM Developers (iv) : Evolution of The Mobile Phone, The Most Comprehensive History of Mobile Terminals in History

Introduction to Zero-Base Communication Technology for IM Developers (5) : 1G to 5G, 30 Years of Mobile Communication Technology Evolution

Introduction to Zero-Base Communication Technology for IM Developers (6) : Contact Person of Mobile Terminal — “Base Station” Technology

Introduction to Zero-Base Communication Technology for IM Developers (7) : The Swift Horse of Mobile Terminal — “Electromagnetic Wave”

Introduction to Zero-Based Communication Technology for IM Developers (8) : Zero-Based, The Most Powerful “Antenna” Theory Literacy

Introduction to Zero-Base Communication Technology for IM Developers (9) : The Core Network — the Backbone of Wireless Communication Networks

Introduction to Zero-Base Communication Technology for IM Developers (10) : Zero-Base, Greatest 5G Literacy

Introduction to Zero-Base Communication technology for IM Developers (11) : Why WiFi Signal is Bad?

Introduction to Basic Communication technology for IM Developers (12) : Access to network traffic? Network Down? Get it!

Introduction to Zero-Base Communication technology for IM Developers (13) : Why Cell phone Signal is Bad?

How hard is wireless Internet access on high-speed Trains?

Introduction to Zero-Base Communication technology for IM Developers (Part 15) : Understanding Location Technology in one article

Baidu APP Mobile Terminal Network In-depth Optimization Practice Sharing (I) : DNS Optimization

Baidu APP Mobile Terminal Network In-depth Optimization Practice Sharing (II) : Network Connection Optimization chapter

Baidu APP Mobile Terminal Network In-depth Optimization Practice Sharing (III) : Mobile Terminal Weak Network Optimization Chapter

Sharing by Chen Shuo: From simple to deep, A Summary of Learning Experience of Network Programming

How Many HTTP Requests can You Make over a TCP connection?

Zhihu Technology Sharing: The Practice of High Performance Long Connection Gateway technology for Ten Million Level Concurrent In Zhihu

>> More similar articles……

(This article is simultaneously published at: www.52im.net/thread-2851…)