Article catalog
I UDP communicator
1. UDP sending data
- UDP communication in Java
UDP protocol is an unreliable network protocol. It establishes a Socket object at both ends of the communication, but the two sockets are only objects for sending and receiving data. Therefore, for both sides of communication based on UDP protocol, there is no so-called concept of client and server
Java provides DatagramSocket class as a Socket based on UDP protocol
- Construction method

- correlation method

- To send data
- Create a Socket object (DatagramSocket) at the sending end
- Create data and package it
- Call the method of DatagramSocket object to send data
- Close sender
- Code demonstration
public class SendDemo { public static void main(String[] args) throws IOException { //Create a Socket object (DatagramSocket) at the sending end // Datagram socket() constructs a datagram socket and binds it to any available port on the local host DatagramSocket ds = new DatagramSocket(); //Create data and package it //DatagramPacket(byte[] buf, int length, InetAddress address, int port) //Construct a packet and send the packet with length to the specified port number on the specified host. byte[] bys = "hello,udp,I'm coming.".getBytes(); DatagramPacket dp = new DatagramPacket(bys,bys.length,InetAddress.getByName("127.0.0.1"),10086); //Call the method of DatagramSocket object to send data //Void send (datagram packet P) sends datagram packets from this socket ds.send(dp); //Close sender //void close() closes the datagram socket ds.close(); } }
2.UDP receiving data
- To receive data
- Create a Socket object (datagram Socket) at the receiving end
- Create a packet to receive data
- Call the method of DatagramSocket object to receive data
- Parse the data package and display the data on the console
- Close the receiver
- Construction method

- correlation method

- Sample code
public class ReceiveDemo { public static void main(String[] args) throws IOException { //Create a Socket object (datagram Socket) at the receiving end DatagramSocket ds = new DatagramSocket(12345); //Create a packet to receive data byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //Call the method of DatagramSocket object to receive data ds.receive(dp); //Parse the data package and display the data on the console System.out.println("The data are:" + new String(dp.getData(), 0, dp.getLength())); } } }
3.UDP communication program practice
- Case requirements UDP sending data: the data comes from keyboard input until the input data is 886, and the sending data ends UDP receiving data: because the receiving end does not know when the sending end stops sending, it adopts dead loop receiving
- code implementation
/* UDP Send data: The data comes from the keyboard input until the input data is 886, and the sending data ends */ public class SendDemo { public static void main(String[] args) throws IOException { //Create a Socket object (DatagramSocket) at the sending end DatagramSocket ds = new DatagramSocket(); //Keyboard input data Scanner sc = new Scanner(System.in); while (true) { String s = sc.nextLine(); //The input data is 886, and the sending data ends if ("886".equals(s)) { break; } //Create data and package it byte[] bys = s.getBytes(); DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.1.66"), 12345); //Call the method of DatagramSocket object to send data ds.send(dp); } //Close sender ds.close(); } } /* UDP Receive data: Because the receiving end does not know when the sending end will stop sending, it adopts dead cycle reception */ public class ReceiveDemo { public static void main(String[] args) throws IOException { //Create a Socket object (datagram Socket) at the receiving end DatagramSocket ds = new DatagramSocket(12345); while (true) { //Create a packet to receive data byte[] bys = new byte[1024]; DatagramPacket dp = new DatagramPacket(bys, bys.length); //Call the method of DatagramSocket object to receive data ds.receive(dp); //Parse the data package and display the data on the console System.out.println("The data are:" + new String(dp.getData(), 0, dp.getLength())); } //Close the receiver // ds.close(); } }
4. Three communication modes of UDP
- unicast Unicast is used for end-to-end communication between two hosts
- Multicast Multicast is used to communicate with a specific group of hosts
- radio broadcast Broadcast is used for data communication between one host and all hosts on the whole LAN
5.UDP multicast implementation
- Implementation steps
Sender
Create a Socket object (DatagramSocket) at the sending end
Create data and package the data (datagram packet)
Call the method of DatagramSocket object to send data (in unicast, it is sent to the computer with the specified IP, but in multicast, it is sent to the multicast address)
Release resources
receiving end
Create receiver Socket object (MulticastSocket)
Create a box to receive data
Bind the current computer to a multicast address
Receive data into box
Parse the packet and print the data
Release resources
- code implementation
// Sender public class ClinetDemo { public static void main(String[] args) throws IOException { // 1. Create the Socket object (DatagramSocket) of the sender DatagramSocket ds = new DatagramSocket(); String s = "hello Multicast "; byte[] bytes = s.getBytes(); InetAddress address = InetAddress.getByName("224.0.1.0"); int port = 10000; // 2. Create data and package the data (datagram packet) DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port); // 3. Call the method of DatagramSocket object to send data (in unicast, it is sent to the computer with the specified IP, but in multicast, it is sent to the multicast address) ds.send(dp); // 4. Release resources ds.close(); } } // receiving end public class ServerDemo { public static void main(String[] args) throws IOException { // 1. Create a receiver Socket object (MulticastSocket) MulticastSocket ms = new MulticastSocket(10000); // 2. Create a box to receive data DatagramPacket dp = new DatagramPacket(new byte[1024],1024); // 3. Bind the current computer to a multicast address to add it to this group ms.joinGroup(InetAddress.getByName("224.0.1.0")); // 4. Receive the data into the box ms.receive(dp); // 5. Analyze the data package and print the data byte[] data = dp.getData(); int length = dp.getLength(); System.out.println(new String(data,0,length)); // 6. Release resources ms.close(); } }
6.UDP broadcast implementation
- Implementation steps
Sender
Create sender Socket object (datagram Socket)
Create a box for storing data and encapsulate the broadcast address
send data
Release resources
receiving end
Create a Socket object (datagram Socket) at the receiving end
Create a packet to receive data
Call the method of DatagramSocket object to receive data
Parse the data package and display the data on the console
Close the receiver
- code implementation
// Sender public class ClientDemo { public static void main(String[] args) throws IOException { // 1. Create the sender Socket object (DatagramSocket) DatagramSocket ds = new DatagramSocket(); // 2. Create a box for storing data and encapsulate the broadcast address String s = "radio broadcast hello"; byte[] bytes = s.getBytes(); InetAddress address = InetAddress.getByName("255.255.255.255"); int port = 10000; DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port); // 3. Send data ds.send(dp); // 4. Release resources ds.close(); } } // receiving end public class ServerDemo { public static void main(String[] args) throws IOException { // 1. Create the Socket object (DatagramSocket) of the receiving end DatagramSocket ds = new DatagramSocket(10000); // 2. Create a data packet DatagramPacket dp = new DatagramPacket(new byte[1024],1024); // 3. Call the method of DatagramSocket object to receive data ds.receive(dp); // 4. Parse the data package and display the data on the console byte[] data = dp.getData(); int length = dp.getLength(); System.out.println(new String(data,0,length)); // 5. Close the receiver ds.close(); } }