9. Network protocols-TCP and UDP protocols

1. Overview of TCP protocol

TCP overview

TCP (Transmission Control Protocol ) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, defined by IETF's RFC 793. In the simplified computer network OSI model, it completes the fourth layer (host to host, such as computer to server protocol, 1. Similar to a point-to-point protocol, there is only one arrival path, and data packets are generally not lost. 2 .The order in which packets arrive is the same as the order in which they are sent) Function specified by the transport layer, User Datagram Protocol (UDP) is another important transport protocol within the same layer.

TCP message structure

The first 20 bytes of TCP are more important

 TCP message structure

[Source port: Identifies the sender's application process. Destination port: identifies the receiving application process.
[Sequence number: Ensure the orderliness of data transmission (send according to sequence number 1, 2, 3, 4 and so on). Confirmation number: Confirm the received data (confirm the sequence number you hope to receive from the origin next time. If the sequence number received by the host is consistent with the sequence number you intend to receive next time, it proves that it is There is no problem. If it is inconsistent, it proves that the data packet is lost)
[Flag field (8 bits)
ACK‒ confirmation number flag, set to 1 to indicate that the confirmation number is valid, indicating that specific data from the peer has been received
RST‒ reset flag, set to 1 to indicate rejection Wrong and illegal data packets, reset the wrong connection
SYN‒ synchronization sequence number flag, set to 1 to indicate the synchronization sequence number, used to establish the connection
FIN‒ end flag, set to 1 to indicate that the connection will be disconnected, used to tear down the connection
[Option field (can Option)
MSS maximum segment length (MSS information will be included when sending the first SYN packet to tell the other party the maximum length of the message it hopes to receive). By setting this bit, negotiate the size of the TCP data that can be carried.

host to host layer

When two hosts AB establish a connection, A carries Seq, Ack and 9 bytes of data. When B responds, it carries its own sequence number SEQ=1 and confirmation number Ack=10 (because a sent 9 bytes, so it is hoped that a will start sending from 10 bytes next time) and the length of its own data 20. A.

 

 

 • The source port is randomly assigned, and the destination port uses a well-known port (for example, the WEB port is 80);
• The source port number used by the application client is generally a random port that is unused in the system and is greater than 1023;
• Purpose The port number is the listening port of the service opened on the server. For example, HTTP uses 80 by default.

As can be seen from the figure, when B receives the data, it converts the source port of the other end to its own destination port. TCP ensures the reliability of the transmission.

2. TCP protocol three-way handshake and four-way wave (to ensure reliability)

 Transport mechanism--TCP three-way handshake

Note that it is three times. Only three times can the connection be established.

Transport mechanism—TCP four waves 

 Disconnect: Both parties need to agree to disconnect before the connection can be disconnected

 byte stream oriented

 

 Flow control—sliding window mechanism

 

 Reliable Transport—Stop-Wait Protocol

 congestion control

 

Congestion: During a certain period of time, if our demand on the network exceeds what the resources can provide, and network performance changes, congestion occurs. Prevent overloading caused by too much data.

Pass quickly, start slowly, recover quickly

 RST flag

The normal way to close a TCP connection is the four-way handshake. But is it true that only a four-way handshake can close a TCP connection?
RST flag bit
Function: Used to reset an erroneous connection caused by some reason, and also used to reject illegal data and requests. If the RST bit is received,
some error usually occurs.
RST may be set by the receiver or intermediate device.
Reasons why RST is set
‒ The server port is not open (listen)
‒ The server responds too slowly and the user terminates the connection
‒ Network attack
‒ Others

3. Overview of UDP protocol

UDP protocol overview

UDP is the abbreviation of User Datagram Protocol. The Chinese name is User Datagram Protocol. It is
a connectionless transport layer protocol in the OSI (Open System Interconnection) reference model, which provides simple and unreliable transaction-oriented information transmission services. , IETF RFC 768 is the official specification of UDP. The protocol number of UDP in IP packets is 17.

UDP message structure--(applicable to WeChat video)

UDP protocol characteristics 

1. Because UDP is connectionless. There is no need for a complex three-way handshake to establish a connection before transmitting data.
2. When transmitting data, there is no inter-protocol communication traffic (confirmation signal), and there is no need to waste unnecessary processing time (receive the confirmation signal and then send it).
3. After the transmission is completed, there is no need to use a four-way handshake to end the connection.

 Comparative summary of TCP and UDP

TCP applicable scenarios:
When network hardware fails or the burden is too heavy, data packets may be lost, duplicated, delayed, and out of order. These will cause our communication to be abnormal. If the application is responsible for error control, it will undoubtedly bring a lot of complicated work to the programmer. Therefore, it is very necessary for us to use an independent communication protocol to ensure the reliability of communication.
TCP is generally used for file transfer (FTP HTTP has high requirements for data accuracy and can be relatively slow), sending or receiving emails
(POP IMAP SMTP has high requirements for data accuracy, non-emergency applications), remote login (TELNET SSH has high requirements for data accuracy) Sex has certain requirements, the concept of connection) etc.

UDP applicable scenarios:
1. In an efficient and reliable network environment (there is no need to consider problems such as packet loss, disorder, delay, duplication, etc. caused by poor network), because UDP is a connectionless service and does not consume unnecessary network resources (inter-protocol communication in TCP) and processing time (the time expected for acknowledgment), making it much more efficient.
2. In
light-weight communication , when the amount of data to be transmitted is small (can be contained in an IP packet). If we use the TCP protocol, then to establish the connection first, a total of 3 IP data packets need to be sent, and then the data is transmitted. 1 IP data packet generates an IP packet of acknowledgment signal, and then the connection is closed, 5 IP data packets need to be transmitted. . The utilization rate of IP packets using TCP protocol is 1/10. With UDP, only one IP packet needs to be sent. Even if the packet is lost (the service is unsuccessful), you can still apply for the service again (retransmit).
UDP is well suited for this environment where the client transmits simple service requests to the server . Such application layer protocols include TFTP, SNMP, DNS, DHCP, etc.
3. In communications
with strong real-time requirements : In environments with high real-time requirements such as live video broadcasts, a certain amount of packet loss is allowed (live matches, previously lost packets, retransmissions It doesn’t make much sense anymore), UDP is more suitable. (Reliability can be provided through application layer protocols according to specific needs, and it does not need to be as strict as TCP.)

Guess you like

Origin blog.csdn.net/weixin_49765221/article/details/124176727