Computer Network (2) - Network Protocol Foundation, TCP/UDP

1. The concept of agreement

1. What is an agreement:

From an application point of view, a protocol can be understood as a "rule", which is a rule for data transmission and data interpretation.

The transmission rules that are only obeyed between A and B are called the original protocol. When the protocol is adopted by more people, it is continuously increased and improved, and finally a stable and complete file transfer protocol is formed, which is widely used in various file transfer processes, and the protocol becomes a standard protocol.

2. Typical agreement:

Transport layer: common protocols are TCP/UDP protocol

Application layer: common protocols include HTTP protocol, FTP protocol

Network layer: common protocols include IP protocol, ICMP protocol, IGMP protocol

Network interface layer: common protocols include ARP protocol, RARP protocol

(1) TCP transport protocol: a connection-oriented, reliable, byte stream-based transport layer communication protocol.

(2) UDP User Datagram Protocol: It is a connectionless transport layer protocol in the OSI reference mode, providing a simple and unreliable information transmission service for transaction,

(3) HTTP Hypertext Transfer Protocol: It is the most widely used network protocol on the Internet.

(4) FTP file transfer protocol

(5) IP protocol is the Internet Internet Protocol

(6 ) The ICMP protocol is the Internet Control Message Protocol , which is a sub-protocol of the TCP/IP protocol suite. It is used to transfer control protocols between IP hosts and routers.

(7) The IGMP protocol is an Internet group management protocol , a multicast protocol in the Internet protocol family, which runs between a host and a multicast router.

(8) ARP protocol is a forward address resolution protocol , which finds the MAC address of the corresponding host through the known IP.

(9) The RARP protocol is a reverse address translation protocol , which determines the IP address through the MAC address.

 

2. Web Application Design Patterns

1. C/S mode

The traditional network application design mode, the client/server mode, needs to deploy the client and the server respectively in the two communication boxes to complete the data communication.

advantage:

(1) Data can be buffered locally on the client side to improve operating efficiency.

(2) The protocols that can be adopted are more flexible.

shortcoming:

(1) It poses a threat to the safety of users

(2) Client/server development is required, and the theoretical workload is doubled.

(3) In the process of program development, joint debugging is difficult.

2. B/S mode

Browser/server mode. Just deploy the server on one end, and use the browser configured by default on each PC to complete the data transfer on the other end.

Advantages: 7.

(1) The amount of work development is small.

(2) The development is not limited by the platform, and the portability is strong.

(3) High user security.

shortcoming:

(1) It must use the http protocol to communicate with the browser, and the protocol selection

(2) Limited network application support and unsatisfactory buffered data

 

3. Layered Model

1. OSI seven-layer model

(7) Application layer

(6) Presentation layer

(5) Session layer

(4) Transport layer

(3) Network layer

(2) Data link layer

(1) Physical layer

(1) Physical layer: It mainly defines the physical equipment standards, such as the connection type of the network cable, the transmission rate of various transmission media, etc. Its main function is to transmit the bit stream (that is, it is converted from 1, 0 to current strength for transmission, and then converted to 1, 0 after reaching the destination, which is often referred to as digital-to-analog conversion and analog-to-digital conversion) This layer The data are called bits.

(2) Data link layer: defines how to transmit data in units of frames and how to control access to the physical medium. This layer also typically provides error detection and correction to ensure reliable transmission of data. Such as: 115200, 8, N, 1 used in serial communication.

(3) Network layer: Provides connection and path selection between two host systems in a network located in different geographical locations. The development of the Internet has greatly increased the number of users accessing information from sites around the world, and the network layer is the layer that manages this connection.

(4) Transport layer: Define some protocols and port numbers for data transmission (WWW port number 80, etc.), such as: TCP (Transmission Control Protocol, low efficiency, strong reliability, high user transmission reliability requirements, small data volume data) ), UDP (User Datagram Protocol, just the opposite of TCP characteristics, used to transmit data with low reliability requirements and a large amount of data) . It is mainly to segment and transmit the data received from the lower layer, and then reassemble it after reaching the destination. This layer is usually called a segment.

(5) Session layer: establishes a data transmission path through the transport layer (port number: transmit port and receive port) . Mainly initiate sessions or receive session requests between your systems (devices need to know each other so that IP can also be MAC or hostname).

(6) Presentation layer: It can ensure that the information sent by the application layer of one system can be read by the application layer of another system . Example: A PC programmer communicates with another computer, one of which uses Extended-Decimal Interchange Code (EBCDIC) and the other uses American Standard Code for Information Interchange (ASCII) to represent the same characters, if any If necessary, the presentation layer uses a common format to convert between multiple data formats.

(7) Application layer: The OSI layer closest to the user, this layer provides network services for the user's applications (e-mail, file transfer, terminal emulation, etc.).

2. TCP/IP four-layer model

The TCP/IP network protocol stack is divided into four layers: an application layer (Application), a transport layer (Transport), a network layer (Network), and a link layer (link, network interface layer). As shown below:

Two computers are located in different networks, and the data transfer process from one computer to another computer:

The link layer has Ethernet, token ring network and other standards. The link layer is responsible for the driver of the network card device, frame synchronization (that is, what signal is detected from the network cable as the start of a new frame) , and conflict detection (if a conflict is detected, it will automatically retransmission) , data error checking, etc. A switch is a network device that works at the link layer and can forward data frames between different link layer devices ( such as between 10M Ethernet and 100M Ethernet, between Ethernet and Token Ring). The frame format of the link layer is different, and the switch needs to remove the link layer header and re-encapsulate the future datagram before forwarding.

The IP protocol of the network layer is the foundation of the Internet. IP hosts on the Internet are identified by IP addresses. There are a large number of routers on the Internet that are responsible for data IP addresses to select appropriate paths to forward data packets . Datagrams are sent from source hosts on the Internet to The purpose clause often goes through dozens of routers. A router is a network device working on the third layer, and it also functions as a switch. It can forward data packets between different link layer interfaces. Therefore, the router needs to remove the incoming data packets from the network layer and the link layer. header and repackage.

The network layer is responsible for point-to-point transmission (point refers to a host or router), while the transport layer is responsible for end-to-end transmission (end refers to source and destination hosts) . The transport layer can choose TCP or UDP protocol.

The destination host receives the data packet, and finally reaches the application through the songcheng protocol stack:

The Ethernet driver first determines whether the payload of the data frame (payload, refers to the data actually transmitted except the protocol header) is a datagram of the IP, ARP or RARP protocol according to the "upper layer protocol" field in the Ethernet header, and then submits the datagram. Deal with the corresponding protocol. If it is an IP datagram, the IP protocol determines whether the payload of the datagram is TCP, UDP, ICMP or IGMP according to the "upper layer protocol" field in the IP header, and then hands it to the corresponding protocol for processing. If it is a TCP segment or a UDP segment, the TCP or UDP protocol then determines which user process the application layer data should be delivered to according to the "port number" field of the TCP header or UDP header . An IP address is an address that identifies different hosts in the network, and a port number is an address that identifies different processes on the same host. The combination of the IP address and the port number identifies a unique process in the network.

ARP and RAPR belong to the link layer, IP belongs to the network layer, ICMP, IGMP and IP belong to the network layer, and TCP and UDP belong to the transport layer.

 

4. Protocol format

1. Packet encapsulation

The transport layer and the mechanisms below it are provided by the kernel, the application layer is provided by the user process (how to use the socket API to write applications will be introduced later), the application program interprets the meaning of the communication data, and the transport layer and its below deal with the details of communication , which sends data from one computer to another through a certain path. When application layer data is sent to the network through the protocol stack, each layer protocol must add a data header (header), called encapsulation, as shown in the following figure:

Different protocol layers have different names for data packets, which are called segments at the transport layer , datagrams at the network layer , and frames at the link layer . The data is encapsulated into frames and sent to the transmission medium. After reaching the destination host, each layer of the protocol strips the corresponding header, and finally the application layer data is handed over to the application for processing.

2. Ethernet frame format (link layer)

The frame format of Ethernet is as follows:

The source address and destination address refer to the hardware address (also called MAC address) of the network card. The length is 48 bits, which are solidified when the network card leaves the factory. You can use the ifconfig command in the shell to view, the "HWaddr 00:15:F2:14:9E:3F" part is the hardware address. The protocol field has three values, corresponding to IP, ARP, and RARP respectively. The end of the frame is the CRC check code. 

The data length in the Ethernet frame specifies a minimum of 46 bytes and a maximum of 1500 bytes. The length of the ARP and RARP data packets is less than 46 bytes, and padding bits should be added later. The maximum value of 1500 is called the maximum transmission unit (MTU) of Ethernet. Different network types have different MTUs. If a packet is routed from Ethernet to the dial-up link, the packet length is greater than the MTU of the dial-up link. Fragmentation of packets. There is also "MTU: 1500" in the ifconfig command output. Note that the concept of MTU refers to the maximum length of the payload in the data frame, excluding the length of the frame header.

3. ARP datagram format

In the era of network communication, the application program of the source host knows the IP address and port number of the destination host, but does not know the hardware address of the destination host, and the data packet is first received by the network card and then processed by the upper-layer protocol. If the received data If the hardware address of the packet does not match the local machine, it will be discarded directly. Therefore, the hardware address of the destination host must be obtained before communication. ARP protocol plays this role. The source host sends an ARP request, asking "what is the hardware address of the host whose IP address is 192.168.0.1", and broadcasts the request to the local network segment (the hardware address in the header of the Ethernet frame is filled with FF:FF:FF:FF:FF :FF means broadcast), the destination host receives the broadcast ARP request and finds that the IP address is consistent with the local machine, then sends an ARP response packet to the source host, and fills in its own hardware address in the response packet.

Each host maintains an ARP buffer table, which can be viewed with the arp-a command. Items in the buffer table have an expiration time.

4. IP segment format

The header length and data length of IP datagrams are variable lengths, which are always integer multiples of 4 bytes. TTL (Time to live) is used like this: the source host sets a time-to-live for the data packet, such as 64, and the value is reduced by 1 every time a router passes. If it is reduced to 0, it means that the route has been too long and still cannot be found. To the network of the destination host, the packet is discarded, so the unit of this lifetime is not seconds, but hops . The protocol field indicates whether the upper layer protocol is TCP, UDP, ICMP or IGMP. Then there is the checksum, only the IP header is checked, and the verification of the data is responsible for the higher layer protocol.

5. UDP datagram format

UDP header: The source port number is the client port number, and the destination port number is the port number of the TFTP service.

The general network is like TFTP. The two sides of the communication are the client and the server. The client actively initiates the request, while the server passively waits, receives and responds to the request. The client's IP address and port number uniquely identify the TFTP client process on the host, and the server's IP address and port number uniquely identify the host's TFTP service process. Since the client is the party that initiates the request, he must know the server. Therefore, some common network protocols have default server ports, such as HTTP service default TCP protocol port 80, FTP service default TCP protocol port 21, TFTP service default UDP protocol used Port 69. When using the client program, the host name or IP address of the server must be specified. If the port number is not specified, the default port is used.

Many services have a well-known port number, but the port of the client program does not have to be well-known. Often, a free port number is automatically allocated by the system every time the client program is run, and it is released when it is used up, which is called Port number for ephemeral. Because when the client connects to the server, it must clearly know the IP and port number of the server, and the IP and port number of the client will be obtained by the server when connecting.

6. TCP datagram format

The UDP protocol also has a source port number and a destination port number, and the communication parties are identified by the IP address and port number. 32-bit serial number, 32-bit confirmation serial number, and the window size will be explained in detail later. The 4-bit header length is similar to the IP protocol header, indicating that the length of the TCP protocol header is in units of 4 bytes. Therefore, the maximum length of the TCP protocol header can be 4*15 = 60 bytes.

 

5. NET mapping and hole punching mechanism

1. NET mapping

There is a NET mapping table in the switch: the correspondence between the local area network IP and the public network IP that will be connected to the current router terminal.

Using the NAT protocol, computers on the local area network can access computers on the Internet, but computers on the Internet cannot access computers on the local area network.

2. Hole punching mechanism

Assuming two buttons A and B, their communication needs to pass through Tencent's server. When A sends a message, it needs to be forwarded to B through the Tencent server. However, the real-time nature of communication cannot be guaranteed by using this method, such as video communication, which requires high real-time performance. If they communicate directly, the real-time performance will be much higher. But the router has a protection mechanism (for the first time the data packet sent by the unfamiliar IP will be blocked or discarded, mainly to prevent malicious attacks on the network). If A wants to communicate with B, the IPs of the data packets carried must be familiar with each other, which requires a hole to be punched between the two routers, and the server performs this operation.

Implementation process: When you log in to QQ, you will access the Tencent server, and the Tencent server will also return a data packet, which will carry the public IP of the Tencent server. Relatively speaking, the park IP is familiar to A and B. The server uses the public IP to help A and B complete the hole punching (hole punching is to realize a kind of access). When it punches the hole, A and B can communicate in real time. Hole punching is done by the server, and the ultimate purpose is to increase the data transmission rate.

UDP hole punching: The so-called UDP hole punching means that client A sends data packets to the server through the UDP protocol. After the server receives the data packets, it can obtain the address and port number of client A. Similarly, after client B sends the UDP data packet to the server, the server also obtains the address and port number of B after receiving the data packet sent by B, and sends the addresses and port numbers of A and B to the other party respectively, so that both parties can You can continue to communicate with the UDP protocol. For some applications or requirements, two clients need to do some communication temporarily, which can be done without establishing TCP.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469925&siteId=291194637