黑马程序员技术交流社区

标题: 大神看下这个聊天小程序哪有问题!!!在线等 [打印本页]

作者: 痞子、蔚    时间: 2014-7-24 21:34
标题: 大神看下这个聊天小程序哪有问题!!!在线等
/*
报的这错误
Exception in thread "Thread-0" java.lang.RuntimeException: 发送端异常
        at Send.run(ChatDemo.java:50)
        at java.lang.Thread.run(Thread.java:745)

*/

import java.io.*;
import java.net.*;

class ChatDemo
{
        public static void main(String[] args) throws Exception
        {
                DatagramSocket sendSocket = new DatagramSocket();

                DatagramSocket receiveSocket = new DatagramSocket(10001);

                new Thread(new Send(sendSocket)).start();

                new Thread(new Receive(receiveSocket)).start();
        }
}

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;

                        while((line= bufr.readLine())!=null)
                        {
                                if("886".equals(line))
                                        break;

                                byte[] buf = line.getBytes();
                               
                                DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);

                                ds.send(dp);
                        }
                        ds.close();
                }
                catch (Exception e)
                {
                        throw new RuntimeException("发送端异常");
                }
        }
}

class Receive implements Runnable
{
        private DatagramSocket ds;

        public Receive(DatagramSocket ds)
        {
                this.ds = ds;
        }
        public void run()
        {
                try
                {
                        while(true)
                        {
                                byte[] buf = new byte[1024];

                                DatagramPacket dp = new DatagramPacket(buf,buf.length);

                                ds.receive(dp);

                                String data = new String(dp.getData(),0,dp.getLength());

                                String ip = dp.getAddress().getHostAddress();

                                System.out.println(ip+"::"+data);
                        }
                }
                catch (Exception e)
                {
                        throw new RuntimeException("接收端异常");
                }
        }
}



作者: masai158    时间: 2014-7-24 21:57
:D

带我明天在来回答。刚刚看到网络这块
作者: space2007    时间: 2014-7-24 22:07
还没看到这块,无能为力了
作者: 王一勇    时间: 2014-7-24 22:58
本帖最后由 王一勇 于 2014-7-24 23:06 编辑

DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);中差了一个参数0,应该写成:DatagramPacket(buf,0,buf.length,InetAddress.getByName("192.168.1.105"),10001); 而且你多了一个括号。
作者: 痞子、蔚    时间: 2014-7-25 10:01
王一勇 发表于 2014-7-24 22:58
DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);中差了一个参数0,应该写 ...

你说的这种和我用的方法是两种初始化方式,都对
不是这问题,而且我没多括号啊
作者: 姿_`态    时间: 2014-7-25 10:25
你确实是多了一个括号
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);
应该是
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105"),10001);

还有就是在我的电脑上用本地回环地址127.0.0.1测试,是可以的。
也许是你的ip问题,你可以用本地回环地址测试一下,
作者: blz01003hm    时间: 2014-7-25 10:58
   DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.105)"),10001);这有问题


   ("192.168.1.105)")冒号放错位置了吧,该是这样吧("192.168.1.105"))
作者: 痞子、蔚    时间: 2014-7-26 11:18
姿_`态 发表于 2014-7-25 10:25
你确实是多了一个括号
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("19 ...

真是我弄错了,:#已经好了




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