Home >Java >javaTutorial >A brief introduction to udp protocol in Java
This article mainly introduces relevant information about the introduction of UDP protocol in detail, which has certain reference value. Interested friends can refer to it
1. Brief introduction to UDP
UDP is a transport layer protocol and is in the same layer as the TCP protocol. However, unlike the TCP protocol, the UDP protocol does not provide timeout retransmission, error retransmission and other functions, which means that it is an unreliable protocol.
2.UDP protocol header
UDP port number
Since many software need to use the UDP protocol, the UDP protocol must use a certain flag to distinguish the data packets required by different programs. This is the function of the port number. For example, if a certain UDP program A registers port 3000 in the system, then all UDP packets with destination port number 3000 that come in from the outside will be handed over to this program. Theoretically, the port number can be as many as 2^16. Because its length is 16 bits
UDP checksum
This is an optional option and Not all systems perform checksums on UDP packets (compared to the requirements of the TCP protocol), but the standard in the RFC requires that the sender should calculate the checksum.
The UDP checksum covers the UDP protocol header and data, which is different from the IP checksum. The IP protocol checksum only covers the IP data header, not all data. Both UDP and TCP contain a pseudo header, which is filmed for the purpose of calculating the checksum. The pseudo header even contains information that is included in the IP protocol such as the IP address. The purpose is to allow UDP to check twice whether the data has reached the destination correctly. If the sender does not turn on the checksum option and the receiver makes an error in calculating the checksum, the UDP data will be quietly discarded (delivery is not guaranteed) without generating any error messages.
UDP length
UDP can be very long, as long as 65535 bytes. However, when the general network is transmitting, it generally cannot transmit such a long protocol at one time (involving MTU issues), so it has to fragment the data. Of course, these are transparent to upper-level protocols such as UDP, and UDP does not need to care about the IP protocol layer. Regarding how to shard data, the next chapter will briefly discuss some sharding strategies.
IP Fragmentation
After IP receives data from the upper layer, it must determine which data is coming from based on the IP address. The interface sends data (through routing) and queries the MTU. If the data size exceeds the MTU, the data is fragmented. The fragmentation of data is transparent to the upper and lower layers, and the data will only be reassembled when it reaches the destination. But don't worry, the IP layer provides enough information for reassembly of the data.
In the IP header, the 16-bit identification number uniquely records the ID of an IP packet, and IP slices with the same ID will be reassembled; while the 13-bit slice offset records a certain IP The position of the fragment relative to the entire packet; and the two 3-bit flags in the middle indicate whether there are new fragments after the fragment. These three indicators constitute all the information of IP fragmentation, and the recipient can use this information to reorganize the IP data (even if the later fragments arrive before the previous fragments, this information is enough).
Because fragmentation technology is frequently used on the Internet, software and people who forge IP fragmentation packets to conduct rogue attacks emerge in endlessly.
You can use the Trancdroute program to perform simple MTU detection. Please refer to the textbook.
Interactive use between UDP and ARP
This is not often used A detail that people have noticed, this is for some system implementations. When the ARP cache is still empty. Before UDP is sent, an ARP request must be sent to obtain the MAC address of the destination host. If the UDP data packet is large enough and the IP layer must fragment it, imagine that the UDP data packet The first fragment will issue an ARP query request, and all fragments will wait until the query is completed before sending it. Is this actually the case?
The result is that some systems will cause each fragment to send an ARP query, and all fragments are waiting. However, when receiving the first response, the host only sends the last data fragment and discards it. Otherwise, this is really incredible. In this way, because the fragmented data cannot be assembled in time, the receiving host will discard the IP data packets that can never be assembled within a period of time, and send an ICMP message with an assembly timeout (in fact, many systems do not generate this error) to ensure that The receiving host's own sink buffer is not filled with fragments that never get assembled.
ICMP source station suppression error
When the processing speed of the target host cannot keep up with the speed of data reception, because the receiving host The IP layer cache will be full, so the host will send an ICMP message "I can't stand it".
UDP server design
# Certain characteristics of the UDP protocol will affect our server program design, a rough summary as follows:
1. About the client IP and address: The server must have the ability to determine whether the data packet is legal based on the client IP address and port number (this seems to be required by every server)
2. About the destination address: The server must The ability to filter broadcast addresses is required.
3. Regarding data input: Usually each port number of the server system corresponds to an input buffer. Incoming input waits for processing by the server on a first-come, first-served basis, so buffer overflow problems will inevitably occur. In this case , UDP packets may be dropped without the application server program itself being aware of the problem.
4. The server should restrict the local IP address, which means it should be able to bind itself to a certain port of a certain network interface.
The above is the detailed content of A brief introduction to udp protocol in Java. For more information, please follow other related articles on the PHP Chinese website!