Java代码

  1. import java.io.InputStream;
  2. import java.net.DatagramPacket;
  3. import java.net.DatagramSocket;
  4. import java.util.Properties;
  5.  
  6. public class UdpClientSocket {
  7.  
  8. public static void main(String[] args) throws Exception {
  9.  
  10. InputStream in =UdpClientSocket.class.getResourceAsStream("/config/udpConfig.properties");
  11. Properties properties = new Properties();
  12. properties.load(in);
  13.  
  14. int listenPort = Integer.parseInt(properties.getProperty("listenPort"));
  15. int maxPacketsLength = Integer.parseInt(properties.getProperty("maxPacketsLength"));
  16.  
  17. while (true) {
  18. DatagramSocket ds = new DatagramSocket(listenPort);
  19. byte[] buf = new byte[maxPacketsLength];
  20. DatagramPacket dp = new DatagramPacket(buf, maxPacketsLength);
  21. // dp.getData();
  22. // System.out.println(dp.getData());
  23. ds.receive(dp);
  24. System.out.println(new String(buf, 0, dp.getLength()));
  25. ds.close();
  26. }
  27. }
  28. }