< prev index next >

test/java/net/MulticastSocket/JoinLeave.java

Print this page

        

@@ -21,26 +21,42 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 4091811 4148753
+ * @bug 4091811 4148753 4102731
  * @summary Test java.net.MulticastSocket joinGroup and leaveGroup
- *
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.NetworkConfiguration
+ * @run main JoinLeave
  */
 
-import java.io.*;
-import java.net.*;
-
-
-public class JoinGroup {
-
-    public static void main(String args[]) throws Exception  {
-        MulticastSocket soc = null;
-        InetAddress sin = null;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+import java.net.NetworkInterface;
+import jdk.testlibrary.NetworkConfiguration;
+
+public class JoinLeave {
+
+    public static void main(String args[]) throws IOException  {
+        InetAddress ip4Group = InetAddress.getByName("224.80.80.80");
+        InetAddress ip6Group = InetAddress.getByName("ff02::a");
+
+        NetworkConfiguration nc = NetworkConfiguration.probe();
+        nc.ip4MulticastInterfaces().forEach(nic -> joinLeave(ip4Group, nic));
+        nc.ip6MulticastInterfaces().forEach(nic -> joinLeave(ip6Group, nic));
+    }
 
-        soc = new MulticastSocket();
-        sin = InetAddress.getByName("224.80.80.80");
-        soc.joinGroup(sin);
-        soc.leaveGroup(sin);
+    static void joinLeave(InetAddress group, NetworkInterface nif)
+    {
+        System.out.println("Joining:" + group + " on " + nif);
+        try (MulticastSocket soc = new MulticastSocket()) {
+            soc.setNetworkInterface(nif);
+            soc.joinGroup(group);
+            soc.leaveGroup(group);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
     }
 }
< prev index next >