我在两台机器A、B上做测试,只是发送各个机器的ip,A能收到B以及自己的ip,但是B却受不到A的ip,只能收到自己的ip我的程序代码如下:(很简单的程序代码)
MultiS.java 用于发送的
public class MultiS { public static void main(String[] args) throws UnknownHostException, InterruptedException, IOException {
InetAddress group = InetAddress.getByName("230.0.0.1");
int port = 4000;
MulticastSocket ms = null;
String str = InetAddress.getLocalHost().getHostAddress();

try {
ms = new MulticastSocket();
ms.joinGroup(group);
while(true) {
byte[] buffer = str.getBytes();
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, group, port);
ms.send(dp);
Thread.sleep(10000);
}
} catch(IOException e) {
e.printStackTrace();
} finally {
if(ms != null) {
try{
ms.leaveGroup(group);
ms.close();
} catch(IOException e) {}
}
}
}MultiR.java用于接收的
public class MultiR { public static void main(String[] args) throws Exception {
InetAddress group = InetAddress.getByName("230.0.0.1");
int port = 4000;
MulticastSocket ms = null;
String str = null;

try {
ms = new MulticastSocket(port);
ms.joinGroup(group);

byte[] buffer = new byte[8192];
while(true) {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ms.receive(dp);
str = new String(dp.getData(), 0 ,dp.getLength());
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(ms != null) {
try {
ms.leaveGroup(group);
ms.close();
} catch(IOException e) {}
}
}
}}