A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fmi110 高级黑马   /  2015-10-7 10:22  /  237 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

a
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayOutputStream;
  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 MutilThreadUdp {

  9.         /**
  10.          * @param args
  11.          * @throws Exception
  12.          */
  13.         public static void main(String[] args) throws Exception {
  14.                 DatagramSocket ds1 = new DatagramSocket(8888);
  15.                 DatagramSocket ds2 = new DatagramSocket(10888);
  16.                 Udpsend send = new Udpsend( ds1);
  17.                 Udpreceive re = new Udpreceive(ds2);
  18.                 new Thread(re).start();
  19.                 new Thread(send).start();

  20.         }

  21. }
  22. class Udpsend implements Runnable{
  23.         private DatagramSocket ds ;

  24.         public Udpsend(DatagramSocket ds) {
  25.                 super();
  26.                 this.ds = ds;
  27.         }

  28.         @Override
  29.         public void run() {
  30.                 System.out.println("客户端跑起:");
  31.                 BufferedReader br = null;
  32.                 while(true){
  33.                         br = new BufferedReader(new InputStreamReader(System.in));
  34.                         String line = null;
  35.                         try {
  36.                                 while((line=br.readLine())!=null){
  37.                                         byte[] buf = line.getBytes();
  38.                                         DatagramPacket dp = new DatagramPacket(buf,0,buf.length,
  39.                                                         InetAddress.getByName("192.168.220.255"),10888);
  40.                                         System.out.println("Send message...:");
  41.                                         ds.send(dp);
  42.                                         if(line.equals("886"))
  43.                                                 break;
  44.                                 }
  45.                                 break;
  46.                         } catch (IOException e) {
  47.                                 e.printStackTrace();
  48.                         }finally{
  49.                                 if(br!=null)
  50.                                         try {
  51.                                                 br.close();
  52.                                         } catch (IOException e) {
  53.                                                 e.printStackTrace();
  54.                                         }
  55.                                 if(ds!=null)
  56.                                         ds.close();
  57.                         }
  58.                 }
  59.                
  60.         }       
  61. }
  62. class Udpreceive implements Runnable{
  63.         private DatagramSocket ds;

  64.         public Udpreceive(DatagramSocket ds) {
  65.                 super();
  66.                 this.ds = ds;
  67.         }
  68.         @Override
  69.         public void run() {
  70.                 byte[] buf = new byte[1024];
  71.                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  72.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  73.                 System.out.println("服务器启动:");
  74.                 while (true) {
  75.                         try {
  76.                                 ds.receive(dp);
  77.                                 int len = dp.getLength();
  78.                                 int port = dp.getPort();
  79.                                 String ip = dp.getAddress().getHostAddress();
  80.                                 buf = dp.getData();
  81.                                 String message = new String(buf,0,len);
  82.                                 System.out.println(ip+" "+port+"发来信息:"+message);
  83.                                 if(message.equals("886"))
  84.                                         System.out.println(ip+" 下线");
  85. //                                bos.write(buf, 0, len);
  86. //                                System.out.println("字节数组输出流长度"+bos.size());
  87. //                                bos.writeTo(System.out);
  88. //                                bos.reset();
  89.                         } catch (IOException e) {
  90.                                 throw new RuntimeException("接收失败");
  91.                         }
  92.                 }
  93.         }
  94. }
复制代码


2 个回复

倒序浏览
从哪里复制来的额
回复 使用道具 举报

看笔记 然后自己打的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马