[Computer Network] Detailed explanation of UDP protocol

Table of contents

Preface      

Port number expansion

Port number range division

netstat

pidof

UDP protocol

UDP protocol end format

Characteristics of UDP

datagram oriented

UDP buffer

Things to note when using UDP

UDP-based application layer protocol


 

Preface      

  We have finished talking about the http and https protocols earlier . They both belong to the application layer . According to the TCP/IP five-layer model, we should explain the transport layer in the next layer . The transport layer is divided into TCP protocol and UDP protocol. The five-layer model is named after TCP/IP, so the importance of TCP can be imagined. So this chapter and the following chapters will explain TCP and IP in detail, and the differences between them.

Port number expansion

As we said before, the port number (Port) identifies different applications (processes) that communicate on a host . The only process in the world can be located by using the ip address + port number .

        Once you know the ip address, you know the location of the other party's host; as for which application handler to send it to, it is determined according to the port number. Each application handler will have one or more port numbers.

In the TCP/IP protocol, use a five-tuple such as "source IP", "source port number", "destination IP", "destination port number", and "protocol number" to identify a communication (you can view it through netstat -nltp );

Port number range division

        0 - 1023 : Well-known port numbers, HTTP, FTP, SSH and other widely used application layer protocols, their port numbers are fixed.

ssh server, use port 22; ftp server, use port 21;
telnet server, use port 23; http server, use port 80
https server, use 443, etc.

        1024 - 65535: The port number dynamically allocated by the operating system . The port number of the client program is allocated from this range by the operating system. For example, we usually use port 8080 to run our own program, or other ports are fine

We can use the following command to view all well-known port numbers :

cat /etc/services

When we write programs to use port numbers, we should try our best to avoid these well-known port numbers. 

netstat

Netstat is an important tool for viewing network status .

Syntax : netstat [options]
Function : View network status
Common options:

  • nRefuse to display aliases and convert all numbers that can be displayed into numbers
  • l Only list the service status that is listening
  • p displays the name of the program that establishes the relevant link
  • t (tcp) only displays tcp related options
  • u (udp) displays only udp related options
  • a (all) displays all options , LISTEN-related is not displayed by default

For example, what we usually use

netstat -nltp

  That is, display everything as an array, then only display listen-related services, and then display the name of the program connected to tcp.

pidof

Very convenient when viewing the process ID of the server .

Syntax: pidof [process name]
Function: View process id through process name

When we run a process, we can use pidof to find the corresponding pid directly instead of using ps and grep to find it.

We can also use it with xargs , which can use the content of the pipeline as a command line parameter . I also talked about it when I talked about basic linux commands.

For example, when using pipes normally:

cat log.txt | tail -10

This command first outputs the content in log.txt, then sends it to tail as input, and then takes the content of the last 10 lines and outputs it.

xargs:

pidof HttpServer | xargs kill -9

This command will use the pid of the obtained HttpServer as the parameter of kill, which is equivalent to kill -9 pid.

UDP protocol

UDP protocol end format

We first need to know that almost any protocol must first solve two problems:

a. How to separate (encapsulate) b. How to deliver

As for how to solve it, each protocol has a different method, and UDP uses a fixed-length header to solve it.

The data (payload) is the data of the upper application layer , and the above 8 bytes are the UDP header.

The 16-bit UDP length is the sum of the length of the UDP header and the length of the data. Therefore, when extracting data, the 16-bit UDP length-8 bytes is the length of the data.

In this way, the problem is solved:

a. How to separate : Use a fixed-length (8-byte) header to separate the header and data (payload)

b. How to deliver: After separation, deliver upward according to the 16-bit destination port number in the header . Because the process binds the port number, if you know the port number, you know which process to hand it to.

Therefore, this can also explain

a. When we usually write code, the port number usually uses the uint16_t type, because the port number is 16 bits .

b.How does udp extract the complete message? First extract the fixed-length header, and then based on the 16-bit udp length in the header - 8 , the remaining length is the total data length. This also shows that UDP is oriented to datagrams , which will be explained in detail later.

Characteristics of UDP

  • No connection: Transmit directly without knowing the IP and port number of the opposite end, without establishing a connection;
  • Unreliable: no confirmation mechanism, 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: unable to flexibly control the number and quantity of reading and writing data

datagram oriented

The application layer hands over the length of the message to UDP, and UDP sends it as it is, neither splitting nor merging;

For example, using UDP to transmit 100 bytes of data:

        If the sending end calls sendto once and sends 100 bytes, then the receiving end must also call the corresponding recvfrom once to receive 100 bytes; instead of calling recvfrom 10 times in a loop, receiving 10 bytes each time;

In short, I will strictly accept the number of times you send it, no more and no less.

UDP buffer

UDP does not have a real sending buffer. Calling sendto will be directly handed over to the kernel, and the kernel will pass the data to the network layer protocol for subsequent transmission actions;
UDP has a receiving buffer. But this receiving buffer cannot guarantee the received UDP The order of packets is consistent with the order of sending UDP packets ; if the buffer is full, then arriving UDP data will be discarded;

UDP sockets can both read and write. This concept is called full duplex.

Things to note when using UDP

We have noticed that there is a maximum length of 16 bits in the UDP protocol header. That is to say, the maximum length of data that can be transmitted by a UDP is 64K ( including the UDP header).
However, 64K is a very small number in today's Internet environment .If
the data we need to transmit exceeds 64K, we need to manually subpackage at the application layer, send it multiple times, and manually assemble it at the receiving end.

UDP-based application layer protocol

NFS: Network File System
TFTP: Simple File Transfer Protocol
DHCP: Dynamic Host Configuration Protocol
BOOTP: Boot Protocol (for diskless device boot)
DNS: Domain Name Resolution Protocol

Of course, this also includes custom application layer protocols when you write your own UDP program.

At this point, the content of the UDP protocol is explained. Of course, this is relatively simple. TCP is the focus of our explanation. In the next chapter, we will introduce the TCP protocol in detail.

Guess you like

Origin blog.csdn.net/weixin_47257473/article/details/132760509