In some Java programming, we often involve the use of UDP protocol, so today we will elaborate on the use of UDP protocol in Java. First, you need to understand the basic concepts and significance of UDP.
What is UDP?
The full name of UDP protocol is user datagram, which is used to process data packets in the same way as TCP protocol in the network. In the OSI model, the fourth layer is the transport layer, which is the top layer of the IP protocol. UDP has the disadvantages of not providing datagram grouping, assembly, and sorting of data packets. That is to say, when a message is sent, it cannot be known whether it is safe and complete.
Why UDP?
When selecting the protocol, you must be cautious when selecting UDP. In an environment with unsatisfactory network quality, UDP packet loss may be serious. However, due to the characteristics of UDP: it is not a connection protocol, so it has the advantages of low resource consumption and high processing speed. Therefore, most of the audio, video, and common data are transmitted using UDP, because even if one or two packets are occasionally lost, they do not have much impact on the received results. For example, the ICQ and OICQ used for our chat are the UDP protocol.
Manipulating UDP in Java
You can use the initramsocket and initrampacket classes under the Java.net package in JDK to conveniently control user data packets.
Before describing them, you must understand the InetAddress class in the same location. InetAddress implements the Java. io. Serializable interface and does not allow inheritance. It is used to describe and encapsulate an InternetIP address, and returns the InetAddress instance in three ways:
GetLocalhost (): returns the instance that encapsulates the local address.
GetAllByName (Stringhost): returns an array of InetAddress instances that encapsulate the Host address.
GetByName (Stringhost): returns an instance that encapsulates the Host address. The Host can be a domain name or a valid IP address.
The DatagramSocket class is used to create a Socket instance that receives and sends UDP. Similar to Socket dependent SocketImpl, the implementation of the initramsocket class also relies on the initramscoketimplfactory class specifically designed for it. The initramsocket class has three constructors:
DatagramSocket (): creates an instance. This is a special usage. It is usually used for client programming. It does not have a specific listening port and only uses a temporary one.
DatagramSocket (intport): creates an instance and monitors packets on the Port.
DatagramSocket (intport, InetAddresslocalAddr): This is a very useful builder. When a machine has more than one IP address, the instance it creates only receives packets from LocalAddr.
It is worth noting that when creating an initramsocket class instance, if the port is used, a SocketException exception will be thrown and the program is terminated illegally. capture this exception. The main methods of the initramsocket class are as follows:
Receive (DatagramPacketd): receives data packets to d. The receive method generates a "blocking ".
Send (DatagramPacketd): Send the message d to the destination.
SetSoTimeout (inttimeout): Set the timeout time in milliseconds.
Close (): Close DatagramSocket. When an application exits, resources are usually automatically released and the Socket is closed. However, resources cannot be recycled due to abnormal exit. Therefore, when the program is completed, you should take the initiative to use this method to close the Socket, or close the Socket after an exception is caught.
"Blocking" is a specialized term that produces an internal loop that pauses the program in this place until a condition is triggered.
The DatagramPacket class is used to process packets. It packs data such as Byte arrays, destination addresses, and destination ports into packets or disassembles the packets into Byte arrays. When generating data packets, applications should note that TCP/IP requires a maximum of 65507 data packets. Generally, the host receives 548 bytes, but most platforms support 8192 bytes. The initrampacket class has four constructors:
DatagramPacket (byte [] buf, intlength, InetAddressaddr, intport): Extracts long-Length data from the Buf array to create a data packet object. The target is the Addr address and Port.
DatagramPacket (byte [] buf, effecffset, intlength, InetAddressaddress, intport, port.
DatagramPacket (byte [] buf, effecffset, intlength): pack the data starting from Offset and long Length into the Buf array.
DatagramPacket (byte [] buf, intlength): load long data in the data packet into the Buf array.
The most important method of the DatagramPacket class is getData (), which obtains the Byte array encoding of packets from the instance.