Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manipulate UDP in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to manipulate UDP in Java". In daily operation, I believe many people have doubts about how to manipulate UDP in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to manipulate UDP in Java"! Next, please follow the editor to study!

Why use UDP

UDP must be chosen carefully when choosing to use the protocol. In the environment where the network quality is not very satisfactory, the packet loss of UDP protocol will be more serious. However, because of the characteristics of UDP: it does not belong to the connection type protocol, so it has the advantages of low resource consumption and fast processing speed, so audio, video and ordinary data usually use UDP when transmitting, because even if they occasionally lose one or two data packets, they will not have much impact on the receiving result. For example, ICQ and OICQ, which we use to chat, use UDP protocol.

How to manipulate UDP in Java

Using the DatagramSocket and DatagramPacket classes located under the Java.net package in JDK, you can easily control user datagrams.

Before you describe them, you must understand the InetAddress classes that are in the same location. InetAddress implements the Java.io.Serializable interface and does not allow inheritance. It is used to describe and wrap an InternetIP address and return an InetAddress instance through three methods:

GetLocalhost (): returns the instance that encapsulates the local address.

GetAllByName (Stringhost): returns an array of InetAddress instances encapsulating Host addresses.

GetByName (Stringhost): returns an instance that encapsulates the Host address. Where Host can be a domain name or a legal IP address.

The DatagramSocket class is used to create Socket instances that receive and send UDP. Just as the Socket class depends on the SocketImpl class, the implementation of the DatagramSocket class depends on the DatagramScoketImplFactory class designed specifically for it. The DatagramSocket class has three builders:

DatagramSocket (): create an instance. This is a special use, usually used in client-side programming, which does not have a specific listening port, only a temporary one.

DatagramSocket (intport): create an instance and listen for messages on the Port port on a fixed basis.

DatagramSocket (intport,InetAddresslocalAddr): 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 worth noting 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 caught. There are four main methods of the DatagramSocket class:

Receive (DatagramPacketd): receives a data message into d. The receive method produces a "block".

Send (DatagramPacketd): sends a message d to the destination.

SetSoTimeout (inttimeout): sets the timeout in milliseconds.

Close (): closes DatagramSocket. When the application exits, it usually releases the resources actively and closes the Socket, but the resources may not be recycled due to abnormal exit. Therefore, you should proactively use this method to close Socket when the program is complete, or close Socket after catching an exception throw.

"blocking" is a technical term that creates an internal loop that pauses the program at this place until a condition is triggered.

The DatagramPacket class is used to process messages. It wraps the Byte array, destination address, destination port and other data into messages or disassembles the messages into Byte arrays. When generating packets, applications should be aware that TCP/IP specifies a maximum of 65507 packets, usually the host receives 548 bytes, but most platforms can support 8192-byte messages. There are four builders for the DatagramPacket class:

DatagramPacket (byte [] buf,intlength,InetAddressaddr,intport): fetch the long data of Length from the Buf array to create a packet object, the target is the Addr address and Port port.

DatagramPacket (byte [] buf,intoffset,intlength,InetAddressaddress,intport): from the Buf array, take the data starting from Offset and the long data of Length to create a packet object. The target is the Addr address and Port port.

DatagramPacket (byte [] buf,intoffset,intlength): loads the data in the packet that starts with Offset and has a long Length into the Buf array.

DatagramPacket (byte [] buf,intlength): loads the data of the Length length in the packet into the Buf array.

The most important method of the DatagramPacket class is getData (), which gets the Byte array encoding of the message from the instance.

At this point, the study of "how to manipulate UDP in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report