This article is participating in the “Network protocol must know must know” essay contest

preface

The OSI Layer 7 protocol is an international network architecture standard, which starts from the bottom layer and then follows the physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer.

The meanings are as follows:

Physical layer: the original data bit stream can be transmitted on physical media, such as optical fiber, cable, repeater, etc. It mainly defines physical device standards, such as the interface types of network cables, optical fibers, and transmission rates of various transmission media.

Data link layer: Stable data links are formed by means of verification, confirmation, and feedback retransmission. Physical links are unreliable in computer networks because of various interferences. Its specific work is to receive data from the physical layer in the form of bit stream, and encapsulated into frames, transmitted to the upper layer; Similarly, data frames from the upper layer are disassembled into bitstreams and forwarded to the physical layer.

Network layer: Routing and traffic control (IP protocol). Main functions: Selects the most appropriate path for packets (the data unit of the layer that is packaged from the data of the previous layer) to pass through the communication subnet using the routing algorithm. This layer defines IP addresses and addresses through IP addresses, hence the IP protocol.

Transport layer: Provides reliable port-to-port data transfer services (TCP/UDP protocol). When a packet is lost, the other computer tells you whether all packets have been received in their entirety. If they are missing, they tell you which packets are missing and send them again until they are all received. The main functions are as follows: Monitors the quality of data transmission services and ensures the correct transmission of packets.

Session layer: Responsible for establishing, managing, and terminating sessions and data exchange between processes. Since it is impossible to call transport layer protocol to pack and then call IP protocol to find routes every time, it is necessary to establish an automatic packet sending and receiving, automatic addressing function.

Presentation layer: responsible for data format conversion, data encryption and decryption, compression and decompression, etc., to convert the information processed by the application into a format suitable for network transmission, or to convert the data from the next layer into a format that the upper layer can process.

Application layer: Provides network services for user application processes. The application layer is the interface between computer users and various applications and the network. Its function is to directly provide services to users and complete all kinds of work that users want to complete on the network.

TCP/IP

TCP/IP, namely transmission Control Protocol/Internet Interconnection Protocol, is the most basic protocol of the Internet, the foundation of the Internet.

TCP/IP is an open network of protocols that define standards for how users connect to the Internet and how data is transferred between them.

TCP/IP four-tier model

The TCP/IP reference model has a hierarchy of four layers, each of which relies on the protocols provided by the next layer to fulfill its own requirements: the application layer, the transport layer, the network layer, and the data link layer.

The differences between TCP/IP4 layer model, 5 layer model and OSI 7 layer model:

Application layer: The application layer determines the activities of communication when providing application services to users. The TCP/IP protocol family stores various common application services. For example, FTP and DNS services are two of them.

Transport layer: Transport layer to upper application layer, providing data transfer between two computers in a network connection. There are two different protocols at the transport layer: TCP and UDP.

Network layer: The network layer handles the packets that flow over the network. A packet is the smallest unit of data transmitted over a network. This layer defines the path through which the packets are sent to each other’s computers. The role of the network layer is to select a transmission route among many options when it is transmitted to and from the other computer through multiple computers or network devices.

Link layer (also known as data link layer, network interface layer) : Handles the hardware part of the network that connects to it. Including the control operating system, hardware device drivers, network cards, optical fibers and other physical visible parts, the scope of hardware are within the role of the link layer.

Common network protocols

TCP protocol

TCP is a connection-oriented transport layer protocol. The TCP layer is an intermediate layer above the IP layer and below the application layer. Reliable, pipe-like connections are often needed between application layers of different hosts, but the IP layer does not provide such a streaming mechanism, but rather unreliable packet switching.

A TCP packet segment consists of the protocol header and data. The fixed part of the protocol header contains 20 bytes, and the options part is followed by the fixed part of the header.

When the application layer sends an 8-byte data stream to the TCP layer for internetwork transmission, TCP divides the data stream into packets of appropriate length. TCP then passes the packet to the IP layer, which carries the packet across the network to the TCP layer of the receiving entity.

TCP has the following characteristics:

Stream-based approach;

Connection-oriented;

Reliable communication mode;

When the network condition is not good, try to reduce the bandwidth cost caused by retransmission.

Communication connections are maintained for both endpoints, regardless of intermediate network segments and nodes.

To meet these features of TCP, TCP provides the following provisions:

Data fragmentation: User data is fragmented at the sender end and reassembled at the receiver end. TCP determines the fragment size and controls the fragment and reassembly.

Acknowledgement on arrival: When receiving fragment data, the receiving end sends an acknowledgement to the sending end according to the fragment data serial number.

Timeout retransmission: The sender starts the timeout timer when sending fragments. If no acknowledgement is received after the timer expires, the fragment is resended.

Sliding window: The size of the receiving buffer on each side of the TCP connection is fixed. The receiving end only allows the other end to send data that can be accepted by the receiving end buffer. TCP provides flow control based on the sliding window to prevent the buffer overflow of the fast host from the slow host.

Out-of-order processing: TCP fragments transmitted as IP datagrams may be out of order upon arrival. TCP reorders received data and sends the received data to the application layer in the correct order.

Repeat processing: TCP fragments transmitted as IP datagrams are duplicated. The TCP receiver must discard the duplicated data.

Data checksum: TCP maintains its head and data checksum, an end-to-end checksum that detects any changes in the data during transmission. If the checksum of a fragment is incorrect, TCP discards the fragment and confirms that the fragment is received. As a result, the peer end times out and resends the fragment.

TCP three-way handshake and four-way handshake are no longer discussed, you can find your own information to learn.

UDP

UDP is a connectionless protocol used to process data packets like TCP on the network. UDP has the disadvantages of not providing packet grouping, assembly, and sorting. It has the following characteristics:

Connectionless: First of all, UDP does not require a three-way handshake to establish a connection like TCP before sending data, and can start sending data. They are only porters of data packets and do not split or splice data packets.

Unicast, multicast, and broadcast functions: UDP not only supports one-to-one transmission mode, but also supports one-to-many, many-to-many, and many-to-one transmission modes, that is, UDP provides unicast, multicast, and broadcast functions.

Packet oriented: The UDP packets sent by the sender to the application are forwarded to the IP layer after the header is added. UDP does not merge or split packets from the application layer, but retains the boundaries of the packets.

Unreliability: No connection is inherently unreliable, and data is transferred as it is received, without backing up data, and without caring whether the other party has received the data correctly. Regardless of the network environment, UDP has no congestion control and will always send data at a constant speed. The header has a low overhead and is very efficient when transmitting data packets:

The UDP header contains the following data:

Two hexadecimal port numbers, source port (optional field) and destination port

The length of the entire data packet

Checksum of the entire data packet (IPv4 optional field). This field is used to find errors in header information and data

UDP header is only 8 bytes, compared to TCP at least 20 bytes, it is very efficient in transmitting data packets.

The HTTP protocol

HTTP hypertext transfer protocol is the most widely used network protocol on the Internet. HTTP is an application layer protocol. It is a request/response protocol based on TCP. After a client establishes a connection with the server, it sends a request to the server. The server responds to the request. The current HTTP protocol in use is HTTP/1.1. HTTP connections in version 1.0 USES a constant connection works, version 1.1 USES a constant connection works, persistent connections is refers to the server after sending response is still in a period of time to keep this article by the TCP transport layer protocol to build a connection, the client and server can continue to transmission HTTP messages on this connection.

Most browsers currently use HTTP1.1, which means that keep-alive connections are initiated by default, so whether a full keep-alive connection can be completed depends on the server Settings.

The HTTP message

HTTP is a request/response protocol based on TCP. The following describes the format of HTTP packets. HTTP packets are classified into request packets and response packets.

Format of the request message:

An HTTP request packet consists of the request line, header line, and entity body. The browser sends an HTTP request packet to the server. In the diagram above, SP represents whitespace and CR LF represents carriage return and line feed.

Response message format

HTTP request method and response status code

In the HTTP request message example above, we can see that the request method is GET, which means that the request reads the information marked by the URL. Besides GET, there are several other common methods.

The HTTP status code is used to tell the client what happened. The status code is returned at the beginning of each response message and returns a numeric status and a readable status. Numeric codes are easy for programs to handle errors, and reason phrases are easy for people to understand

A status code between 200 and 299 indicates success, between 300 and 399 indicates that the resource has been transferred, between 400 and 499 indicates that the client request has failed, and between 500 and 599 indicates that the server has failed.

200 OK: The client request is successful. 400 Bad Request: The client Request has syntax errors and cannot be understood by the server. 401 Unauthorized: The request is not authorized. This status code must be used with the WWW-Authenticate header field. 403 Forbidden: The server receives requests but refuses to provide services. 404 Not Found: The requested resource does Not exist, for example: an incorrect URL was entered. 500 Internal Server Error: An unexpected Error occurs on the Server. 503 Server Unavailable: The Server cannot process requests from clients. However, the Server may become normal after a period of time

conclusion

For development, the focus should be on the TCP/IP four-tier model, and then look at the protocols.