标签:style blog http io ar color os sp on
这篇文章接着上一篇,不多说,直接上demo.
1 class UDPReceiveDemo 2 { 3 public static void main(String[]args)throws Exception 4 { 5 //建立接收端socket服务 6 DatagramSocket ds=new DatagramSocket(12222);//明确监听的端口号 7 //创建接收数据的数据包 8 byte[] buf=new byte[1024]; 9 DatagramPacket dp=new DatagramPacket(buf,buf.length); 10 //接收数据 11 ds.receive(dp); 12 //打印数据 13 System.out.println(dp.getAddress()); 14 System.out.println(dp.getPort()); 15 System.out.println(new String(dp.getData(),0,dp.getLength())); 16 //关闭服务 17 ds.close(); 18 } 19 }
标签:style blog http io ar color os sp on
原文地址:http://www.cnblogs.com/LenLi/p/4146428.html