This paper is participating in theNetwork protocols must be known and must be known”Essay campaign

C/S & B/S mechanism

Internet communication software has two modes, namely C/S architecture and B/S architecture.

C/S architecture refers to client-server, which has one Client software and one Server software respectively. For example, QQ, WeChat and Youku installed on a computer belong to network communication software of C/S architecture, and the Client software needs to be installed.

B/S architecture refers to Brewer-Server. B/S architecture is also C/S architecture in essence, and B refers to browser. In this architecture, browser is the client, for example, when you visit Baidu, Baidu is the Server, and when you visit Youku web, Youku web is the Server.

Communication between computers is fundamentally based on computer hardware, which communicates through physical media.

Introduction to Network Communication

The meaning of the existence of network is the transmission of data across the region, also known as communication; The Essence of the Internet is a set of network protocols that dictate how computers access the Internet and the standards by which they access it. For example, Chinese people and Chinese people call each other in Chinese, and Americans and Americans call each other in English.

So it can be said that network = physical link medium + Internet communication protocol.

Network communication protocol

The core of the Internet is consists of a pile of agreement, for now the world’s common language is English, if the computer to a person, the Internet protocol is the computer English, if all computers are abide by Internet protocol, so all the computer can be carried out in accordance with the unified standard to send and receive information to complete the communication. These protocols specify how computers access the Internet and the standards for accessing it.

Internet protocols are divided into OSI seven-layer protocol, TCP/IP five-layer protocol or TCP/IP four-layer protocol according to their functions.

The OSI seven-layer protocol is proposed by ISO, but it is the Internet, not the OSI protocol, that is widely used in the market at the beginning due to the market reasons and the shortcomings of the protocol itself (the protocol implementation is too complicated, etc.). So there is a phenomenon: the most widely used protocol is not the international standard OSI, but the non-international standard TCP/IP five-layer protocol. Thus TCP/IP Layer 5 became the de facto international standard.

TCP/IP Layer 5 protocol

The application layer, the presentation layer and the session layer are combined into the application layer. Next, we will introduce the functions of each layer from the perspective of TCP/IP five-layer protocol to understand the principle of Internet communication.

The user is aware of only the top layer of protocol, namely the application layer. Each layer depends on the next layer from top to bottom. Therefore, each layer has a specific protocol from the bottom layer, the physical layer, to the user, and the hardware.

The physical layer

If computers want to be happy together, they must access the Internet, that is, they must complete the networking between computers.

The main function of the physical layer is to send electrical signals. A group of data of the physical layer is called bits. Simple electrical signals have no data significance, and they must be grouped to make them have data significance.

The question is how do you group these electrical signals? This is where the data link layer comes in.

Data link layer

As the bottom layer of communication between computers is based on electrical signals, electrical signals have only two states 0 and 1, but the simple electrical signals have no meaning, they must be grouped and each group represents what meaning. Therefore, the main function of data link layer is to define the grouping mode of electrical signals.

Ethernet protocol

The grouping method of electrical signals was not unified in the past, but after the unified standard was formed, it is now the Ethernet protocol. Therefore, the Ethernet protocol specifies the grouping method of electrical signals in the data link layer.

According to the Ethernet protocol, a group of electrical signals constitute a packet, called a frame, and each data frame is divided into a head and a data.

Head – Fixed to contain 18 bytes Data-46 to 1500 bytes
Sender/source address: 6 bytes

Receiver/destination address: 6 bytes

Data type: 6 bytes
The content of an electrical packet

Each data frame contains a minimum of 64 bytes and a maximum of 1518 bytes. If the data frame exceeds the maximum limit, the data frame is sent in fragments.

The MAC address

How do the sender and receiver addresses come from in the header? According to the Ethernet protocol, devices that access the Internet must have network adapters. The addresses of the sender and receiver are the MAC addresses of the network adapters. Each network card will be engraved with the world’s unique MAC address as it enters the arena.

radio

Have the MAC address label different computer, two computers in the same network can communicate through the ARP protocol (a computer to obtain the MAC address of another computer), Ethernet protocol using the most primitive way – radio communication, as long as there are data link layer can be achieved theoretically network communication around the world, But this is very unreasonable, because computers all over the world cannot exist in the same broadcast domain. The problem with Ethernet protocol broadcasting is that when a sender sends a packet, other computers in the same network can also receive the packet. The amount of data is too large.

To solve the problem of broadcast yelling in Ethernet protocols, the network layer has a solution – to put computers into different broadcast domains.

The network layer

Through the Ethernet protocol, theory of computer can communicate with each other in the world, but the world of Internet is made up of one of the isolated small local area network (LAN), if all the Ethernet communication protocol of the radio to send information, then a computer to send packets can get all over the world, it is not only the efficiency of the problem. Therefore, a way must be found to distinguish which computers belong to the same broadcast domain. Computers in the same broadcast domain can broadcast packets of data.

The network layer introduces a new set of addresses to distinguish between different broadcast domains (subnets). This set of addresses is called network IP addresses.

TCP/IP protocol

The IP protocol defines a network address, which is called an IP address. Currently, ipv4 VERSION V4 is used, and the range is 0.0.0.0 to 255.255.255.255. An IP address is usually a four-segment decimal number, such as 112.162.78.49.

The IP protocol also defines that a group of data is called a data packet, which is divided into two parts: header and data. The header contains the source IP address and destination IP address, and the data is the data sent from the transport layer.

An IP address is divided into two parts: the network part, which identifies a LAN (subnet), and the host part, which identifies a host. An IP address segment only identifies the type of an IP address. The subnet of an IP address cannot be identified from either the network part or the host part. For example, 112.162.78.49 and 112.162.78.94 do not indicate that the two IP addresses belong to the same LAN.

A valid ipv4 address consists of the following components: IP address/subnet mask.

Subnet mask

A subnet mask is a parameter of a LAN (subnet). It is the same as an IP address. It is a 32-bit binary number with the values of 1 on the network and 0 on the host. For example, if the IP address is 172.16.10.1 and the first 24 bits of the network and the last 8 bits of the host are known, the subnet mask of the network is as follows:

11111111.11111111.11111111.00000000, written in a decimal is 255.255.255.0

The function of a subnet mask is to determine whether two IP addresses belong to the same subnet (LOCAL area network). To determine whether two IP addresses belong to the same LOCAL area network, the method is to calculate the two IP addresses and the subnet mask respectively (both digits are 1, the calculation result is 1, otherwise, the calculation result is 0). If the comparison results are the same, if they are the same, they are in the same subnet, otherwise they are not. Check whether two computers belong to the same LAN, for example:

The combination of the IP address and subnet mask computes a broadcast domain address/subnet address/LAN address
The IP address and subnet mask are converted into two - level systemThe computer1
172.1610.1.:10101100.0001000000001010.00000001.
255.255255.. 0       11111111.1111111111111111.00000000.Calculation results:172.1610.. 0:10101100.0001000000001010.00000000.The computer2
172.1610.2.:10101100.0001000000001010.00000010.
255.255255.. 0    11111111.1111111111111111.00000000.Calculation results:172.1610.. 0:10101100.0001000000001010.00000000.
Copy the code

If two computers are in the same LAN, they transmit data packets directly by broadcasting. If they are not in the same LAN, they need to output the data packets to the public network through the gateway and transmit them to another network address according to the IP protocol.

ARP protocol

Computer communication basic on roar, radio, all of the bag at the end of the upper to the etheric WangTou encapsulation, and then sent via Ethernet protocol, referring to the Ethernet protocol, we know that communication is based on the MAC broadcast mode, computer is in the contract, it is easy to obtain its own MAC, how to obtain the target host MAC, You need to use ARP.

ARP obtains the MAC address by obtaining the IP address of the target computer.

The transport layer

The IP address of the network layer can distinguish which LAN a computer belongs to, and the MAC address of the data link layer can identify a unique computer. So how to identify applications on the computer? Because the data sent by the computer may be sent by different applications, such as QQ and Youku, the identification of the application on the computer needs to rely on the port. The port is the number associated with the application program and the network card. The range of the port is 0-65535,0-1023 bit system occupied port.

The transport layer’s function is to establish port-to-port communication. There are two protocols at the transport layer: TCP and UDP.

TCP protocol

TCP, also known as the Nice guy protocol, is reliable for transmission. TCP packets have no length limit and can be infinitely long in theory. However, to ensure network efficiency, the length of TCP packets cannot exceed the length of IP packets to ensure that a single TCP packet does not need to be split.

Based on TCP, the client and server must establish a bidirectional communication channel. The client and server need to shake hands three times to establish a bidirectional connection, and wave hands four times to disconnect the connection.

UDP protocol.

UDP is unreliable for data transmission. No connection is required before data is sent, and no connection can be released after data is sent, which greatly reduces resource overhead. However, UDP does not ensure reliable data delivery.

The application layer

Users are using applications, all work in the application layer, the Internet is open, everyone can develop their own applications, data is diverse, we must stipulate the application layer data organization form, so the application layer function is to stipulate the application data format.

TCP can transfer data for a variety of programs, such as emails and files. Therefore, different protocols must be used to define the format of data such as emails, web pages, and files. The protocols of these applications constitute the application layer, including HTTP, SMTP, and FTP.

Network Communication flow

The following picture nicely summarizes the packet and unpacket process of data transmission for these layers of protocol:

For a computer to communicate on a network, each computer needs to have four elements:

  • Local IP address
  • Subnet mask
  • IP address of the gateway
  • DNS IP address

DNS Domain name Resolution

In fact, there is no such concept as www.baidu.com domain name in the Internet, but should be IP address, for example, Baidu’S IP address is 112.112.112.0, you should enter this IP address in the browser, you can also visit Baidu, but now there are numerous websites, Remembering the IP addresses of so many websites was a big challenge, so DNS came along.

DNS is not introduced in detail here. If you want to know DNS in detail, you can search for relevant knowledge by yourself, which has little impact on our normal programming.

Note that the main function of DNS is to resolve IP addresses to domain names, such as 112.112.112.0 to www.baidu.com.

Network Communication flow

We know that there are two kinds of software based on network communication: BS architecture and CS architecture. The difference is that the BS architecture has no client software, it relies on browsers (special clients).

The difference in the network communication process is that the BS architecture software has a DNS domain name resolution process in the client (browser URL input field).

Type a url into the url field of your browser and press Enter to get the page response. The following things happen:

Enter the address you want to visit in the URL box of your browser.

(1) Before sending an HTTP request, domain name resolution is required to obtain the IP address of the target server. The local DNS server is first used to check whether the query request contains the IP address. If the local DNS server cannot find the target IP address, A DNS query is made to the root DNS server for the IP address that the user needs to access.

(2) The browser establishes a TCP connection with the target server.

(3) After the connection is established, the browser will initiate an HTTP request to the target server to request data packets.

(4) If the target server is a simple page, it will return directly. For a page that needs to be redirected, the browser gets the redirect response, finds the redirect address, repeats the first step, and then the browser resends the request with the new URL and the target server returns the data.

conclusion

The article was first published on the wechat public account Program Yuan Xiaozhuang, and synchronized with nuggets and Zhihu.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)