[Computer Network] Transport Layer——UDP

transport layer

Transport layer concepts

When learning application layer protocols such as HTTP, for ease of understanding, you can simply think that the HTTP protocol sends requests and responses directly to the network. However, the actual application layer needs to first hand over the data to the transport layer, which further processes the data and then continues to deliver the data downward. This process runs through the entire network protocol stack, and finally the data can be sent to the network.

The transport layer is responsible for reliable transmission, ensuring that data can be reliably delivered to the destination address. In order to facilitate understanding, when learning the transport layer protocol, you can simply think that the transport layer protocol sends data directly to the network.

Let’s talk about port numbers again

Port numbers (Port) identify different applications on a host for network communication. When the host obtains data from the network, it needs to deliver the data from the bottom up. Which application handler this data should ultimately be handed to the upper layer is determined by the destination port number in the data.

When the data obtained from the network is delivered upward, the destination port number corresponding to the data is extracted at the transport layer, and then the data is determined to which service process on the current host the data should be delivered.

Insert image description here
Therefore, the port number belongs to the concept of the transport layer, and the header of the transport layer protocol will contain fields related to the port.

In the TCP/IP protocol, a five-tuple of "source IP address", "source port number", "destination IP address", "destination port number", and "protocol number" is used to identify a communication .

The server identifies a communication through "source IP address", "source port number", "destination IP address", "destination port number", and "protocol number".

  • First extract the destination IP address and destination port number from the data to confirm that the data is sent to the current service process.
  • Then extract the protocol number from the data and provide corresponding types of services for the data.
  • Finally, extract the source IP address and source port number from the data, use them as the destination IP address and destination port number of the response data, and send the response result to the corresponding client process
    Insert image description here
    You can view such five-tuple information through the netstat command.
    -n: Refuse to display aliases, and convert all numbers that can be displayed into numbers.
    -l: List only services in the LISTEN (listening) state.
    -p: Display the name of the program that establishes related links.
    -t(tcp): Only display tcp related options.
    -u(udp): Only display udp related options.
    -a(all): Display all options, LISTEN related is not displayed by default.
    Insert image description here
    The Local Address represents the source IP address and source port number, the Foreign Address represents the destination IP address and destination port number, and Proto represents the protocol type.

Protocol number and port number

The protocol number exists in the IP header and its length is 8 bits. The protocol number specifies which protocol is used for the data carried in the datagram, so that the IP layer of the destination host knows which protocol of the transport layer should deliver the data for processing.

The port number exists in the UDP and TCP headers, and its length is 16 bits. The role of a port number is to uniquely identify a process on a host.

Port number area

The length of port number is16 bits, so the range of port number is 0 ~ 65535:

0 ~ 1023: Well-known port numbers. For example, widely used application layer protocols such as HTTP, FTP, and SSH have fixed port numbers.

1024 ~ 65535: Port numbers dynamically allocated by the operating system. The port number of the client program is assigned by the operating system from this range.

Common port numbers

Some servers are very commonly used, and the port numbers of these servers are generally fixed:

  • ssh server, using port 22.
  • FTP server, using port 21.
  • Telnet server, using port 23.
  • http server, using port 80.
  • https server, using port 443.

We can view the /etc/services file, which records network service names and their corresponding port numbers and protocols.
Insert image description here

pidof

The pidof command can check the process ID through the process name.
Insert image description here

The pidof command can be used with the kill command to quickly kill a process.
Insert image description here

UDP

UDP (User Datagram Protocol) is a protocol on the transport layer of the OSI seven-layer model. It is similar to TCP and is used to transmit data, but UDP is simpler, more efficient, and more flexible. It is suitable for scenarios that require high data transmission speed but low reliability requirements, such as real-time communication scenarios such as games, audio, and video. .

The various interfaces used in network socket programming are a layer of system call interfaces located between the application layer and the transport layer. These interfaces are provided by the system. We can use these interfaces to build upper-layer applications, such as HTTP. We often say that HTTP is based on TCP. In fact, it is because HTTP is built on TCP socket programming.

The transport layer below the socket interface is actually managed by the operating system. Therefore, UDP belongs to the kernel and comes with the protocol stack of the operating system itself. Its code is not written by upper-layer users. All functions of UDP are provided by The operating system is complete, so the network is also part of the operating system.

UDP protocol format

Insert image description here

  • 16-bit source port number: Indicates where the data comes from.
  • 16-bit destination port number: Indicates where the data is going.
  • 16-bit UDP length: Indicates the length of the entire datagram (UDP header + UDP data).
  • 16-bit UDP checksum: If the checksum of a UDP packet is incorrect, the packet will be discarded directly.

Most of the port numbers we see at the application layer are 16 bits. The fundamental reason is that the port numbers in the transport layer protocol are 16 bits.

When we study the protocol, we usually have to figure out two questions:

Question 1: How does UDP separate headers and payloads?

The UDP header contains only four fields, each field is 16 bits long, and a total of 8 bytes. Therefore, UDP actually uses a fixed-length header. When UDP reads the message, after reading the first 8 bytes, the rest is the payload.

Question 2: How does UDP decide which upper layer protocol to deliver the payload to?

There are also many application layer protocols in the upper layer of UDP, so UDP must find a way to hand over the payload to the corresponding upper layer protocol, that is, to the corresponding process of the application layer.

Each network process in the application layer will be bound to a port number. The server process must be explicitly bound to a port number, and the client process is dynamically bound to a port number by the system. UDP finds the corresponding application layer process through the destination port number in the header.

The data structure of the header is actually a structure
Insert image description here

UDP data encapsulation:

When the application layer hands the data to the transport layer, a UDP header type variable will be created in the transport layer, and then each field in the header will be filled in. At this time, a UDP header is obtained.

At this time, the operating system opens up a space in the kernel and copies the UDP header and payload together, forming a UDP message.

Characteristics of UDP protocol

The process of UDP transmission is similar to sending a letter. Its characteristics are as follows:

  • No connection: Data transmission can be carried out directly without establishing a connection if the IP and port number of the peer are known.
  • Unreliable: There is no confirmation mechanism and no retransmission mechanism; if the segment cannot be sent to the other party due to network failure, the UDP protocol layer will not return any error message to the application layer.
  • Datagram-oriented: cannot flexibly control the number and quantity of reading and writing data.
  • Unordered: The UDP protocol is unordered, and the sent data may arrive at the destination address through different paths, so the receiver may not be able to assemble the data in the order in which it was sent.

Datagram-oriented: When the application layer hands over a message of any length to UDP, UDP will send it as it is without splitting or merging it. This is called datagram-oriented.

UDP buffer

  • UDP has no real send buffer. Calling sendto will be handed directly to the kernel, which will pass the data to the network layer protocol for subsequent transmission actions.
  • UDP has a receive buffer. However, this receiving buffer cannot guarantee that the order of received UDP packets is consistent with the order of sent UDP packets; if the buffer is full, the UDP data arriving will be discarded.
  • The UDP socket can both read and write, so UDP is full-duplex.

The role of the receive buffer

If UDP does not have a receiving buffer, then the upper layer is required to read the message obtained by UDP in time. If a message is not read in UDP, then the message data obtained by UDP from the bottom layer will be forced to throw away.

A message is transmitted from one host to another, and host resources and network resources are consumed during the transmission process. If UDP receives a message and is forced to discard a possibly error-free message just because the last received message was not read by the upper layer, this is a waste of host and network resources.

Therefore, UDP itself will maintain a receiving buffer. When a new UDP message arrives, it will put the message into the receiving buffer. At this time, when the upper layer reads data, it will directly read from this receiving buffer. Just read, but if there is no data in the UDP receiving buffer, the upper layer will be blocked when reading. Therefore, the function of the UDP receive buffer is to temporarily save the received messages for the upper layer to read.

Things to note when transmitting data via UDP

It should be noted that the maximum length of UDP in the UDP protocol header is 16 bits, so the maximum length of aUDP message is 64K (contains the size of the UDP header).

However, 64K is a very small number in today's Internet environment. If the data that needs to be transmitted exceeds 64K, it needs to be manually subpackaged at the application layer, sent multiple times, and manually assembled at the receiving end.

Guess you like

Origin blog.csdn.net/Tianzhenchuan/article/details/134068688