staticvoid test(InetAddress group1, InetAddress group2) throws IOException
{ try (MulticastSocket mc1 = new MulticastSocket();
MulticastSocket mc2 = new MulticastSocket(mc1.getLocalPort());
DatagramSocket ds = new DatagramSocket()) { finalint port = mc1.getLocalPort();
out.printf("Using port: %d\n", port);
mc1.setSoTimeout(TIMEOUT);
mc2.setSoTimeout(TIMEOUT); int nextId = id; byte[] msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
DatagramPacket p = new DatagramPacket(msg, msg.length);
p.setAddress(group1);
p.setPort(port);
// join groups on all network interfaces
NetworkConfiguration.probe()
.ip4MulticastInterfaces(false)
.forEach((nic) -> { try {
mc1.joinGroup(toSocketAddress(group1), nic);
out.printf("mc1 joined the MC group on %s: %s\n",
nic.getDisplayName(), group1);
mc2.joinGroup(toSocketAddress(group2), nic);
out.printf("mc2 joined the MC group on %s: %s\n",
nic.getDisplayName(), group2);
} catch (IOException io) { thrownew UncheckedIOException(io);
}
});
out.printf("Sending datagram to: %s/%d\n", group1, port);
ds.send(p);
// the packet should be received by mc1 only
receive(mc1, true, nextId);
receive(mc2, false, 0);
nextId = ++id;
msg = (UUID + Integer.toString(nextId)).getBytes("UTF-8");
p = new DatagramPacket(msg, msg.length);
p.setAddress(group2);
p.setPort(port);
out.printf("Sending datagram to: %s/%d\n", group2, port);
ds.send(p);
// the packet should be received by mc2 only
receive(mc2, true, nextId);
receive(mc1, false, 0);
// leave groups on all network interfaces
NetworkConfiguration.probe()
.ip4MulticastInterfaces(false)
.forEach((nic) -> { try {
mc1.leaveGroup(toSocketAddress(group1), nic);
out.printf("mc1 left the MC group on %s: %s\n",
nic.getDisplayName(), group1);
mc2.leaveGroup(toSocketAddress(group2), nic);
out.printf("mc2 left the MC group on %s: %s\n",
nic.getDisplayName(), group2);
} catch (IOException io) { thrownew UncheckedIOException(io);
}
});
}
}
// Requires IP_MULTICAST_ALL on Linux (new since 2.6.31) so skip // on older kernels. Note that we skip on <= version 3 to keep the // parsing simple if (os.equals("Linux")) {
String osversion = System.getProperty("os.version");
String[] vers = osversion.split("\\.", 0); int major = Integer.parseInt(vers[0]); if (major < 3) {
System.out.format("Kernel version is %s, test skipped%n", osversion); return;
}
}
// multicast groups used for the test
InetAddress ip4Group1 = InetAddress.getByName("224.1.1.120");
InetAddress ip4Group2 = InetAddress.getByName("224.1.1.121");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.