UDP 클 라 이언 트 수신

1183 단어 자바UDP
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();
        }
    }
}

좋은 웹페이지 즐겨찾기