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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 闫杏荣 黑马帝   /  2012-6-21 07:05  /  2288 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 闫杏荣 于 2012-6-21 14:23 编辑

按照视频中敲的代码,但不知道问题出在哪,提示
  1. Exception in thread"main" java.lang.NoClassDefFoundError:chardemo(wrong name:udp/chardemo)
  2. ……
  3. Could not find the main class:chardemo. Program will exit.
复制代码
以下为源代码
  1. package udp;
  2. import java.io.*;
  3. import java.net.*;
  4. class Send implements Runnable
  5. {
  6.         private DatagramSocket ds;
  7.         public Send(DatagramSocket ds){
  8.                 this.ds = ds;
  9.         }
  10.         public void run(){
  11.                 try{
  12.                         BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));
  13.                         String line = null;
  14.                         while((line = bur.readLine())!=null)
  15.                         {
  16.                                 if("886".equals(line))
  17.                                         break;
  18.                                 byte[] buf = line.getBytes();
  19.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);
  20.                         }
  21.                 }
  22.                 catch(Exception e){
  23.                         throw new RuntimeException("发送端失败!");
  24.                        
  25.                 }
  26.         }
  27. }

  28. class Rece implements Runnable{
  29.         private DatagramSocket ds;
  30.         public Rece(DatagramSocket ds){
  31.                 this.ds=ds;
  32.         }
  33.         public void run(){
  34.                 try{
  35.                         while(true){
  36.                                 byte[] buf = new byte[1024];
  37.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  38.                                 ds.receive(dp);
  39.                                 String ip = dp.getAddress().getHostAddress();
  40.                                 String data = new String(dp.getData(),0,dp.getLength());
  41.                                 System.out.println(ip+":"+data);
  42.                         }
  43.                 }
  44.                 catch(Exception e){
  45.                         throw new RuntimeException("接收端失败!");
  46.                        
  47.                 }
  48.         }
  49. }
  50. public class chardemo {

  51.         /**
  52.          * @param args
  53.          */
  54.         public static void main(String[] args) throws Exception{
  55.                 // TODO Auto-generated method stub
  56.                 DatagramSocket sendSocket= new DatagramSocket();
  57.                 DatagramSocket receSocket = new DatagramSocket(1567);
  58.                
  59.                 new Thread(new Send(sendSocket)).start();
  60.                 new Thread(new Rece(receSocket)).start();

  61.         }

  62. }
复制代码
求火眼晶晶~~~~~~~

7 个回复

倒序浏览
你接收端监听的端口与设定的端口号不匹配,改为10002;还有你的类名要注意首字母大写!
回复 使用道具 举报
同学,这个问题我已经帮你解决了
回复 使用道具 举报
本帖最后由 一生一世 于 2012-6-21 09:01 编辑

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 bur = new BufferedReader(new InputStreamReader(System.in));
                    String line = null;
                    DatagramPacket dp = null;
                    System.out.println("please input data:");
                    while((line = bur.readLine())!=null)
                    {
                            System.out.println("input:"+line);
                        byte[] buf = line.getBytes();
                        dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.104"),10002);
                        ds.send(dp);//这个地方要发送数据,不然的话你定义的DatagramPacket就没有用了
                        if("886".equals(line))
                            break;
                    }
                }
                catch(Exception e){
                        throw new RuntimeException("发送端失败!");
                }
        }
}

class Rece implements Runnable{
        private DatagramSocket ds;
        public Rece(DatagramSocket ds){
                this.ds = ds;
        }
        public void run(){
                try{
                        DatagramPacket dp ;
                    while(true){
                        byte[] buf = new byte[1024];
                        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);
                        if(data.equals("886"))//这个地方一定要有退出的语句,不然就会在这里死循环
                                break;
                    }
                }
                catch(Exception e){
                        throw new RuntimeException("接收端失败!");
                }
        }
}
public class chardemo {

        public static void main(String[] args) throws Exception{
                DatagramSocket sendSocket = new DatagramSocket();
                DatagramSocket receSocket = new DatagramSocket(10002);
               
               /* new Thread(new Send(sendSocket)).start();
                new Thread(new Rece(receSocket)).start();*/
               
                Thread send = new Thread(new Send(sendSocket));
                send.start();
                Thread rece = new Thread(new Rece(receSocket));
                rece.sleep(500);这是先让接受线程等待,不然没有数据,你接受什么呀,你说对吧,当然你也可以用其他的办法比如wait方法
                rece.start();

        }

}
回复 使用道具 举报
本帖最后由 一生一世 于 2012-6-21 09:03 编辑

不好意思忘了写注释,你自己对比一下这个代码和你自己的代码有什么不同点,然后就可以找到bug的原因了,要是还有什么不明白的地方可以和我联系,联系方式我已经发给你了

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
哈哈,你发送端封装了数据,但是没发送啊。还有,数据报封装的端口和监听的端口不一样,所以你本机上是收不到的,UDP是面向无连接的,所以你发送的数据都丢掉了
回复 使用道具 举报
王涛 黑马帝 2012-6-21 11:47:00
7#
你的发送端没有写发送语句ds.send(dp),数据就没有发送
还有,你的DatagramSocket绑定到本地主机上的端口号与你的DatagramPacket发送到指定主机上的指定端口号不一样
数据接收不到。
回复 使用道具 举报
看看...........................
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马