黑马程序员技术交流社区

标题: udp [打印本页]

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

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.InetAddress;

  8. public class Send {

  9.         /**
  10.          * 发送端
  11.          *
  12.          * @throws IOException
  13.          */
  14.         public static void main(String[] args) throws IOException {
  15.                 // 建立数据报对象
  16.                 DatagramSocket ds = new DatagramSocket();
  17.                 // 封装键盘录入信息
  18.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  19.                 String line = null;
  20.                 // 信息装包
  21.                 while ((line = br.readLine()) != null) {
  22.                         if("886".equals(line)){
  23.                                 break;
  24.                         }
  25.                         DatagramPacket dp = new DatagramPacket(line.getBytes(),
  26.                                                 line.getBytes().length,InetAddress.getByName("192.168.31.248"),10086);
  27.                         // 发送包
  28.                         ds.send(dp);
  29.                 }
  30.                 // 释放资源
  31.         }

  32. }
复制代码







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