Example usage for java.nio.channels MulticastChannel join

List of usage examples for java.nio.channels MulticastChannel join

Introduction

In this page you can find the example usage for java.nio.channels MulticastChannel join.

Prototype

MembershipKey join(InetAddress group, NetworkInterface interf) throws IOException;

Source Link

Document

Joins a multicast group to begin receiving all datagrams sent to the group, returning a membership key.

Usage

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Set a {@link MulticastChannel} to listen on all IPv6 interfaces.
 * @param channel multicast channel to listen on
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *//*from   w  w  w.ja v  a 2s .co  m*/
public static void multicastListenOnAllIpv6InterfaceAddresses(MulticastChannel channel) throws IOException {
    Validate.notNull(channel);

    final InetAddress ipv6Group = InetAddress.getByName("ff02::1"); // NOPMD

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
            InetAddress addr = addrs.nextElement();

            try {
                if (addr instanceof Inet6Address) {
                    channel.join(ipv6Group, networkInterface);
                }
            } catch (IOException ioe) { // NOPMD
                // occurs with certain interfaces, do nothing
            }
        }
    }
}

From source file:com.offbynull.portmapper.common.NetworkUtils.java

/**
 * Set a {@link MulticastChannel} to listen on all IPv4 interfaces.
 * @param channel multicast channel to listen on
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *//* w ww .j a  va  2 s .  com*/
public static void multicastListenOnAllIpv4InterfaceAddresses(MulticastChannel channel) throws IOException {
    Validate.notNull(channel);

    final InetAddress ipv4Group = InetAddress.getByName("224.0.0.1"); // NOPMD

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface networkInterface = interfaces.nextElement();
        Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
        while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface
            InetAddress addr = addrs.nextElement();

            try {
                if (addr instanceof Inet4Address) {
                    channel.join(ipv4Group, networkInterface);
                }
            } catch (IOException ioe) { // NOPMD
                // occurs with certain interfaces
                // do nothing
            }
        }
    }
}