This article describes the Java programming method to implement data transmission based on UDP protocol. Share it for your reference, as follows:
The UDP protocol (User Datagram Protocol) is different from the TCP protocol. It is impossible to rely on, but it has a faster transmission speed than the TCP protocol. The data unit sent by UDP is called a datagram. When the network transmits UDP. It is impossible to guarantee that the data can reach the destination in the order of transmission of UDP datagrams, and it cannot guarantee that the destination will be reached in the order of transmission. That is to say, "hello" is sent first, and then "world" is sent, but the receiver may receive " World” and you may not receive the data after receiving “hello” again. Why? Because it is impossible to rely on, it may be lost during transmission. But UDP is more suitable for transmitting real-time audio than TCP. Here is a simple example of UPD datagram transmission
Server side:
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class EchoServer { private DatagramSocket datagramSocket; private final int port = 8088; public static void main(String[ ] args) throws SocketException { new EchoServer().service(); } public EchoServer() throws SocketException{ datagramSocket = new DatagramSocket( port); System.out.println("Server Start"); } public String echo(String msg ){ return "echo:"+msg; } public void service(){ while (true) { try { DatagramPacket packet = new DatagramPacket(new byte[512], 512); datagr amSocket.receive(packet); String msg = new String(packet.getData(), 0, packet.getLength()); System.out.println(packet.getAddress()+"/"+packet.getPort()+" msg:"+msg); packet. setData (echo(msg).getBytes()); datagramSocket.send(packet); } catch (IOException e) { e.printStackTrace(); } } } }Client:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.Data gramSocket; import java.net.InetAddress; import java.net.SocketException; public class EchoClient { private String remoteHost="localhost"; private int remotePort=8088; private DatagramSocket datagramSocket; public EchoClient t() throws SocketException{ datagramSocket = new DatagramSocket(); } public static void main(String[] args) throws SocketException { new EchoClient().talk(); } public void talk(){ try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; In etAddress address = InetAddress.getByName(remoteHost); while (( msg=reader.readLine())!=null) { //Send datagram byte [] buffer = msg.getBytes(); DatagramPacket packet = new DatagramPacket(buffer,buffer.length, add ress, remotePort); datagramSocket.send( packet); //Receive datagramDatagramPacket inputPacket = new DatagramPacket(new byte[512], 512); datagramSocket.receive(inputPacket); System.out.pr intln(new String(inputPacket.getData(), 0, inputPacket.getLength ())); if("bye".equals(msg)){ break; } } } catch (IOException e) { e.printStackTrace(); } finally{ datagramSocket.close(); } }I hope this article will be helpful to everyone's Java programming.