Transport layer protocol - UDP

1. Know the well-known port numbers

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




0-1023: Well-known port numbers, HTTP, FTP, SSH application layer protocols, the port number is fixed
1024-65535: The port number dynamically assigned by the operating system, the port number of the client program is allocated by the operating system. of

2. netstat

Netstat is an important tool for viewing network status

Enter netstat -nltp

n: Refuse to display aliases, and convert all numbers that can be displayed into numbers.


If you enter ltp, you will find that the number will not be displayed, but the corresponding alias
l will be displayed: only view the listen status


Enter netstat -ntp

t: Check only TCP
p: Display more process information


If p is removed, enter netstat -nt

The information in the corresponding PID column will not be displayed.


If you enter netstat -np , basically all the protocols in the system will be found.


Enter netstat -nup to check only udp


Enter netstat -naup to display all udp services

a: Show all entries

3. pidof

It is very convenient to check the process ID of the server on the server.

Enter pidof + process to get the PID of the corresponding process and child process

4. UDP protocol

UDP protocol end format


The payload must be given by the upper layer - the application layer. The width of the UDP header that the upper layer copies the data over through system calls is 0-31, indicating the number of bytes corresponding to the header (4 bytes)


1. How to separate header and payload?
The header is a fixed length of 8 bytes, the rest is the payload

Through the 16-bit UDP length, 8 bytes are subtracted from the whole, which is the length of the payload.


2. How is the payload delivered upward?
The UDP header contains a 16-bit destination port number. A message is sent to the host and delivered to the application layer based on the destination port number. The process that binds the port number


The essence of header (protocol): structured data
There are two data types based on struct, one is structure and the other is bit field

The struct udp_header structure contains the source port number, destination port number, udp length, and checksum.


With the help of bit fields, get the 16 bits from the 4 bytes of the integer


Copy buffer data from the application layer to the operating system

Provide a buffer and define a pointer p to point to the buffer.
Since the header size is a fixed length of 8 bytes, move p backward by 8 units
and then copy the application layer data to the corresponding location.


Then point the pointer p to the beginning position, and force p into the struct udp_header type, pointing to the source port number, destination port number, udp length, and checksum in the structure


The message passed through the network and was received by the other party through the protocol stack.
Then define a pointer s, let s+8 point to the payload again.


Convert s to struct udp_header type, pointing to the source port number, destination port number, udp length, and checksum in the structure

Characteristics of UDP

Connectionless: Transmit directly if you know the IP and port number of the other end, without the need for a connection.
Unreliable: If the network failure segment cannot send to the other end, the UDP protocol 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 understanding

Because UDP has its own fixed header length of 8 bytes, UDP can know the length of its payload
without relying on the upper layer. The bottom layer automatically knows the length of the message and payload,
so it can ensure that the upward delivery must be independent. The complete payload
does not need to be processed by yourself. As long as what is sent is complete, what is received is complete. The other party sends it several times and receives it several times. It is
handed over by the bottom layer. The independent message is called a datagram.

That is, the application layer sends a message of any length to UDP. UDP sends it as it is without splitting or merging it.

Buffer understanding

The data corresponding to UDP must be complete, so the data is directly handed over to the lower layer,
so UDP does not need to send a buffer.

A message is received, but the upper layer has no time to process it, so a receive buffer is needed
. This buffer is not used to ensure reliability, but to ensure that no packets are lost. If the buffer is full, further data will be discarded.
For example: I bought five items on Taobao, numbered 1-5. The item I bought first was item No. 1. The time is uncertain, so it is uncertain who arrived first and who arrived last. This situation is
called When receiving packets out of order,
out-of-order is a sign of unreliability, so UDP does not consider
the UDP receive buffer, but it does not guarantee that the received packets are in order.

Guess you like

Origin blog.csdn.net/qq_62939852/article/details/132840073