Home >Java >javaTutorial >Detailed introduction to java UDP communication
Introduction:
UDP is the User Datagram Protocol. To manipulate UDP in java, use the under the
java.net package in the JDK DatagramSocket
and DatagramPacket
classes can conveniently control user data messages.
DatagramPacket
class fills data bytes into UDP packets, which are called datagrams.
DatagramSocket
is used to send this packet. If you accept data, you can accept a DatagramPack object from the DatagramSocket and then read the contents of the data from the package.
UDP is connectionless simplex communication, which is fast.
Online video tutorial recommendation: java online video
1. DatagramSocket class
constructor :
DatagramSocket()
Create an instance, usually used for client programming. It does not have a specific listening port and only uses a temporary one.
DatagramSocket(int port)
Create an instance and monitor the packets of the Port port.
DatagramSocket(int port, InetAddress laddr)
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.
DatagramSocket(SocketAddress bindaddr)
The port and address are specified in the bindaddr object.
Commonly used functions:
receive(DatagramPacket p)
Receive data packets into p. The receive method is blocking. If no datagram packet is received, it will be blocked.
send(DatagramPacket p)
Send message p to the destination.
setSoTimeout(int timeout)
Set the timeout in milliseconds.
close()
Close DatagramSocket. When the application exits, it usually actively releases resources and closes the Socket. However, due to abnormal exit, the resources may not be recycled. Therefore, you should actively use this method to close the Socket when the program is completed, or close the Socket after catching an exception.
2. DatagramPacket class
The DatagramPacket class is used to process messages, packaging byte arrays, target addresses, target ports and other data into message or disassemble the message into a byte array.
Constructor:
DatagramPacket(byte[] buf, int length, InetAddress addr, int port)
Take the data starting from offset and length from the buf byte array to create a data object. The target address is addr and the target port is port.
DatagramPacket(byte buf[], int offset, int length, SocketAddress address)
Take out the data starting from offset and length from the buf byte array to create a data object. The target address is address
Commonly used functions:
getData() byte[]
Get it from the instance The byte array encoding in the message.
setData(byte[] buf, int offset, int length)
Set the data content in the datagram package
3. Communication process of UDP communication
UDP sender:
1. Establish updsocket service.
2. Provide data and encapsulate the data into data packets.
3. Send the data packet through the sending function of the socket service.
4. Close the resource.
UDP receiving end:
1. Define the udpsocket service, which usually listens on a port.
2. Define a data packet to store the received byte data.
3. Store the received data into the defined data packet through the receive method of the socket service.
4. Through the unique functions of the data packet object, these different data are taken out and printed on the console.
5. Close the resource.
For more related questions, please visit the java article tutorial: Getting Started with Java
The above is the detailed content of Detailed introduction to java UDP communication. For more information, please follow other related articles on the PHP Chinese website!