我是刚刚学到UDP的。刚照着写了个程序能编译但是运行的时候TestUdp这个发送程序出现找不到地址的异常,接受程序UdpRece很正常。我机子没有上网但是我自己设了IP为192.168.0.2 难道单机不能编写着UDP程序吗?请那位大虾指点一下 感激!
--------------------------------------------------------
import java.net.*;
public class TestUdp {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String strInfo ="www.it315.org"; DatagramPacket dp = new DatagramPacket(strInfo.getBytes(),
strInfo.getBytes().length,InetAddress.getByName("192.168.0.2"),3000);
ds.send(dp);
ds.close();
}
}
--------------------------------------------------------------
import java.net.*;
public class UdpRece {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,1024);
ds.receive(dp);
System.out.println( new String(dp.getData(),0,dp.getLength()));
}
}
--------------------------------------------------------
import java.net.*;
public class TestUdp {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String strInfo ="www.it315.org"; DatagramPacket dp = new DatagramPacket(strInfo.getBytes(),
strInfo.getBytes().length,InetAddress.getByName("192.168.0.2"),3000);
ds.send(dp);
ds.close();
}
}
--------------------------------------------------------------
import java.net.*;
public class UdpRece {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,1024);
ds.receive(dp);
System.out.println( new String(dp.getData(),0,dp.getLength()));
}
}
解决方案 »
- 求助:JApplet,JFrame,JPane的问题。
- 向老紫竹询问可用分问题
- 输出字符转整数问题
- neatbeans 多个TextField组件关联同一个popupMenu,如何添加popupMenuItem的事件,以判断是那个组件上弹出的菜单?
- 如何判断一个字符串中是否含有另一个字符串?
- 请教一个在JTable中显示swing组件的问题
- 什么叫中间包(件)?----初学者
- 请问哪里有jbuilder8.0下载地址呢?
- 是足球迷的进来!不要错过呦。
- while(true)对有返回值得java方法调用的影响
- 怎样设计数据库的访问框架????????????????
- NoClassDefFoundError错误,我在JSP中调用这个方法可以执行,但是用java命令来执行就出现了这个错误,不知奥什么原因
不需要设置IP地址
虽然答案比较简单,要我琢磨一下
但是还是有所得。
回去再试试!