UDP 客戶端接收
Java代碼 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);
- ds.receive(dp);
- System.out.println(new String(buf, 0, dp.getLength()));
- ds.close();
- }
- }
- }
歡迎關注本站公眾號,獲取更多信息