黑马程序员技术交流社区
标题:
UDP接收和发送端小程序一枚——挑错
[打印本页]
作者:
闫杏荣
时间:
2012-6-21 07:05
标题:
UDP接收和发送端小程序一枚——挑错
本帖最后由 闫杏荣 于 2012-6-21 14:23 编辑
按照视频中敲的代码,但不知道问题出在哪,提示
Exception in thread"main" java.lang.NoClassDefFoundError:chardemo(wrong name:udp/chardemo)
……
Could not find the main class:chardemo. Program will exit.
复制代码
以下为源代码
package udp;
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;
while((line = bur.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);
}
}
catch(Exception e){
throw new RuntimeException("发送端失败!");
}
}
}
class Rece implements Runnable{
private DatagramSocket ds;
public Rece(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 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 chardemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
DatagramSocket sendSocket= new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(1567);
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
复制代码
求火眼晶晶~~~~~~~
作者:
田建
时间:
2012-6-21 07:57
你接收端监听的端口与设定的端口号不匹配,改为10002;还有你的类名要注意首字母大写!
作者:
一生一世
时间:
2012-6-21 08:56
同学,这个问题我已经帮你解决了
作者:
一生一世
时间:
2012-6-21 08:56
本帖最后由 一生一世 于 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 08:57
本帖最后由 一生一世 于 2012-6-21 09:03 编辑
不好意思忘了写注释,你自己对比一下这个代码和你自己的代码有什么不同点,然后就可以找到bug的原因了,要是还有什么不明白的地方可以和我联系,联系方式我已经发给你了
作者:
潘东升
时间:
2012-6-21 10:22
哈哈,你发送端封装了数据,但是没发送啊。还有,数据报封装的端口和监听的端口不一样,所以你本机上是收不到的,UDP是面向无连接的,所以你发送的数据都丢掉了
作者:
王涛
时间:
2012-6-21 11:47
你的发送端没有写发送语句ds.send(dp),数据就没有发送
还有,你的DatagramSocket绑定到本地主机上的端口号与你的DatagramPacket发送到指定主机上的指定端口号不一样
数据接收不到。
作者:
徐传任
时间:
2012-10-10 12:21
看看...........................
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2