java Socket UDP example details

  • 2020-06-01 09:53:19
  • OfStack

UDP programming example

Server-side:


package socket; 
 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
 
public class UDPServer { 
 
   
  public static void main(String[] args) throws IOException { 
    byte[] buf = new byte[1024]; 
    DatagramPacket dp = new DatagramPacket(buf, buf.length);// The statement 1 A "package" to receive data  
    DatagramSocket ds = new DatagramSocket(5678);// Specifies the UDP Port number in 5678 In the TCP Medium, there is another 1 The port number is 5678 The port of  
    while(true){ 
      ds.receive(dp); // The block type  
      System.out.println(new String(buf,0,dp.getLength()));//dp The length of the data obtained in  
    } 
 
  } 
 
} 

Client:


package socket; 
 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetSocketAddress; 
import java.net.SocketException; 
 
public class UDPClient { 
 
  public static void main(String[] args) throws IOException { 
    byte[] buf = new String("Hello").getBytes(); 
    //UDP It is connectionless, so specify what to send in the sent data package ip : port 
    DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678)); 
    DatagramSocket ds = new DatagramSocket(9999); // Specifies the port number of the sender  
    ds.send(dp); 
    ds.close(); 
  } 
 
} 

Description:

1. Strictly speaking, UDP does not have server and client, unlike ServerSocket and Socket in TCP, UDP has DatagramSocket.

2. Ports of UDP and TCP are separated. For example, UDP has a port number of 5678, and TCP has a port number of 5678.

3. UDP is connectionless, so specify the ip: port to be sent in the data package to be sent.

4. If you want to send data of long, int and other types, you need to use streams of ByteArrayOutputStream, DataOutputStream, ByteArrayInputStream and ByteArrayInputStream, as follows:

Server-side:


package socket; 
 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
 
public class UDPServer { 
 
   
  public static void main(String[] args) throws IOException { 
    byte[] buf = new byte[1024]; 
    DatagramPacket dp = new DatagramPacket(buf, buf.length); 
    DatagramSocket ds = new DatagramSocket(5678); 
    ByteArrayInputStream bais = null; 
    DataInputStream dos = null; 
    while(true){ 
      ds.receive(dp);  
      bais = new ByteArrayInputStream(buf); 
      dos = new DataInputStream(bais); 
      System.out.println(dos.readLong()); 
    } 
 
  } 
 
} 

Client:


package socket; 
 
import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetSocketAddress; 
import java.net.SocketException; 
 
public class UDPClient { 
 
  public static void main(String[] args) throws IOException { 
    byte[] buf = null; 
    long l = 100000L; 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    DataOutputStream dos = new DataOutputStream(baos); 
    dos.writeLong(l); 
    buf = baos.toByteArray(); 
    DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678)); 
    DatagramSocket ds = new DatagramSocket(9999);  
    ds.send(dp); 
    ds.close(); 


        dos.close(); 
  } 
 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: