Layer 5 network protocol of computer network

introduction

Computer networks are an inseparable part of our daily lives, allowing us to communicate with each other, transfer data quickly, and achieve great achievements in various fields. To achieve this efficient communication, network protocols become crucial. Network protocols are the rules and conventions for communication between computers, which ensure the smooth transmission of data in the network. Among the network protocols, the 5-layer network protocol is widely used, which is an architecture that divides the entire network communication process into 5 layers. Each layer is responsible for different tasks, cooperates with each other, and finally realizes efficient data transmission. This article will give an in-depth introduction to the concept, function and role of the 5-layer network protocol, and take you to explore the mysteries of the network protocol.

1. OSI seven-layer model

OSIThe ( Open Systems Interconnection) model is a theoretical reference model of a network communication protocol , which is used to describe and plan communication functions at different levels in a computer system. It was released by the International Organization for Standardization (ISO) in 1984 and is a standard network communication architecture.

The OSI model divides network communication into seven different layers, each layer is responsible for specific functions and tasks, and communicates with the upper and lower layers through interfaces . The following is a detailed description of each level:

  1. Physical layer ( Physical Layer): The physical layer is responsible for the transmission of bit streams, that is, the transmission of voltage, current or optical signals through physical media. It defines the cable type, interface specification, transmission distance, etc.

  2. Data Link Layer ( Data Link Layer): The data link layer is responsible for dividing the bit stream provided by the physical layer into data frames and handles error detection and correction. It defines the frame format, MACaddresses, etc.

  3. Network layer ( Network Layer): The network layer is responsible for data routing and forwarding, as well as address resolution and logical addressing. It defines IPaddresses, routing protocols, etc.

  4. Transport layer ( Transport Layer): The transport layer provides end-to-end reliable transmission and data segmentation functions. It defines the transport protocol (eg TCP) and port number etc.

  5. Session layer ( Session Layer): The session layer is responsible for establishing, managing, and terminating session connections between two applications. It defines the session control mechanism and synchronization rules.

  6. Presentation layer ( Presentation Layer): The presentation layer handles format conversion, encryption, and decryption of data to ensure data compatibility between different systems. It defines data compression, encryption, and format conversion, among others.

  7. Application layer ( Application Layer): The application layer provides the interface between network services and applications to meet the specific needs of users. It defines many application layer protocols, such as HTTP、FTPand SMTPso on.

        +------------------------+
      7 |   Application Layer    |
        +------------------------+
          
        +------------------------+
      6 |   Presentation Layer   |
        +------------------------+
          
        +------------------------+
      5 |      Session Layer     |
        +------------------------+
          
        +------------------------+
      4 |    Transport Layer     |
        +------------------------+
          
        +------------------------+
      3 |      Network Layer     |
        +------------------------+
          
        +------------------------+
      2 |   Data Link Layer      |
        +------------------------+
          
        +------------------------+
      1 |    Physical Layer      |
        +------------------------+

Each layer works independently and only exchanges information with adjacent layers without involving the specific implementation details of other layers. This layered design makes the development, maintenance and upgrade of network protocols more simplified and reliable. In addition, because the functions and responsibilities of each layer are clear, different vendors can independently develop and implement specific layers, thus promoting the rapid development and interoperability of network technologies.

2. TCP/IP reference model

The TCP/IP reference model is a TCP/IPmodel that divides the protocol stack into different levels, and is used to explain and describe the functions and functions of each level in network communication. It is the basis of the Internet communication protocol family and is also a widely used network protocol.

The TCP/IP reference model is generally divided into five layers, from top to bottom are the application layer, transport layer, network layer, data link layer and physical layer . Each layer has different functions and tasks, and they work together to achieve reliable transmission of data from the sender to the receiver.

  1. Application layer : The application layer is the layer closest to the user and is responsible for handling communication between network applications. It defines a series of communication standards and protocols, such as HTTP、FTPand SMTPso on. Data at the application layer is called a packet.

  2. Transport layer : The transport layer is responsible for processing end-to-end data transmission and providing reliable data transmission services. It uses a transport layer protocol (such as TCPand UDP) to split the data into an appropriate size and transmit it between the sender and receiver.

  3. Network layer : The network layer is responsible for data communication between networks, and provides addressing and routing functions through the IP protocol. It packs data into packets for transmission over the network to its destination.

  4. Data link layer : The data link layer is responsible for transmitting data packets on the physical medium, converting data packets into bit streams, and providing error detection and correction functions.

  5. Physical layer : The physical layer defines how a stream of data bits is transmitted over a physical medium, such as via cables, optical fibers, or wireless signals.

The following figure shows the transmission process of data from the application layer to the physical layer:

------------------------------------
|      应用层       |
------------------------------------
|              发送数据              |
------------------------------------
                |
------------------------------------
|      传输层       |
------------------------------------
|          TCP/UDP协议          |
|              分割数据              |
|              添加头部              |
|              端口号              |
|              检查差错              |
------------------------------------
                |
------------------------------------
|      网络层       |
------------------------------------
|              IP协议              |
|              寻址              |
|              路由              |
|              打包成数据包              |
------------------------------------
                |
------------------------------------
|      数据链路层       |
------------------------------------
|         数据链路协议          |
|             转换为比特流             |
|      添加帧头和帧尾(开始和结束标记)       |
|            物理层传输            |
------------------------------------

3. The concept and function of network protocol

网络协议It refers to the collection of norms and rules that are followed during data communication between communication devices and computers in a computer network . It defines the format of data transmission, transmission steps and control information, etc., to ensure effective communication between various devices and applications in the computer network.

network protocolmain effecthave:

  1. Data transmission specification : The network protocol defines the format and structure of data transmission in the network, ensuring that different devices can correctly parse and process data.

  2. Network connection management : The network protocol defines how to establish, maintain and disconnect network connections, including the handshake process, flow control of data transmission, and error handling.

  3. Data security and verification : Network protocols can provide data encryption, authentication and verification mechanisms to ensure data security and integrity.

  4. Network resource management : Network protocols can define and manage the allocation and scheduling of network resources, including IP address allocation, route selection, and bandwidth control.

  5. Quality of Service Guarantee : Network protocols can define and manage priority, bandwidth allocation, and congestion control of different applications and services to provide better quality of service.

In short, the network protocol is the basis of computer network communication, which provides the specifications and rules required for effective and reliable communication between communication devices and computers. The correct implementation and application of network protocols can ensure the normal operation of network communication and secure data transmission.

The following are the protocols corresponding to each layer in the TCP/IP model:

level protocol
application layer HTTP、FTP、SMTP、Telnet、DNS
transport layer TCP、UDP
Network layer IP、ICMP、ARP
link layer Ethernet、PPP

It should be noted that the table only lists some common protocols corresponding to each layer. In fact, each layer contains more protocols.

4. Detailed introduction of each layer of TCP/IP reference model

1. Physical layer

The physical layer in the TCP/IP model is the lowest layer in the network architecture, which is responsible for the actual data transmission, and transmits bit streams (that is, electrical signals of 0 and 1) on the network medium . It provides the physical interface that converts the bit stream into a transmittable signal.

The main responsibilities of the physical layer include:

  1. Encoding and decoding of data : The physical layer converts digital signals into analog signals, and decodes the transmitted analog signals into digital signals at the receiving end to ensure correct data transmission.

  2. Data transmission method : The physical layer defines the method used to transmit data on the physical medium, which can be serial transmission or parallel transmission. It also defines parameters such as the transmission rate, bandwidth, and voltage of the signal.

  3. Establishment and teardown of physical connections : The physical layer is responsible for establishing and tearing down physical connections between sending and receiving devices to ensure efficient data transmission.

  4. Interface standards of physical media : The physical layer defines the interface standards of physical media (such as network cables, optical fibers, etc.) connecting devices to ensure interoperability between different devices.

  5. Basic transmission equipment : The physical layer includes various transmission equipment, such as repeaters, hubs, etc., which are used to amplify and transmit signals and enhance the coverage of the network.

In general, the main task of the physical layer is to provide a mechanism for reliable transmission of bit streams, and to provide reliable physical connections and transmission media for higher-level protocols.

Analogy to actual case explanation
The function of the physical layer can be compared to a postal system. The mailing system is responsible for delivering a letter or package from sender to recipient and ensuring that the letter is not damaged or lost in transit. In this analogy, the letter or package is the data, and the postal system between the sender and recipient is the physical layer. The physical layer is responsible for packaging the letter or package, selecting the appropriate packaging material and transmission method (such as mailing method, courier method), and ensuring that the item is delivered to the recipient efficiently and reliably during the transmission process, while providing tracking and confirmation Service ensures accuracy .

2. Data link layer

1. Basic concepts

The data link layer is the second layer in the TCP/IP model. It is mainly responsible for converting the IP datagram transmitted by the network layer into a bit stream that can be transmitted on the physical link, and ensuring the reliability of channel transmission and the integrity of data. sex.

in the data link layerCore ideainclude:

  1. Media Access Control ( Media Access Control,MAC): It defines the methods and rules for data transmission between multiple devices on a shared medium, as well as the address identification method of devices. Common MACprotocols are Ethernet CSMA/CD(carrier sense multipoint access/collision detection) and wireless LAN CSMA/CA(carrier sense multipoint access/collision avoidance).

  2. Logical Link Control ( Logical Link Control,LLC): It provides a communication service-oriented interface that enables the upper network layer to transparently access different physical media.

  3. Frame encapsulation : The data link layer encapsulates the datagrams received from the network layer into frames, and adds control information at the head and tail of each frame, such as synchronization bits, frame start and end characters, checksums, etc., so that The receiver correctly identifies and parses out the data.

Analogy to an actual case, the data link layer can be compared to the sorting and packaging work of the post office. In order to deliver the mail, in the role of the data link layer, the post office packs the mail (network layer datagram), pastes the correct address (MAC address) and number (frame sequence number), and then encapsulates it into a package (frame ), with opening and closing tags and other control information. The receiver's post office parses the package, checks that the address is correct, and delivers the mail to the correct recipient (upper network layer)

The main purpose of the data link layer is to ensure that data can be transmitted accurately on the physical link, and provide some mechanisms to deal with errors and retransmissions that may occur during data transmission to ensure reliable data transmission.

2.MAC address

MACAddress ( Media Access Control address), also known as physical address or hardware address, is a unique identifier used to identify computer network devices . Each network device (such as a computer, router, switch, network printer, etc.) is assigned a unique MACaddress when it is manufactured.

MACAddresses consist of 48 binary digits, usually expressed in hexadecimal. It is divided into 6 groups, each group contains 8 or 16 binary numbers. For example: 00-1A-2B-3C-4D-5E.

MACAn address consists of two parts: a globally unique identifier ( OUI) and a device unique identifier ( NIC). OUIis a 3-byte identifier assigned to each manufacturer by IEEE( ), used to indicate the identity of the manufacturer. It is a 3-byte identifier assigned by the manufacturer itself, which is used to identify the unique value of the manufacturer's device.Institute of Electrical and Electronics EngineersNIC

MACAddresses play a vital role in computer networking. At the data link layer, when a network device receives a data packet, it will use the destination MACaddress to determine whether the data is sent to itself or to other devices, and process it accordingly. Therefore, MACaddresses are mainly used for addressing and transmission control of data packets.

MACAddresses are unique worldwide, which means that devices from different manufacturers leave the factory MACwith different addresses. And MACthe address is fixed throughout the life cycle of the device, even if the location or connection of the physical device changes in the network environment.

Although MACthe address is unique, it is only LANvalid within the LAN ( ) range. In the case of crossing different networks, it is necessary to use higher-level addresses, such as IPaddresses

It should be noted that MACthe address can be modified, which may be necessary in some specific cases, but in general, you should avoid modifying MACthe address to avoid network communication problems.

3.ARP protocol

ARP( Address Resolution Protocol) is a protocol for IPmapping addresses to physical MACaddresses . In a computer network, when a host needs to send data to another host, it needs to know MACthe address of the destination host. However, hosts can only IPbe addressed by addresses, not directly MAC.

The role of the ARP protocol is to resolve the mapping relationship between IP addresses and MAC addresses . When a host wants to send data to a target host, it first checks the local ARPcache table ( ARP Cache), which records some known IPaddresses and corresponding MACaddresses.

If the target IPaddress does not ARPfind the corresponding MACaddress in the cache table, the sending host will send a broadcast ARPrequest packet ( ARP Request) to broadcast to other hosts in the LAN. This ARPrequest packet contains IPthe address and MACaddress of the source host, and IPthe address of the destination host.

When the target host receives ARPthe request packet, it checks IPwhether its own address IPmatches the target address. If there is a match, the destination host sends a unicast ARPresponse packet ( ARP Reply) to the source host, containing its own IPaddress and MACaddress.

When the source host receives ARPthe response packet, it will add IPthe address and address of the target host to the cache table for future communication.MACARP

In summary, the protocol is to determine the address of the target host ARPthrough broadcast and corresponding request and response packets, so that the mapping relationship between addresses and addresses can be established , so as to realize the correct delivery of data packets.ARPMACIPMAC

3. Network layer

1. Basic concepts

The network layer is the third layer in the TCP/IP model, responsible for routing and forwarding data packets between different networks. Its main functions include IP address allocation, routing and forwarding of data packets .

Analogy to the actual case, one can imagine that the network layer is like a post office system. When a person needs to send a letter to a friend in another city, he first writes down the friend's address (IP address), and then delivers the letter to his local post office. The job of the post office system is to distribute these letters to different cities and make sure they follow the correct route to their destination

In this example, the network layer acts as a post office system. Its specific tasks include:

  1. IP address assignment : The network layer is responsible for assigning a unique IP address to each device. Just like the post office system assigns everyone a unique address, the network layer assigns unique IP addresses to devices connected to the network so they can recognize and communicate with each other.

  2. Routing : The network layer determines the transmission path of data packets between different networks by using routing protocols. Just like the post office system chooses the best route for letters based on the postal code system, the network layer uses routing protocols to determine the best path for packets of data to travel between different networks.

  3. Data packetization and forwarding : The network layer divides the data provided by the upper transport layer into smaller data packets (also called data packets) and adds them to the packet header in the network layer header. These data packets will be forwarded through appropriate network nodes according to the routing table in the network layer to ensure that the data packets reach the destination with the correct path.

Through the above process, the network layer plays the role of connecting different networks, so that the entire network can theoretically communicate seamlessly. The work of the network layer can help realize the distribution, forwarding and routing of data, and ensure the accurate transmission of data and reach the target network service.

2. IP protocol

The IP protocol refers to the Internet Transmission Protocol ( Internet Protocol), which is one of the most important protocols in computer networks. IPThe protocol uses a method of packet exchange data to realize network communication, which defines the format, rules and mechanisms of data transmission in the network.

The IP protocol is mainly responsible for two important functions: addressing and routing . Addressing refers to IPthe unique identification of devices or hosts in the network through addresses, through which IPdata can be sent to target devices. Routing refers to IPthe process of selecting an appropriate path according to the destination address to transmit the data packet to the destination device.

IPThe protocol uses a connectionless communication method, which means that each data packet is independent during transmission, and no long-term connection is established. Data packets may pass through multiple network nodes during transmission, and these nodes select the transmission path according to the routing table. IPThe protocol is responsible for transmitting data packets from the source address to the destination address, but it does not guarantee that the data packets will arrive in order, not lost and not repeated. These problems need to be solved in higher layer protocols (such as TCP) .

IPThe protocol uses a 32 -bit IPaddress to identify a device or host, IPv4using dotted decimal notation (such as 192.168.0.1 ), and IPv6a 128-bit address space (such as 2001:db8:0:1234:0:567:8:1). In addition IPto the address, IPthe protocol also defines other fields, such as version, header length, service type, total length, identifier, flag, slice offset, time to live, protocol, header checksum, source IP address, and destination IP address wait.

IP version IP address format
IPv4 Four decimal numbers, each ranging from 0 to 255, separated by dots, such as 192.168.0.1
IPv6 Eight groups of four-digit hexadecimal numbers, separated by colons, such as 2001:0db8:85a3:0000:0000:8a2e:0370:7334

An IPv4 address consists of 32-bit binary numbers, usually expressed in dotted decimal notation. Each decimal digit represents the value of an 8-bit binary number. An IPv4 address is divided into a network part and a host part, wherein the network part represents the identity of the network, and the host part represents the identity of a specific host.

An IPv6 address consists of 128-bit binary numbers. In order to reduce redundancy, IPv6 uses abbreviation rules, such as removing leading 0s, combining consecutive 0s, and using double colons to represent a group of 0s, etc. The increase of IPv6 addresses is to solve the shortage of IPv4 addresses.

IPProtocol is the core protocol in computer network, which makes Internet communication possible. Through IPthe protocol, different types of devices and hosts can exchange data and information with each other, realizing global Internet transmission.

3. Subnet mask

A subnet mask is a method used to divide network addresses. It is represented by a 32-bit binary number and has the same structure as an IP address. A subnet mask divides a network by indicating which bits in an IP address are used for network identification and which bits are used for host identification .

In a computer network, an IP address consists of two parts: the network address and the host address. A subnet mask divides an IP address into two parts: a network identifier and a host identifier. The network identifier is used to indicate the network where the IP address is located, and the host identifier is used to identify a specific host in the network. The 1 bit in the subnet mask represents the network identification, and the 0 bit represents the host identification.

Whether two IP addresses are in the same local area network can be judged by comparing whether the network identification parts of the two IP addresses are the same . Specific steps are as follows:

  1. Get the subnet masks of two IP addresses.
  2. Perform a logical AND operation on the two IP addresses and the subnet mask.
  3. Comparing the results, if the logical AND operation results of the two IP addresses and subnet masks are the same, it means that they are in the same LAN, otherwise they are not in the same LAN.

For example, suppose there are two IP addresses: 192.168.1.100 and 192.168.1.200 with a subnet mask of 255.255.255.0. Perform a logical AND operation on the two IP addresses and the subnet mask, and the result is as follows:

The result of logical AND operation between 192.168.1.100 and 255.255.255.0 is 192.168.1.0
The result of logical AND operation between 192.168.1.200 and 255.255.255.0 is 192.168.1.0

It can be seen that the result of the logical AND operation between the two IP addresses and the subnet mask is the same, so they are in the same LAN.

It should be noted that if the subnet masks of two IP addresses are different, even if their network identification parts are the same, they are not necessarily in the same LAN. Therefore, when judging whether two IP addresses are in the same LAN, in addition to comparing the network identification part, it is also necessary to consider whether the subnet mask is the same.

4. Transport layer

1. Basic concepts

The transport layer in computer networking is a layer in the network hierarchy that manages the transmission and connection of data. The main function of the transport layer is to provide end-to-end communication services between the source host and the destination host .

The main tasks of the transport layer include:

  1. Segmentation and Reassembly : The transport layer breaks application layer data into smaller chunks of data called segments, and reassembles these segments on the destination host to restore the original data. This process of segmentation and reassembly can solve the problem of size limitation and unreliability in network transmission .

  2. Port Identification : The transport layer uses port numbers to identify applications in communication. Each application can use a unique port number so that the transport layer can accurately deliver the received data to the target application .

  3. Transmission control : The transport layer ensures reliable data transmission by implementing mechanisms such as error detection and correction, flow control, and congestion control. These mechanisms guarantee data integrity, in-order delivery, and reliability .

  4. Multiplexing and demultiplexing : The transport layer can share network connections between multiple applications, by packaging the data of multiple applications in one transport layer connection for transmission, and then decomposing the data on the target host to individual applications.

A practical application of the analogy is the mailing service. The transport layer can be compared to a post office, which is responsible for managing and controlling the transmission process of mail. Specifically, the Post Office addresses mail size limitations by breaking mail into smaller envelopes (fragmentation and reassembly). Each envelope has a unique address (port identification) to ensure that mail is delivered exactly to the intended recipient. In addition, the Post Office will provide error detection and repair mechanisms to ensure the integrity and reliability of mail. When multiple people use the post office at the same time, the post office can process the mail of different people through multiplexing and demultiplexing to ensure that everyone can receive their own mail

In summary, the transport layer is like a key service layer that carries data segmentation, transport control, and connection management. It ensures the reliability and reliability of network transmission through various mechanisms.

2. Agreement

The transport layer in computer networking involves the following network protocols:

protocol describe
TCP Transmission Control Protocol (Transmission Control Protocol) is a connection-oriented protocol that provides reliable data transmission. It ensures data reliability through sequence number, confirmation and retransmission mechanism, and realizes flow control and congestion control through sliding window mechanism. TCP is the most commonly used protocol in the transport layer.
UDP User Datagram Protocol (User Datagram Protocol) is a connectionless protocol that does not provide reliability guarantees. It only provides basic data transmission functions, and the transmission efficiency is high. UDP is suitable for applications with high real-time requirements, such as video streaming and audio streaming.
SCTP The Stream Control Transmission Protocol (Stream Control Transmission Protocol) is a message-oriented protocol with reliability and message integrity guarantees. SCTP supports multi-stream transfers, multi-homed connections, and optional ordered transfers. SCTP is suitable for applications that require reliable and ordered data transmission.

These protocols play different roles and functions in the transport layer of computer networks, and provide different data transmission methods and reliability guarantees. In practical applications, according to the requirements of the application program and the different network environments, an appropriate transport layer protocol is selected for data transmission.

In addition to the above protocols, there are other transport layer protocols such as Datagram Congestion Control Protocol (DCCP) and Transport Layer Security (TLS). These protocols play different roles in computer networks and meet the needs of different applications.

3. TCP (three-way handshake and four-way wave)

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer protocol. It is part of the Internet protocol stack used to transmit data in the network .

TCP protocolmain effectIt is to provide reliable data transmission . It ensures data reliability by:

  1. A virtual channel is established for each connection to ensure that data is transmitted in the correct order.
  2. The reliability of data is guaranteed through confirmation and retransmission mechanisms. After sending data each time, the receiver will send a confirmation message to the sender. If the sender does not receive the confirmation message, it will resend the data.
  3. Provide a flow control mechanism to prevent the sender from sending too fast and the receiver cannot process it.

TCPAgreementwork processas follows:

  1. Establishing a connection : Before transmitting data, the sender and receiver need to establish a connection. The process of establishing a connection is called a three-way handshake. The sender sends a message requesting a connection, the receiver replies with a confirmation message after receiving it, and the sender replies with a final confirmation message.
  2. Data transfer : After the connection is established, the sender can start sending data to the receiver. Packets are transmitted in the order specified, and the receiver sends an acknowledgment to the sender.
  3. Closing the connection : After the data transfer is complete, the sender and receiver can close the connection. The process of closing the connection is called four waves. The sender sends a message requesting to close the connection, the receiver replies with a confirmation message, then the receiver sends a message requesting to close the connection, and the sender replies with a final confirmation message.

The process of three-way handshake to establish a connection is as follows:

  1. The client sends a SYN(synchronous) message to the server, indicating that the client requests to establish a connection.
  2. After receiving SYNthe message, the server sends a SYN + ACK(synchronization + confirmation) message as a response, indicating that the connection can be established.
  3. After the client receives SYN + ACKthe message from the server, it sends a ACK(confirmation) message, indicating that the client confirms that the connection can be established.
    After the connection is established, both parties can carry out data transmission.
客户端                  服务器
  |                       |
  |     SYN报文发送        |
  |---------------------> |
  |                       |
  |  SYN + ACK报文发送    |
  | <---------------------|
  |                       |
  |     ACK报文发送        |
  |---------------------> |
  |       连接建立         |
  |                       |

The process of closing the connection by waving four times is as follows:

  1. The client sends an FIN(end) message, indicating that the client wishes to close the connection.
  2. After receiving FINthe message, the server sends a ACKmessage as a response to confirm the connection closing request.
  3. The server sends a FINmessage indicating that the server also wishes to close the connection.
  4. FINAfter receiving the message from the server, the client sends a ACKmessage as a response to confirm that the connection is closed.
客户端                            服务器
  |                                 |
  |  FIN报文发送                    |
  |------------------------------>  |
  |                                 |
  |      ACK报文发送                 |
  | <------------------------------- |
  |                                 |
  |  FIN报文发送                    |
  |------------------------------>  |
  |                                 |
  |      ACK报文发送                 |
  | <------------------------------- |
  |        连接关闭                 |
  |                                 |

The connection is established by three-way handshake and four-way handshake to close the connection, TCPwhich ensures the reliability of data transmission and the correct establishment and closure of the connection.

+--------+                     +-----------+
| 客户端 |                     | 服务器   |
+--------+                     +-----------+
     |                                 |
     |    SYN=1,seq=x                |
     |------------------------------>|
     |                                 |
     |    SYN=1,ACK=1,seq=y, ack=x+1 |
     |<------------------------------|
     |                                 |
     |    ACK=1,seq=x+1, ack=y+1      |
     |------------------------------>|
     |                                 |
     |       请求连接建立             |
     |                                 |
     |                                 |
     |    FIN=1,seq=x                |
     |------------------------------>|
     |                                 |
     |   ACK=1,seq=y, ack=x+1         |
     |<------------------------------|
     |                                 |
     |      通知结束连接              |
     |                                 |
     |    FIN=1,ACK=1,seq=y+1, ack=x |
     |<------------------------------|
     |                                 |
     |    ACK=1,seq=x, ack=y+1        |
     |------------------------------>|
     |                                 |

In summary, TCPthe protocol is a reliable transmission protocol that ensures data reliability by establishing connections, confirmation and retransmission mechanisms, and flow control. It is one of the most commonly used transport protocols in the Internet.

4. UDP

UDP(User Datagram Protocol)Is a connectionless, unreliable transport protocol for sending datagrams over a network. Unlike with TCP, UDPthere is no need to establish a connection before transmitting data, and there is no reliability guarantee and data verification.

UDPThe protocol works as follows:

  1. The application encapsulates the data into a packet ( Datagram) and specifies the destination IP address and port number.
  2. Packets of data travel across the network, but there is no guarantee of packet order, integrity, or whether they reach their destination.
  3. The receiver application receives the packet over the network.
  4. The receiver application processes the packet based on its source IP address and port number.
  5. The application program extracts the data in the data packet and performs corresponding operations.

UDPFeatures of the agreement include:

  1. No connection: The sender does not need to establish a connection with the receiver in advance, and can directly send data packets.
  2. Unreliability: The sender cannot know whether the data packet has reached the destination, nor can it guarantee the order of the data packet, so it is not suitable for scenarios that require high reliability of data transmission.
  3. Rapidity: Since there is no need to establish a connection and ensure reliability, UDPthe data transmission speed is relatively fast.
  4. Simplicity: TCPCompared with , UDPthe protocol header of , is smaller and simpler, so it is more UDPsuitable in some applications that are sensitive to network overhead, such as real-time video transmission, voice communication, etc.

UDPCommonly used in the following situations:

  1. Real-time applications: such as audio and video transmission, real-time communication, etc., because of UDPits speed and simplicity can provide lower delay.
  2. Broadcast and multicast applications: UDPData packets can be sent to multiple receivers at the same time.
  3. DNSResolution: Domain name resolution is used to UDPsend requests.

It should be noted that due to UDPthe unreliability of the protocol, some data packets may be lost in some cases, so it is recommended to use the protocol in the case of important data transmission or high data integrity requirements TCP.

5. Application layer

1. Basic introduction

The application layer is the highest layer in the computer network, which provides the interface between the network application program and the network transport layer. The application layer is mainly responsible for handling the communication services of user applications, enabling applications in different networks to communicate with each other and exchange data.

The main work of the application layer includes the following aspects:

  1. Definition of application protocol : The application layer defines the format and rules for exchanging data between applications, such as HTTP, SMTP, FTP and other protocols. These protocols describe the structure, encoding and transmission methods of data, as well as various details of communication.

  2. Data encoding and format conversion : The application layer is responsible for converting data generated by the application into a format suitable for transmission over the network, and at the receiving end into a format recognizable by the application. For example, convert data into ASCII code or Unicode code, or convert data such as audio and video into a format suitable for transmission.

  3. Data segmentation and reassembly : When the amount of data is too large, the application layer is responsible for segmenting the data and reorganizing the segmented data into complete data at the receiving end. Segmentation improves transfer efficiency while ensuring data integrity.

  4. Data transmission control : The application layer is responsible for controlling data transmission, including transmission speed, security, reliability, etc. For example, increase the transmission rate by establishing multiple parallel connections, ensure data security by using encryption technology, and ensure reliable data transmission by using confirmation and retransmission mechanisms.

  5. User authentication and authorization : The application layer can provide user authentication and authorization services to ensure that only legitimate users can access network resources and control user operation rights. This kind of service is generally implemented through authentication methods such as user name and password.

In short, the application layer is a key link in the computer network responsible for realizing the communication between the user application program and the network transport layer. It defines the communication rules and formats of the application program, and provides functions such as data transmission control, data conversion and processing.

2. URI sum URL

URI (Uniform Resource Identifier)and URL (Uniform Resource Locator)are both ways to identify and access network resources. There is a certain relationship between them, URI is a special form of URL. The following table shows their explanations and descriptions:

vocabulary explain illustrate
URI Uniform Resource Identifier (Uniform Resource Identifier) It is used to identify and locate a unique resource, which can be any accessible resource on the network, such as web pages, files, etc.
URL Uniform Resource Locator (Uniform Resource Locator) It is a specific form of URI, which contains the complete address pointing to a specific resource, and can be opened directly in the browser.
Format URIs can have many different formats, including URL, URN, etc. URL is the most common URI format used to locate resources and specify how to access them.
use Used to identify and access resources on the network URI or URL can be used to accurately locate resources on the network, such as web pages, pictures, videos, and so on.
containment relationship URL is a concrete implementation of URI and is a subset of URI URL is a URI in a specific format, which contains location information of resources. Thus, all URLs are URIs, but not all URIs are URLs.
example http://www.example.com/index.html This is a typical URL, indicating a link to a website page under the HTTP protocol.
file:///path/to/file.txt This is also a URL, representing the path of a local file.
urn:isbn:0-486-27557-4 This is a URN that uniquely identifies an International Standard Book Number (ISBN). Although it is a URI, it is not a URL, because it does not specify a specific way (such as a network protocol) to access the resource.

It should be noted that although the concepts of URI and URL are different, they are often used interchangeably in actual use, and the term URL is often used to refer to a unique identifier of a resource.

URL (Uniform Resource Locator) consists of the following parts:

  1. Protocol (Protocol): The first part of the URL is the protocol, which is used to specify the communication protocol between the client and the server. Common protocols are: HTTP (for web page access), HTTPS (encrypted web page access), FTP (file transfer protocol), etc. Example: http://

  2. Domain Name (Domain Name): The second part of the URL is the domain name, which is used to specify the server where the resource to be accessed is located. A domain name is composed of multiple parts separated by dots (.), and the higher domain name levels are indicated from right to left. Example: www.example.com

  3. Port number (Port): The third part of the URL is the port number, which is used to specify the port that is listening on the server. If not specified explicitly, the default port number will be used. The default port for HTTP is 80, and the default port for HTTPS is 443. Example: :8080

  4. Path (Path): The fourth part of the URL is the path, which is used to specify the path or directory where the resource to be accessed is located. Paths can be absolute (starting with /) or relative. Example: /path/to/resource

  5. Query Parameters (Query Parameters): The fifth part of the URL is the query parameter, which is used to pass additional information or request parameters to the server. Parameters start with ?, and multiple parameters are separated by &. Each parameter consists of a key-value pair, and the key and value are connected with =. Example: ?param1=value1¶m2=value2

  6. Anchor : The last part of the URL is the anchor, which is used to specify a specific location in the page. Anchor points start with # followed by a specific identifier. Example: #section1

Suppose you want to access a blog post on your website with a URL of https://www.example.com/blog/article?id=123#comments.

component example illustrate
plan https:// Use HTTPS protocol for network communication
domain name www.example.com The domain name of the visited website is www.example.com
The port number 443 By default the default port number 443 is used for HTTPS
path /blog/article The accessed resource path is /blog/article
query parameters ?id=123 The value of the parameter id passed in the URI is 123
fragment identifier #comments Navigate to the part specifically identified as comments within the resource

3. http protocol

1. Basic introduction

HTTP(Hypertext Transfer Protocol)It is an application layer protocol for transmitting hypertext, which is the basis of Web communication. The main job of the HTTP protocol is to transmit information between the client and the server, enabling the client to request and receive web resources on the server .

The way the HTTP protocol works is based on a request-response model . The client sends HTTPa message containing the request to the server, and the server sends HTTPa message containing the response to the client. The request message includes the request method, request URI(Uniform Resource Identifier), protocol version, request header and request data, etc., while the response message includes the protocol version, status code, response header and response data, etc.

The HTTP protocol has the following characteristics:

  1. Connectionless: Each request-response interaction is independent, and the server does not keep any state information about the client. This is beneficial for the server to process multiple client requests, but it also causes a new connection to be established for each request, which increases a certain amount of overhead.

  2. Stateless: The server does not save any information from previous requests, and each request is independent. If you need to maintain the state of the client, you can use Cookiethe mechanism such as to achieve it.

The main tasks of the HTTP protocol include:

  1. Establish a connection: When the client initiates a request to the server, it needs to establish TCPa connection first. Through the three-way handshake process, a reliable communication link is established between the client and the server.

  2. Send request: The client sends an HTTP request to the server, including the requested method ( GET、POSTetc.), requested URIand protocol version , etc. The request may contain additional header information and request data, such as request parameters , Cookieetc.

  3. Processing request: After receiving the request, the server URIperforms corresponding processing according to the request and method information, such as querying the database, reading files, etc. The server may verify the legitimacy of the request and generate response data.

  4. Send a response: The server generates a response message, including the protocol version, status code, response header, and response data . Then send the response to the client.

  5. Receiving response: After receiving the response message sent by the server, the client analyzes it according to the protocol version and status code. The client can obtain the response data and perform corresponding processing, such as parsing HTML, rendering pages, and so on.

The HTTP protocol plays a key role in Web communication, which enables the client to interact with the server and obtain the required Web resources. It defines the rules and formats of communication, and provides a flexible extension mechanism, enabling Web applications to transmit data quickly and reliably.

2. Request message

The HTTP request message consists of three parts: request line, request header and request body.

  1. Request line: The request line contains the request method, requested URL, and HTTP protocol version. Common request methods include GET, POST, PUT, DELETE, etc. For example:

    GET /index.html HTTP/1.1
    
  2. Request header: The request header contains additional information about the request, presented in the form of key-value pairs. Common request headers include Host, User-Agent, Accept, Content-Type, etc.

request header field describe example
Host Specifies the domain name and port number of the requested server Host: www.example.com
User-Agent Identifies the user agent sending the request User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Accept Tell the server what response content types are acceptable Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Language 告诉服务器可接受的响应内容语言 Accept-Language: en-US,en;q=0.9
Accept-Encoding 告诉服务器可接受的响应内容的压缩方法 Accept-Encoding: gzip, deflate, br
Connection 维持持久连接,或关闭连接 Connection: keep-alive
Referer 告诉服务器发送请求的前一页URL Referer: http://www.example.com/page1.html
Cookie 包含之前由服务器设置的cookie Cookie: name=value; name2=value2
Authorization 用于身份验证的凭证信息 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Content-Type 请求体的媒体类型 Content-Type: application/json
Content-Length 请求体的字节数 Content-Length: 348
If-None-Match 如果资源的ETag与请求头中指定的一致,则返回304 Not Modified If-None-Match: “686897696a7c876b7e”
If-Match 如果资源的ETag与请求头中指定的一致,则进行请求处理 If-Match: “686897696a7c876b7e”

例如:

Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Content-Type: application/json
  1. 请求主体:请求主体可选,用于传输请求的数据,例如表单数据或JSON数据。在POST请求中常见的请求主体就是要发送的数据。例如:
    {
      "username": "example_user",
      "password": "123456"
    }
    

综合示例:

POST /login HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Content-Type: application/json

{
  "username": "example_user",
  "password": "123456"
}

以上示例是一个使用POST方法向www.example.com/login路径发送JSON数据的HTTP请求报文。

3. 响应报文

HTTP的响应报文是服务器向客户端返回的数据格式,它由三部分组成:响应行、响应头和响应体。下面对每个部分进行详细的介绍,并举例说明。

  1. 响应行:
    响应行包含了响应的状态码和状态码的文本描述,格式为“HTTP版本 状态码 状态码文本描述”。例如:HTTP/1.1 200 OK
  • HTTP版本指明服务器所使用的HTTP协议的版本。
  • 状态码代表了请求的处理结果的状态。常见的状态码有:
    • 200:请求成功
    • 404:资源未找到
    • 500:服务器内部错误

以下是常见的HTTP响应状态码表格说明:

状态码 状态码描述 说明
200 OK 请求成功
201 Created 请求已成功处理,并且已创建了一个新的资源
204 No Content 请求已成功处理,但响应报文中无返回内容
301 Moved Permanently 资源的URL被永久移动到了另一个位置
302 Found 资源的URL被临时移动到了另一个位置
304 Not Modified 资源自从上次请求后未发生过修改,可使用客户端缓存的版本
400 Bad Request 客户端请求的语法错误或无法被服务器理解
401 Unauthorized 请求缺乏有效的身份验证凭证
403 Forbidden 服务器理解请求,但拒绝执行操作,权限不足或者对资源的访问被禁止
404 Not Found 请求的资源在服务器上不存在
500 Internal Server Error 服务器在执行请求时遇到了无法处理的错误
503 Service Unavailable 服务器暂时无法处理请求,一般为过载或维护
  1. 响应头:
    响应头包含了关于响应的一些补充信息,格式为“名称: 值”。常见的消息报头有:
  • Content-Type:指明了响应正文的MIME类型。例如:Content-Type: text/html
  • Content-Length:指明了响应正文的字节长度。例如:Content-Length: 1024
  • Server:指明了服务器的软件信息。例如:Server: Apache/2.4.29 (Unix)
  1. 响应体:
    响应体是服务器返回给客户端的实际数据,它可以是文本、HTML、XML、JSON等格式。请求成功时,响应正文会包含所请求资源的内容;而出现错误时,响应正文可能包含出错信息或者空白。

下面是一个简单的例子:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1024
Server: Apache/2.4.29 (Unix)

<!DOCTYPE html>
<html>
<head>
   <title>Example Page</title>
</head>
<body>
   <h1>Hello, World!</h1>
   <p>This is an example page.</p>
</body>
</html>

在这个例子中,状态行指明了响应的状态是200 OK,消息报头指明了响应正文的MIME类型为text/html,响应正文是一个简单的HTML页面。

4.DNS服务器

1. 基本概念

DNS服务器是一个专门用于域名系统(DNS)的服务器。它的主要功能是将域名解析成相应的IP地址,并将网络请求路由到正确的服务器上。DNS服务器保存着域名和IP地址的对应关系,当用户在浏览器中输入一个域名时,DNS服务器会根据域名查询DNS解析记录,并返回相应的IP地址给用户的计算机,从而使用户能够访问目标网站。DNS服务器也可以进行其他功能,如缓存DNS解析结果以提高解析速度、执行反向DNS解析等。

2. 域名

域名是指用于在互联网上标识和定位网站的字符串。它由多个部分组成,包括顶级域名(TLD)、二级域名(SLD)和子域名(Subdomain)等。

以下是域名的组成及其说明:

组成 说明 示例
协议 域名所使用的网络协议或协议组合 http://或https://
子域 可选的域名部分 blog.
主域 域名的主要部分 example
顶级域 域名的最高级部分 .com

例如,对于URL “http://blog.example.com”,其域名的组成如下:

  • 协议:http://://
  • 子域:blog.
  • 主域:example
  • 顶级域:.com

通过域名,用户可以更容易地记住和访问网站,而不需要记住复杂的IP地址。

以下是一些常见的顶级域名

顶级域名 说明
.com 商业领域,普遍用于商业网站
.org 非营利组织,常用于非盈利性质的组织或个人
.net 网络,常用于网络相关的网站
.edu 教育,主要用于教育机构或学术机构
.gov 政府,用于政府或政府机构
.mil 军事,用于军事组织或军方机构
.int 国际组织,用于国际性组织
.info 信息,常用于提供信息的网站
.biz 商务,用于商务相关的网站
.name 个人名称,用于个人网站
.pro 专业人士,常用于专业人士的网站
.coop 合作社,常用于合作社或合作组织
.museum 博物馆,用于博物馆相关的网站
.aero 航空运输业,用于航空运输业
.int 网际,常用于国际性的互联网机构
.travel 旅游,用于旅游业相关的网站
.jobs 就业机会,用于提供就业机会
.mobi 移动互联网,用于移动互联网相关的网站
.asia 亚洲,用于亚洲地区的网站
.cat 加泰罗尼亚语,用于加泰罗尼亚语言的网站
.eu 欧洲联盟,用于欧洲联盟成员国
.tel 电话号码,用于电话号码相关的网站
.xxx 含有成人内容的网站
.cn 中国,用于中国地区的网站

3. DNS解析域名的过程

域名系统(DNS)通过将域名解析为相应的IP地址来帮助计算机在互联网上定位其他计算机。以下是DNS通过域名查找IP地址的过程:

  1. 用户在浏览器中输入一个域名(例如www.example.com)。
  2. 本地计算机首先检查它的本地DNS缓存,看看是否已经解析过这个域名。如果有,它将返回对应的IP地址,并跳到第6步。
  3. 如果本地缓存没有相应的解析记录,计算机将发送一个DNS查询到本地网络的DNS服务器。
  4. 本地DNS服务器检查它的缓存,如果没有相应的解析记录,它将发起一个迭代查询,开始解析这个域名。
  5. 本地DNS服务器首先询问根域名服务器,根域名服务器知道所有的顶级域名服务器的IP地址。根域名服务器告诉本地DNS服务器.com顶级域名服务器的IP地址。
  6. 本地DNS服务器接着向.com顶级域名服务器发送一个查询请求。
  7. .com顶级域名服务器根据请求返回相应的权威域名服务器的IP地址。权威域名服务器是负责保存特定域名解析记录的服务器。
  8. 本地DNS服务器在知道权威域名服务器的IP地址后,发送一个查询请求给权威域名服务器。
  9. 权威域名服务器检查自己的数据库,找到对应的域名解析记录,并将IP地址返回给本地DNS服务器。
  10. 本地DNS服务器将IP地址存储在缓存中,并将IP地址返回给用户的计算机。
  11. 用户的计算机通过获取到的IP地址与目标计算机建立连接,并开始进行通信。

5. DHCP

Dynamic Host Configuration Protocol (DHCP)是一种网络协议,用于自动分配IP地址、子网掩码、默认网关和DNS服务器的配置给连接到网络上的设备。

DHCP服务是在网络中提供自动IP地址分配和其他网络配置设置的服务。当设备连接到网络时,它会向DHCP服务器发送请求,DHCP服务器会为设备分配一个可用的IP地址,并提供其他网络配置信息。这样,设备就可以自动获得所需的网络配置,而不需要手动配置。DHCP服务可以大大简化网络管理员的工作,同时提供更方便和高效的网络管理。

DHCP服务的工作原理如下:

  1. 设备加入网络时,向网络中的DHCP服务器发送一个DHCP请求。
  2. DHCP服务器收到请求后,在可用的IP地址池中分配一个可用的IP地址,并将其回复给设备。
  3. 设备收到DHCP服务器分配的IP地址,并使用该地址进行网络通信。
  4. DHCP服务器还可以提供其他网络配置信息,如子网掩码、默认网关和DNS服务器的IP地址等。
  5. 设备接受到其他网络配置信息后,可以根据配置信息进行网络设置,以正常使用网络服务。

通过使用DHCP服务,网络管理员可以更方便地管理整个网络,而不需要手动为每个设备分配IP地址和其他网络配置。同时,DHCP还支持IP地址的动态分配和租用,可以在设备不再需要时将分配给它的IP地址回收,以供其他设备使用。这使得网络中的IP地址使用更加高效。

总而言之,DHCP服务是一种自动分配IP地址和其他网络配置的服务,它提供了简化和高效的网络管理方式,使设备能够更方便地连接到网络并使用网络服务。

6. 在浏览器地址栏里输入一个URL,到这个页面呈现出来,中间会发生

以下是从输入URL到页面呈现的基本流程:

  1. 用户在浏览器地址栏中输入URL(统一资源定位符)。
  2. 浏览器通过URL解析出主机名(例如 www.example.com)。
  3. 浏览器使用DNS(域名系统)解析器将主机名转换为IP地址,该IP地址对应于服务器,以便能够建立与服务器的连接。
  4. 浏览器使用HTTP(超文本传输协议)建立与服务器的TCP(传输控制协议)连接。
  5. 建立连接后,浏览器发送一个HTTP请求到服务器,该请求包含请求的资源(例如网页、图像等)的详细信息。
  6. 服务器接收到请求后,根据请求的资源,生成相应的HTTP响应。
  7. 服务器将HTTP响应发送回浏览器。
  8. 浏览器接收到HTTP响应后,解析响应并获取所请求资源的数据。
  9. 浏览器将获取到的数据渲染为用户可见的页面。
  10. 页面呈现完成后,浏览器会继续解析页面上的其他资源(例如脚本、样式表等)并进行相应的处理。

整个过程涉及到多个步骤和技术,包括URL解析、DNS解析、建立连接、发送请求、接收响应、解析和渲染等。这些步骤之间的具体实现可能因浏览器的不同而有所变化,但基本流程是大致相同的。

总结

通过本篇文章的介绍,我们对计算机网络中的五层网络协议有了更深入的了解。首先,我们了解了网络协议的概念以及为什么需要将网络协议分为不同的层次。接着,我们详细介绍了每一层网络协议的功能和作用,从物理层到应用层,每一层都扮演着重要的角色。我们了解了数据在每一层传输的方式以及相应的协议。同时,我们也了解了网络协议的分层模型,如OSI模型和TCP/IP模型,并了解了它们之间的关系。最后,我们讨论了网络协议在实际应用中的作用,如网络安全和网络性能优化。通过学习五层网络协议,我们对计算机网络的运行机制和原理有了更加深入的了解,这对我们在网络相关的工作和学习中都非常有帮助。

Guess you like

Origin blog.csdn.net/jieyucx/article/details/132560363