int serverPort = ports[1];
Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
// first validate that we handle a null group ok
mss = new MulticastSocket(groupPort);
try {
mss.joinGroup(null, null);
fail("Did not get exception when group was null");
} catch (IllegalArgumentException e) {
}
// now validate we get the expected error if the address specified
// is not a multicast group
try {
groupSockAddr = new InetSocketAddress(InetAddress
.getByName("255.255.255.255"), groupPort);
mss.joinGroup(groupSockAddr, null);
fail("Did not get exception when group is not a multicast address");
} catch (IOException e) {
}
// now try to join a group if we are not authorized
// set the security manager that will make the first address not
// visible
System.setSecurityManager(new mySecurityManager());
try {
group = InetAddress.getByName("224.0.0.3");
groupSockAddr = new InetSocketAddress(group, groupPort);
mss.joinGroup(groupSockAddr, null);
fail("Did not get exception when joining group is not allowed");
} catch (SecurityException e) {
}
System.setSecurityManager(null);
if (atLeastOneInterface) {
// now validate that we can properly join a group with a null
// network interface
ports = Support_PortManager.getNextPortsForUDP(2);
groupPort = ports[0];
serverPort = ports[1];
mss = new MulticastSocket(groupPort);
mss.joinGroup(groupSockAddr, null);
mss.setTimeToLive(2);
Thread.sleep(1000);
// set up the server and join the group on networkInterface1
group = InetAddress.getByName("224.0.0.3");
groupSockAddr = new InetSocketAddress(group, groupPort);
server = new MulticastServer(groupSockAddr, serverPort,
networkInterface1);
server.start();
Thread.sleep(1000);
msg = "Hello World";
DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
.length(), group, serverPort);
mss.setTimeToLive(2);
mss.send(sdp);
Thread.sleep(1000);
// now vaildate that we received the data as expected
assertTrue("Group member did not recv data: ", new String(
server.rdp.getData(), 0, server.rdp.getLength())
.equals(msg));
server.stopServer();
// now validate that we handled the case were we join a
// different multicast address.
// verify we do not receive the data
ports = Support_PortManager.getNextPortsForUDP(2);
serverPort = ports[0];
server = new MulticastServer(groupSockAddr, serverPort,
networkInterface1);
server.start();
Thread.sleep(1000);
groupPort = ports[1];
mss = new MulticastSocket(groupPort);
InetAddress group2 = InetAddress.getByName("224.0.0.4");
mss.setTimeToLive(10);
msg = "Hello World - Different Group";
sdp = new DatagramPacket(msg.getBytes(), msg.length(), group2,
serverPort);
mss.send(sdp);
Thread.sleep(1000);
assertFalse(
"Group member received data when sent on different group: ",
new String(server.rdp.getData(), 0, server.rdp.getLength())
.equals(msg));
server.stopServer();
// if there is more than one network interface then check that
// we can join on specific interfaces and that we only receive
// if data is received on that interface
if (atLeastTwoInterfaces) {
// set up server on first interfaces
NetworkInterface loopbackInterface = NetworkInterface
.getByInetAddress(InetAddress.getByName("127.0.0.1"));
theInterfaces = NetworkInterface.getNetworkInterfaces();
while (theInterfaces.hasMoreElements()) {
NetworkInterface thisInterface = (NetworkInterface) theInterfaces
.nextElement();
if ((thisInterface.getInetAddresses() != null && thisInterface
.getInetAddresses().hasMoreElements())
&& (Support_NetworkInterface
.useInterface(thisInterface) == true)) {
// get the first address on the interface
// start server which is joined to the group and has
// only asked for packets on this interface
Enumeration addresses = thisInterface
.getInetAddresses();
NetworkInterface sendingInterface = null;
boolean isLoopback = false;
if (addresses != null) {
InetAddress firstAddress = (InetAddress) addresses
.nextElement();
if (firstAddress.isLoopbackAddress()) {
isLoopback = true;
}
if (firstAddress instanceof Inet4Address) {
group = InetAddress.getByName("224.0.0.4");
if (networkInterface1.equals(NetworkInterface
.getByInetAddress(InetAddress
.getByName("127.0.0.1")))) {
sendingInterface = networkInterface2;
} else {
sendingInterface = networkInterface1;
}
} else {
// if this interface only seems to support
// IPV6 addresses
group = InetAddress
.getByName("FF01:0:0:0:0:0:2:8001");
sendingInterface = IPV6networkInterface1;
}
}
InetAddress useAddress = null;
addresses = sendingInterface.getInetAddresses();
if ((addresses != null)
&& (addresses.hasMoreElements())) {
useAddress = (InetAddress) addresses.nextElement();
}
ports = Support_PortManager.getNextPortsForUDP(2);
serverPort = ports[0];
groupPort = ports[1];
groupSockAddr = new InetSocketAddress(group, serverPort);
server = new MulticastServer(groupSockAddr, serverPort,
thisInterface);
server.start();
Thread.sleep(1000);
// Now send out a package on interface
// networkInterface 1. We should
// only see the packet if we send it on interface 1
InetSocketAddress theAddress = new InetSocketAddress(
useAddress, groupPort);
mss = new MulticastSocket(groupPort);
mss.setNetworkInterface(sendingInterface);
msg = "Hello World - Again" + thisInterface.getName();
sdp = new DatagramPacket(msg.getBytes(), msg.length(),
group, serverPort);
mss.send(sdp);
Thread.sleep(1000);
if (thisInterface.equals(sendingInterface)) {
assertTrue(
"Group member did not recv data when bound on specific interface: ",
new String(server.rdp.getData(), 0,
server.rdp.getLength()).equals(msg));
} else {
assertFalse(
"Group member received data on other interface when only asked for it on one interface: ",
new String(server.rdp.getData(), 0,
server.rdp.getLength()).equals(msg));
}
server.stopServer();
}
}
// validate that we can join the same address on two
// different interfaces but not on the same interface
groupPort = Support_PortManager.getNextPortForUDP();
mss = new MulticastSocket(groupPort);
mss.joinGroup(groupSockAddr, networkInterface1);
mss.joinGroup(groupSockAddr, networkInterface2);
try {
mss.joinGroup(groupSockAddr, networkInterface1);
fail("Did not get expected exception when joining for second time on same interface");