Reference: UDP protocol features and UDP header structure reference: TCP and UDP comparison reference: TCP_Header

TCP/IP network model

For computers and network devices to communicate with each other, they must be based on the same method. For example, rules on how to detect the communication target, which side initiates the communication first, which language to use, and how to end the communication need to be agreed in advance. Communication between different hardware, operating systems, all of this requires a rule. This rule is called a protocol.

TCP/IP is a general name for various Internet-related protocols, such as TCP, UDP, IP, FTP, HTTP, ICMP, and SMTP, which belong to the TCP/IP family.

The TCP/IP model is the foundation of the Internet. It is the general name for a series of network protocols. These protocols can be divided into four layers: link layer, network layer, transport layer, and application layer.

  • Link layer: encapsulates and unencapsulates IP packets, sends and receives ARP(Address resolution Protocol) and RARP(Reverse Address translation Protocol) packets.
  • The network layer is responsible for routing and sending packet packets to the target network or host.
  • Transport layer: groups or regroups packets and encapsulates them in TCP or UDP format.
  • Application layer: responsible for providing applications to users, such as HTTP, FTP, Telnet, DNS, SMTP, and so on.

In the network architecture, the establishment of network communication must be carried out in the peer layer of communication parties, and can not be staggered. In the entire data routing process, the protocol header and protocol tail of the corresponding layer must be attached when the data passes through each layer at the sending end (only the protocol tail needs to be encapsulated at the data link layer), that is, the data must be encapsulated by protocol to identify the communication protocol used by the corresponding layer.

UDP

UDP is a connectionless protocol used to process data packets like TCP on the network. In the OSI model, it is at layer 4, the transport layer, which is one layer above the IP protocol. UDP does not provide packet grouping, assembly, and sorting. That is, after a packet is sent, it is impossible to know whether the packet arrived safely and intact.

He has the following characteristics:

  1. Connectionless First of all, UDP does not need to establish a connection through three handshakes before sending data like TCP. If you want to send data, you can start sending data. UDP is only a data packet porter, and does not split and splicing tasks for library packets. Specifically to lock is:

    • At the sending end: The application layer passes data to the UDP protocol at the transport layer. UDP adds a UDP header to the data to identify the UDP protocol below, and then passes the data to the network layer.
    • At the receiving end: The network layer transmits data to the UDP protocol at the transport layer. UDP only removes IP packets and sends them to the application layer without any splicing operation.
  2. UDP not only supports one-to-one transmission mode, but also supports one-to-many, many-to-many, and many-to-many modes. In other words, UDP provides unicast, multicast, and broadcast functions.

  3. UDP packets are delivered by the UDP program of the sender. After adding a UDP header identifier, the packets are delivered to the IP layer. UDP packets transmitted by the corresponding layer are neither merged nor split, but the boundaries of these packets are retained. Therefore, the program must select the appropriate size of the message.

  4. Unreliability first of all, unreliability is reflected in the absence of connection, communication does not need to be connected, want to send, such a situation is certainly not reliable.

    The data is transmitted as it is received, without backing up the data, and without caring whether the data has been received correctly.

    The on-board network environment is up and down, but UDP always sends data at a constant speed because it has no congestion control. Real-time network conditions are not good, and the sending rate will not be adjusted. The disadvantage of this implementation is that packet loss may occur in the case of poor network conditions. However, it is also obvious that UDP is used instead of TCP in some real-time scenarios (such as video and voice calls).



As can be seen from the dynamic diagram above, UDP only sends data to the other party without caring whether the data arrives safely and intact.

  1. The header cost is low and the transmission of data packets is very efficient

The functions of each part in the header structure are as follows: – 16-bit Source port Number Records the source port number, which is selected when the peer party needs to reply. The value can be 0 to 16 bits if not required. Destination port Number Records the destination port number. This must be used when the endpoint delivers the message. – 16-bit UDP datagram length (including data and header). The minimum value is 8B (if only the header has no data). – the checksum checks whether the UDP datagram has errors during transmission. This field is optional. If the source host does not want to calculate the checksum, set all the fields to 0. When the transport layer receives a UDP packet from the IP layer, it sends the UDP packet to the process through the corresponding port based on the destination port in the header. If the UDP of the receiving party finds that the destination port number in the received packet is incorrect (that is, no application process with the corresponding port number exists), the UDP of the receiving party discards the packet and sends an ICMP port Unreachable error message to the sender.

Because UDP header overhead is small, only eight sub-sections, compared with TCP at least 20 sub-sections, in the transmission of data packets is very efficient.Copy the code

TCP

When one computer wants to communicate with another, the two computers need to be smooth and reliable so that they can send and receive data correctly.

TCP (Transmission Control Protocol) is an object-oriented connection, reliable, byte stream based transport layer control protocol, defined by THE Internet Engineering Task Force (IETF) RFC 793. TCP is a connection-oriented, reliable streaming protocol. A flow is an uninterrupted data structure that can be understood as the flow of water in a drainpipe.

  1. The connection process is shown in the figure, and the TCP connection establishment process is as follows:

    • First handshake The client sends a connection request packet segment to the server. The paragraph contains its own initial data communication sequence number. After the request is SENT, the client enters the SYN-sent state.

    • After the server receives the connection request packet segment, if the server agrees to the connection, it sends a reply containing its initial data communication sequence number and enters the SYN-received state.

    • Third handshake The client sends an acknowledgement packet to the server after receiving the photocopy of the connection approval. The client enters the ESTABLISHED state after sending the packet. The server also enters the ESTABLISHED state after receiving the shadow order. In this case, the connection is ESTABLISHED successfully.

  2. disconnectTCP is duplex. FIN and ACK occur on both ends when the connection is disconnected.

    • First handshake If client A considers the data transmission complete, it needs to send A connection release request to server B.
    • After receiving the connection release request, USER B tells the application layer to release the TCP connection. Then the ACK packet is sent and the state CLOSE_WAIT is entered. At this time, the connection from SURFACE A to surface B has been released and the data sent by surface A is no longer received. But because the TCP connection is bidirectional, B can still send data to A.
    • For the third handshake, user B will continue to send if the data has not been sent, and then send A connection release request to User A. Then, user B enters the last-ACK state.
    • Fourth handshake After receiving the release request, user A sends A confirmation reply to user B. User A enters the time-wait state. The state lasts for 2MSL (maximum segment lifetime, which refers to the duration of the packet segment in the network. The timeout will be discarded). If there is no resending request from B within this period, the state is CLOSED. When B receives the confirmation reply, it also enters the CLOSED state.
  3. Protocol characteristics

    • Connection-oriented connections are established at both ends before data is sent. The method of establishing a connection is the “three-way handshake”, which establishes a reliable connection. Establish the connection, and lay the foundation for the reliable transmission of data.
    • Unicast transmission Only A TCP connection consists of only two endpoints for point-to-point data transmission. Multicast and broadcast transmission modes are not supported.
    • Byte stream oriented TCP does not transmit packets independently like UDP, but transmits packets in byte stream mode without preserving packet boundaries.
    • Reliable transmission For reliable transmission, packet loss and error codes are determined by the TCP segment number and confirmation number. To ensure the reliability of packet transmission, TCP assigns a serial number to each packet, and the serial number also ensures that the entity packets sent to the receiving end are received in sequence. The receiving entity then sends back an acknowledgement (ACK) of the received byte; If the sending entity does not receive acknowledgement within a reasonable round trip delay (RTT), the corresponding data (if lost) will be retransmitted.
    • Congestion control When a network is congested, TCP can reduce the rate and quantity of data injected into the network to alleviate congestion
    • TCP provides full-duplex communication TCP allows applications on both sides of the communication to send data at any time because there is no cache at either end of the TCP connection to temporarily store two-way communication data. Of course, TCP can send a segment immediately, or it can cache for some time to send more segments at a time (the maximum segment size depends on MSS(maximum packet length)).

Comparison of TCP and UDP

  1. contrast
UDP TCP
Whether connection There is no connection connection-oriented
reliable Unreliable connection, not using flow control and congestion control Reliable connection, using flow control and congestion control
Number of connected objects Supports one-to-one, one-to-many, many-to-one, and many-to-many interactive communication Only one-to-one communication is supported
transport For a message Word oriented stream
The first overhead The overhead on the front is only 8B subsections The tip has a minimum of 20 segments and a maximum of 16 segments
Applicable scenario Applicable to real-time scenarios (voice, video call, live broadcast, etc.) Suitable for applications that require reliable transmission, such as file transfer
  1. conclusion
    • TCP provides reliable connection-oriented services to the upper layer, while UDP provides connectionless unreliable services to the upper layer
    • Although UDP is not as accurate as TCP transmission, it can also be used in many places where real-time requirements are high
    • TCP can be used if the data accuracy is high and the speed is relatively slow