Java programming to implement the method of data transmission based on UDP protocol

  • 2020-04-01 04:22:06
  • OfStack

The example of this paper describes the method of data transmission based on UDP protocol in Java programming. Share with you for your reference, as follows:

UDP (User Datagram Protocol, User Datagram Protocol) is different from the TCP Protocol, it is impossible to rely on, but it has faster transmission speed than TCP Protocol, UDP send unit of data called datagrams, when the network transmission UDP UDP Datagram is no guarantee that the data to the destination, there is no guarantee that according to send the order to arrive, That is to say, "hello" is sent first, then "world" is sent, but the receiver may receive "world" first, then "hello", or may not receive the data, why? Because it is impossible to rely on, it may be lost in transit. But UDP is better than TCP with transmission of real-time audio. The following is a simple example of an UPD transmission datagram

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 startup "); 
 } 
 public String echo(String msg){ 
  return "echo:"+msg; 
 } 
 public void service(){ 
  while (true) { 
   try { 
    DatagramPacket packet = new DatagramPacket(new byte[512], 512); 
    datagramSocket.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.DatagramSocket; 
import java.net.InetAddress; 
import java.net.SocketException; 
public class EchoClient { 
 private String remoteHost="localhost"; 
 private int remotePort=8088; 
 private DatagramSocket datagramSocket; 
 public EchoClient() 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; 
   InetAddress address = InetAddress.getByName(remoteHost); 
   while ((msg=reader.readLine())!=null) { 
    //Send datagram
    byte [] buffer = msg.getBytes(); 
    DatagramPacket packet = new DatagramPacket(buffer,buffer.length, address, remotePort); 
    datagramSocket.send(packet); 
    //Receiving datagram
    DatagramPacket inputPacket = new DatagramPacket(new byte[512], 512); 
    datagramSocket.receive(inputPacket); 
    System.out.println(new String(inputPacket.getData(), 0 , inputPacket.getLength())); 
    if("bye".equals(msg)){ 
     break; 
    } 
   } 
  } catch (IOException e) { 
   e.printStackTrace(); 
  }finally{ 
   datagramSocket.close(); 
  } 
}

I hope this article has been helpful to you in Java programming.


Related articles: