黑马程序员技术交流社区

标题: 怎么结束程序,大家帮忙改改 [打印本页]

作者: 曹睿翔    时间: 2012-12-22 16:59
标题: 怎么结束程序,大家帮忙改改
  1. import java.io.*;
  2. import java.net.*;
  3. class Send implements Runnable
  4. {
  5.         private DatagramSocket ds;
  6.         public Send(DatagramSocket ds){
  7.                 this.ds = ds;
  8.         }
  9.         public void run(){
  10.                 try
  11.                 {
  12.                        
  13.                         BufferedReader bufr =
  14.                                 new BufferedReader(new InputStreamReader(System.in));
  15.                         String line = null;
  16.                         while ((line = bufr.readLine())!= null)
  17.                         {
  18.                                 if ("886".equals(line))
  19.                                         <font color="red">break;</font>
  20.                                 byte[] buf = line.getBytes();
  21.                                 DatagramPacket dp =
  22.                                         new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"),10005);
  23.                                 ds.send(dp);
  24.                         }
  25.                         <font color="red">ds.close();</font>
  26.                 }
  27.                 catch (Exception e)
  28.                 {
  29.                         System.out.println("出现异常,发送失败");
  30.                 }
  31.         }
  32. }
  33. class Rece implements Runnable
  34. {
  35.         private DatagramSocket ds;
  36.         public Rece(DatagramSocket ds){
  37.                 this.ds = ds;
  38.         }
  39.         public void run(){
  40.                 try
  41.                 {
  42.                         while (true)
  43.                         {
  44.                                 byte[] buf = new byte[1024];
  45.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  46.                                 ds.receive(dp);
  47.                                 String ip = dp.getAddress().getHostAddress();
  48.                                 String data = new String(dp.getData(),0,dp.getLength());
  49.                                 System.out.println(ip+":::"+data);
  50.                         }
  51.                 }
  52.                 catch (Exception e)
  53.                 {
  54.                         System.out.println("出现异常,接收失败");
  55.                 }
  56.         }
  57. }
  58. class ChatDemo
  59. {
  60.         public static void main(String[] args) throws Exception
  61.         {
  62.                 DatagramSocket sendSocket = new DatagramSocket();
  63.                 DatagramSocket receSocket = new DatagramSocket(10005);
  64.                 new Thread(new Send(sendSocket)).start();
  65.                 new Thread(new Rece(receSocket)).start();
  66.         }
  67. }
复制代码
今天又试试,发现按下886中断不了程序,break,跳出while循环之后,ds关闭,接下来dos命令行卡在那里了,怎么改写让程序关闭,
作者: 曹睿翔    时间: 2012-12-22 20:47
老大给面子加分了,我自己顶顶,求解释,求改进




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