Java代码
- import java.io.InputStream;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.util.Properties;
- public class UdpClientSocket {
- public static void main(String[] args) throws Exception {
- InputStream in =UdpClientSocket.class.getResourceAsStream("/config/udpConfig.properties");
- Properties properties = new Properties();
- properties.load(in);
- int listenPort = Integer.parseInt(properties.getProperty("listenPort"));
- int maxPacketsLength = Integer.parseInt(properties.getProperty("maxPacketsLength"));
- while (true) {
- DatagramSocket ds = new DatagramSocket(listenPort);
- byte[] buf = new byte[maxPacketsLength];
- DatagramPacket dp = new DatagramPacket(buf, maxPacketsLength);
- // dp.getData();
- // System.out.println(dp.getData());
- ds.receive(dp);
- System.out.println(new String(buf, 0, dp.getLength()));
- ds.close();
- }
- }
- }