Home > Java > javaTutorial > body text

Detailed introduction to java UDP communication

王林
Release: 2019-11-26 14:32:51
forward
3135 people have browsed it

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()
Copy after login

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)
Copy after login

Create an instance and monitor the packets of the Port port.

DatagramSocket(int port, InetAddress laddr)
Copy after login

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)
Copy after login

The port and address are specified in the bindaddr object.

Commonly used functions:

receive(DatagramPacket p)
Copy after login

Receive data packets into p. The receive method is blocking. If no datagram packet is received, it will be blocked.

send(DatagramPacket p)
Copy after login

Send message p to the destination.

setSoTimeout(int timeout)
Copy after login

Set the timeout in milliseconds.

close()
Copy after login

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)
Copy after login

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)
Copy after login

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[]
Copy after login

Get it from the instance The byte array encoding in the message.

setData(byte[] buf, int offset, int length)
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template