a- package udp.communication;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- public class Receive {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // 建套接字对象
- DatagramSocket ds = new DatagramSocket(10086);
- // 建包对象
- DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
- // 解析数据包
- while(true){
- ds.receive(dp);
- String line = new String(dp.getData(),0,dp.getLength());
- System.out.println("from"+dp.getAddress().getHostAddress()+" :"+line);
- }
- }
- }
复制代码
|
|