黑马程序员技术交流社区

标题: udp接收 [打印本页]

作者: fmi110    时间: 2015-9-18 23:36
标题: udp接收
a
  1. package udp.communication;

  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;

  5. public class Receive {

  6.         /**
  7.          * @param args
  8.          * @throws IOException
  9.          */
  10.         public static void main(String[] args) throws IOException {
  11.                 // 建套接字对象
  12.                 DatagramSocket ds = new DatagramSocket(10086);
  13.                 // 建包对象
  14.                 DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
  15.                 // 解析数据包
  16.                 while(true){
  17.                         ds.receive(dp);
  18.                         String line = new String(dp.getData(),0,dp.getLength());
  19.                         System.out.println("from"+dp.getAddress().getHostAddress()+" :"+line);
  20.                 }
  21.         }

  22. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2