Transport layer - detailed explanation of UDP principle

Table of contents

foreword

1. netstat

2.pidof

3. UDP protocol format

4. The characteristics of UDP

5. Datagram-oriented

6. UDP buffer

7. Precautions for using UDP

8. UDP-based application layer protocol

Summarize


foreword

        In the previous article, I introduced the first layer of the network protocol stack is the application layer, including the use of sockets, coding and implementing the server at the application layer, and introduced the HTTP protocol and HTTPS protocol. The main protocols of the transport layer include UDP. The relationship between the protocol and the TCP protocol and the application layer is that the application layer protocol is implemented based on the transport layer protocol. Today we are going to introduce the UDP protocol in the transport layer. Let's take a look at it together below!

1. netstat

netstat is an important tool for viewing network status.
Syntax: netstat [options]
Function: View network status

Commonly used options:
n Refuse to display aliases, and convert all numbers that can be displayed into numbers
l Only list the service status that is in Listen (monitoring)
p Display the program name that establishes a related link
t (tcp) Only display tcp related options
u (udp) Only display udp related options
a (all) display all options, default does not display LISTEN related

2.pidof

It is very convenient to view the process id of the server.
Syntax: pidof [process name]
Function: view the process id through the process name

Description: The above introduces two common tools for viewing the status of network services, and then introduces the UDP protocol

3. UDP protocol format

In the process of network transmission, each layer of transmission contains its own header. When the underlying data is transmitted to the upper layer, it needs to be unpacked and divided. The UDP protocol uses a fixed header.

as the picture shows:

16-bit UDP length, indicating the maximum length of the entire datagram (UDP header + UDP data);
if the checksum is wrong, it will be discarded directly;

How to understand UDP header?

Language level is a kind of structured data

struct udphdr
{
    uint16_t src_port;
    uint16_t dec_port;
    uint16_t length;
    uint16_t check;
};

4. The characteristics of UDP

The process of UDP transmission is similar to sending a letter.

No connection: If you know the IP and port number of the peer, you can directly transmit without establishing a connection;
unreliable: there is no confirmation mechanism and no retransmission mechanism; if the segment cannot be sent to the peer due to network failure, the UDP protocol layer will not give The application layer returns any error information;
datagram-oriented: cannot flexibly control the number and quantity of reading and writing data;

5. Datagram-oriented

The application layer sends UDP the length of the message, and UDP sends it as it is, neither splitting nor merging;
use UDP to transmit 100 bytes of data:
if the sender calls sendto once and sends 100 bytes, then The receiving end must also call the corresponding recvfrom once, receiving 100 bytes; instead of calling recvfrom 10 times in a loop, receiving 10 bytes each time;

6. 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 reporting is the same as the order of sending UDP reports; if the buffer is full, the UDP data that arrives will be discarded;
UDP sockets can be read and written, this concept is called full-duplex

as the picture shows: 

7. Precautions for 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;

8. UDP-based application layer protocol

NFS: Network File System
TFTP: Simple File Transfer Protocol
DHCP: Dynamic Host Configuration Protocol
BOOTP: Boot Protocol (for booting diskless devices)
DNS: Domain Name Resolution Protocol
Of course, it also includes custom application layer protocols when you write UDP programs yourself ;

Summarize

        The above is about the way UDP is implemented at the transport layer. Because UDP is connectionless, packet-oriented, and unreliable, the design of UDP is very simple. How to implement a reliable mechanism is what we will explain in the next article. Don't miss the TCP protocol, everyone is welcome to read it, and we will see you next time!

Guess you like

Origin blog.csdn.net/qq_65307907/article/details/132258244