Path: blob/master/test/jdk/java/net/MulticastSocket/JoinLeave.java
41149 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.UncheckedIOException;25import java.net.InetAddress;26import java.net.MulticastSocket;27import java.net.NetworkInterface;2829import jdk.test.lib.NetworkConfiguration;30import jdk.test.lib.net.IPSupport;3132/**33* @test34* @bug 4091811 4148753 410273135* @summary Test java.net.MulticastSocket joinGroup and leaveGroup36* @library /test/lib37* @build jdk.test.lib.NetworkConfiguration38* jdk.test.lib.Platform39* @run main JoinLeave40* @run main/othervm -Djava.net.preferIPv4Stack=true JoinLeave41*/42public class JoinLeave {4344public static void main(String args[]) throws IOException {45IPSupport.throwSkippedExceptionIfNonOperational();46InetAddress ip4Group = InetAddress.getByName("224.80.80.80");47InetAddress ip6Group = InetAddress.getByName("ff02::a");4849NetworkConfiguration nc = NetworkConfiguration.probe();50nc.ip4MulticastInterfaces().forEach(nic -> joinLeave(ip4Group, nic));51nc.ip6MulticastInterfaces().forEach(nic -> joinLeave(ip6Group, nic));52}5354static void joinLeave(InetAddress group, NetworkInterface nif) {55System.out.println("Joining:" + group + " on " + nif);56try (MulticastSocket soc = new MulticastSocket()) {57soc.setNetworkInterface(nif);58soc.joinGroup(group);59soc.leaveGroup(group);60} catch (IOException e) {61throw new UncheckedIOException(e);62}63}64}656667