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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 、__WSD吴少东 中级黑马   /  2013-4-25 20:37  /  1366 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 、__WSD吴少东 于 2013-4-27 12:55 编辑

事情是这样子的,麻烦各位大神,复制下代码在自己的机子上运行下,然后通过结束标记来结束,看下是否能够结束,
在我这破机子上结束不了,我不知道是怎么回事, 麻烦各位大神了就!
  1. /*
  2.         编写一个聊天小程序,
  3. */

  4. import java.io.*;
  5. import java.net.*;
  6. /*
  7. 需要用到多线程技术,
  8. */
  9. class Send implements Runnable
  10. {
  11.         private DatagramSocket ds;
  12.         public Send(DatagramSocket ds)
  13.         {
  14.                 this.ds = ds;
  15.         }
  16.         public void run()
  17.         {
  18.                 try
  19.                 {
  20.                         BufferedReader bufr =
  21.                                 new BufferedReader(new InputStreamReader(System.in));
  22.                         String line=null;
  23.                         while((line=bufr.readLine())!=null)
  24.                         {
  25.                                 if("886".equals(line))
  26.                                         break;
  27.                                 byte[] buf = line.getBytes();
  28.                                 DatagramPacket dp =
  29.                                         new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10003);

  30.                                 ds.send(dp);
  31.                         }
  32.                         
  33.                 }
  34.                 catch (Exception e)
  35.                 {
  36.                         throw new RuntimeException("发送端建立失败");
  37.                 }
  38.         }
  39. }

  40. class Receive implements Runnable
  41. {
  42.         private DatagramSocket ds;
  43.         public Receive(DatagramSocket ds)
  44.         {
  45.                 this.ds = ds;
  46.         }
  47.         public void run()
  48.         {
  49.                 try
  50.                 {
  51.                         while(true)
  52.                         {
  53.                                 byte[] buf = new byte[1024];
  54.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length);

  55.                                 ds.receive(dp);
  56.                                 String ip = dp.getAddress().getHostAddress();
  57.                                 String data = new String (dp.getData(),0,dp.getLength());

  58.                                 System.out.println(ip+"::"+data);

  59.                         }        
  60.                 }
  61.                 catch (Exception e)
  62.                 {
  63.                         throw new RuntimeException("接收端创建失败");
  64.                 }

  65.         }
  66. }

  67. class ChartDemo
  68. {
  69.         public static void main(String[] args) throws Exception
  70.         {
  71.                 DatagramSocket sendSocket = new DatagramSocket();
  72.                 DatagramSocket receiveSocket = new DatagramSocket(10003);

  73.                 new Thread(new Send(sendSocket)).start();
  74.                 new Thread(new Receive(receiveSocket)).start();
  75.         }
  76. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

5 个回复

倒序浏览
1、当你输入886时,接收线程还没停止,死循环,加上一个标志就可以了。
/*
        编写一个聊天小程序,
*/

import java.io.*;
import java.net.*;
/*
需要用到多线程技术,
*/
class Send implements Runnable
{
        private DatagramSocket ds;
        public Send(DatagramSocket ds)
        {
                this.ds = ds;
        }
        public void run()
        {
                try
                {
                       
                        BufferedReader bufr =
                                new BufferedReader(new InputStreamReader(System.in));
                        String line=null;

                        while((line=bufr.readLine())!=null)
                        {
                                if("886".equals(line))
                                {
                                        Receive.flag = false;
                                        System.out.println("结束");
                                        break;
                                }
                                byte[] buf = line.getBytes();
                                DatagramPacket dp =
                                        new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10003);

                                ds.send(dp);

                        }
                     //   System.out.println("test2");
                }

                catch (Exception e)
                {
                        throw new RuntimeException("发送端建立失败");
                }
        }
}

class Receive implements Runnable
{
        private DatagramSocket ds;
       public static boolean flag = true;
        public Receive(DatagramSocket ds)
        {
                this.ds = ds;
        }
        public void run()
        {
                try
                {

                                                while(flag)
                        {
                                byte[] buf = new byte[1024];
                                DatagramPacket dp = new DatagramPacket(buf,buf.length);

                                ds.receive(dp);
                                String ip = dp.getAddress().getHostAddress();
                                String data = new String (dp.getData(),0,dp.getLength());

                                System.out.println(ip+"::"+data);

                        }        
                }
                catch (Exception e)
                {
                        throw new RuntimeException("接收端创建失败");
                }

        }
}

public class ChartDemo
{
       

        public static void main(String[] args) throws Exception
        {

                DatagramSocket sendSocket = new DatagramSocket();
                          DatagramSocket receiveSocket = new DatagramSocket(10003);
                          new Thread(new Send(sendSocket)).start();
            new Thread(new Receive(receiveSocket)).start();

        }
}

回复 使用道具 举报
楼主你好   如果问题已经解决了,请把帖子的类型改为“已解决”{:soso_e181:}
回复 使用道具 举报
Den_din 发表于 2013-4-27 10:42
1、当你输入886时,接收线程还没停止,死循环,加上一个标志就可以了。
/*
        编写一个聊天小程序,

谢谢你,昨天刚看完,忘了还有这贴了....
回复 使用道具 举报
袁梦希 发表于 2013-4-27 10:49
楼主你好   如果问题已经解决了,请把帖子的类型改为“已解决”

谢谢提醒
回复 使用道具 举报
{:soso_e127:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马