书上说 DatagramSocket的setReuseAddress(true)方法执行后,可以允许多个DatagramSocket
绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket,
也就是说能够实现多播的功能。(我知道多播程序用MutilcastSocket类) ,
setReuseAddress() 必须在DatagramSocket绑定到端口之前调用才有效,
自己想写个小程序实验一下 setReuseAddress()方法 ,结果抛出异常Exception in thread "main" java.net.SocketException: already bound
at java.net.DatagramSocket.bind(DatagramSocket.java:355)
at UDPRecieve1.main(UDPRecieve1.java:10)
提示端口已经绑定了。
源程序如下:
public class UDPRecieve1 { public static void main(String args[]) throws IOException,SocketException
{
DatagramSocket ds=new DatagramSocket(); // 这个无参数的构造方法不知道调用的对不
ds.setReuseAddress(true);
ds.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),30002)); byte data[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println("从服务端发来的数据是:"+str);
System.out.println("此数据来自:"+dp.getAddress().getLocalHost()+" 端口号是:"+dp.getPort());
ds.close();
}
}
请高手指点一下,我的程序应该如何修改 万分感谢
绑定到相同的IP地址和端口,那么发送到此IP地址和端口的数据能够被复制到多个DatagramSocket,
也就是说能够实现多播的功能。(我知道多播程序用MutilcastSocket类) ,
setReuseAddress() 必须在DatagramSocket绑定到端口之前调用才有效,
自己想写个小程序实验一下 setReuseAddress()方法 ,结果抛出异常Exception in thread "main" java.net.SocketException: already bound
at java.net.DatagramSocket.bind(DatagramSocket.java:355)
at UDPRecieve1.main(UDPRecieve1.java:10)
提示端口已经绑定了。
源程序如下:
public class UDPRecieve1 { public static void main(String args[]) throws IOException,SocketException
{
DatagramSocket ds=new DatagramSocket(); // 这个无参数的构造方法不知道调用的对不
ds.setReuseAddress(true);
ds.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),30002)); byte data[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println("从服务端发来的数据是:"+str);
System.out.println("此数据来自:"+dp.getAddress().getLocalHost()+" 端口号是:"+dp.getPort());
ds.close();
}
}
请高手指点一下,我的程序应该如何修改 万分感谢
DatagramSocket ds = new DatagramSocket(null); // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
ds.setReuseAddress(true); // 绑定之前先设置Reuse
ds.bind(new InetSocketAddress(30002)); // 然后再绑定
System.out.println("REUSEADDR is enabled: " + ds.getReuseAddress()); // 返回结果是true,说明才有效
public class UDPRecieve1 { public static void main(String args[]) throws IOException,SocketException
{
DatagramSocket ds=new DatagramSocket(null);
ds.setReuseAddress(true);
ds.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),30002));
byte data[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println("从服务端发来的数据是:"+str);
System.out.println("此数据来自:"+dp.getAddress().getLocalHost()+" 端口号是:"+dp.getPort());
ds.close();
}
}
发送方程序如下:
public class UDPSend1 { public static void main(String args[]) throws IOException,SocketException
{
DatagramSocket ds=new DatagramSocket();
byte data[]=new byte[1024];
data="I send you something".getBytes();
DatagramPacket dp=new DatagramPacket(data,data.length,InetAddress.getByName("127.0.0.1"),30002);
ds.send(dp);
ds.close();
}
}现在是这样运行的 在两个命令提示符窗口将接收方程序分别运行启动,再另外一个窗口中运行发送方程序
结果是两个接收方都没有收到数据。
现在的问题是:书上说多个DatagramSocket绑定到同一个IP和端口,那么就可以同时收到数据啊
为什么现在没有收到数据? 麻烦高手再指导一下
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(null); // 指定Null很重要,否则Java会自动随机选个可用端口来绑定
ds.setReuseAddress(true); // 绑定之前先设置Reuse
ds.bind(new InetSocketAddress(9999)); // 然后再绑定
System.out.println("REUSEADDR is enabled: " + ds.getReuseAddress()); // 返回结果是true,说明才有效
DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
ds.receive(dp);
System.out.println("RECEIVE: " + new String(dp.getData())); }
发送方:
public static void main(String[] args) throws Exception {
String msg = "Hello world!";
DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.length(), new InetSocketAddress("255.255.255.255", 9999));
ds.send(dp);
System.out.println("SEND: " + new String(dp.getData())); }