UDP---User Datagram Protocol is a simple datagram-oriented transportation layer protocol. UDP does not provide reliability. It just sends datagrams sent by applications to the IP layer, but it does not guarantee that they can reach the destination, nor does it guarantee the order in which data packets arrive. Since UDP does not need to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as timeout retransmission, the transmission speed is very fast.
Implementation of UDP in Java:
* UDP:
* Client:
* 1. Create socket object for UDP communication--DatagramSocket (for sending and receiving UDP data)---Datagram socket
* 2. Prepare data, encapsulation package---DatagramPacket (data package)
* 3. Send data, through the send method
* 4. Close the socket object-socket object
* Server side: Receive data
* 1. Create a socket socket object and bind the port number
* 2. Create package object, create empty array, and prepare to receive data
* 3. Receive data
* 4. Close the resource
* UDP broadcast method:
* All hosts in the same network segment can receive it, provided that the port must listen
* The client sends a broadcast, and the server that enables the port monitoring to receive and print messages
* Implementation of broadcast: broadcast is sent by the client and received by the server
* String host = "255.255.255.255";//Broadcast address-represents all hosts
* 10.0.122.255----Represents that all hosts whose first three network segments are 10.0.122
Code implementation:
Client (send):
import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;public class UDP_client { public static void main(String[] args) throws IOException { //1. Create an object//Construct a datagram socket and bind it to any available port on the localhost. DatagramSocket socket = new DatagramSocket(); //2. Packaging byte[] arr = "Client: Haha...".getBytes(); //Four parameters: length of packet of packet Host object port number DatagramPacket packet = new DatagramPacket (arr, arr.length,InetAddress.getByName("10.0.122.255") , 4000); //3. Send socket.send(packet); //4. Close the resource socket.close(); } }Server side (receiver side):
import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;import java.util.Arrays;public class UDP_server { public static void main(String[] args) throws IOException { //1 DatagramSocket serverSocket = new DatagramSocket(4000); //2 byte[] arr = new byte[1024]; DatagramPacket packet = new DatagramPacket(arr, arr.length); //3 When the program is running, the receive method will be in the listening state serverSocket.receive(packet); //Fetch the data from the package byte[] arr1 = packet.getData(); System.out.println(new String(arr1)); //4 serverSocket.close(); }}result:
The first three network segments are servers with port number 4000 on all hosts with port number 10.0.122. As long as they are open, they can be received--
Client: Haha. . . .
The above broadcast form based on UDP in Java (example explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.