What is a UDP protocol
The full name of the UDP protocol is the user datagram, which is used in the network as a TCP protocol for processing packets. In the OSI model, at layer fourth, the transport layer, is at the upper level of the IP protocol. UDP has the disadvantage of not providing datagram grouping, assembling and sorting packets, that is, when a message is sent, it is not possible to know whether it has arrived safely or completely.
Why to use UDP
When choosing to use protocols, you must be cautious in choosing UDP. In the environment of unsatisfactory network quality, UDP packet loss is more serious. However, because of the characteristics of UDP: It is not a connection-type protocol, and therefore has the advantage of small resource consumption, fast processing, so usually audio, video and ordinary data in the transfer of UDP more, because they even occasionally lost one or two packets, will not have a significant impact on the reception results. For example, we chat with ICQ and OICQ is the use of UDP protocol.
Manipulating UDP in Java
User data packets can be easily controlled using the Datagramsocket and Datagrampacket classes located under the Java.net package in the JDK.
Before you describe them, you must understand the InetAddress class that is located in the same location. InetAddress implements the java.io. Serializable interface and does not allow inheritance. It is used to describe and wrap an Internet IP address, returning the InetAddress instance in three ways:
Getlocalhost (): Returns an instance of the encapsulated local address.
Getallbyname (String Host): Returns an array of inetaddress instances that encapsulate the host address.
Getbyname (String Host): Returns an instance that encapsulates a host address. Where the host can be a domain name or a legitimate IP address.
The Datagramsocket class is used to create socket instances that receive and send UDP. And the socket class relies on the SocketImpl class, the implementation of the Datagramsocket class relies on the Datagramscoketimplfactory class specifically designed for it. The Datagramsocket class has 3 builders:
Datagramsocket (): Creates an instance. This is a special usage, usually for client-side programming, which does not have a specific listening port, and uses only a temporary one.
Datagramsocket (int port): Creates an instance and fixed a message that listens on port ports.
Datagramsocket (int port, inetaddress localaddr): This is a very useful builder, when a machine has more than one IP address, the instance created by it only receives messages from LOCALADDR.
It is noteworthy that when creating an instance of the Datagramsocket class, if the port is already in use, a SocketException exception is thrown, causing the program to terminate illegally, and this exception should be noted for capture. The main methods of the Datagramsocket class are 4:
Receive (Datagrampacket D): Receives data packets into D. The Receive method produces a "block".
Send (Datagrampacket D): Send a delivery paper D to the destination.
Setsotimeout (int timeout): Sets the timeout in milliseconds.
Close (): Closes Datagramsocket. When the application exits, the resource is usually actively released and the socket is closed, but the unexpected exit may cause the resource to be unable to recycle. Therefore, you should actively use this method to close the socket when the program completes, or close the socket after the exception is caught.
"Blocking" is a professional noun that produces an internal loop that causes the program to pause in this place until a condition is triggered.
The Datagrampacket class is used to process messages, which package byte arrays, destination addresses, destination ports, and other data into packets or disassemble packets into byte arrays. Applications in generating packets should be aware that TCP/IP requires a maximum of 65,507 packets of data, usually host 548 bytes, but most platforms can support 8192 byte size of the message. There are 4 builders of the Datagrampacket class:
Datagrampacket (byte[] buf, int length, inetaddress addr, int port): From the BUF array, take the length of the data to create the packet object, the target is the addr address, port ports.
Datagrampacket (byte[] buf, int offset, int length, inetaddress address, int port): From the BUF array, take the length of data from the offset start to create the packet object , the destination is the addr address, port.
Datagrampacket (byte[] buf, int offset, int length): Load data in the packet from offset start, length length into the buf array.
Datagrampacket (byte[] buf, int length): Load the length of data in the packet into the BUF array.
The most important method of the Datagrampacket class is the GetData (), which obtains the byte array encoding of the message from the instance.