Example usage for java.net NetworkInterface supportsMulticast

List of usage examples for java.net NetworkInterface supportsMulticast

Introduction

In this page you can find the example usage for java.net NetworkInterface supportsMulticast.

Prototype


public boolean supportsMulticast() throws SocketException 

Source Link

Document

Returns whether a network interface supports multicasting or not.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface nif = e.nextElement();
        System.out.println("Name: " + nif.getName() + ",  Supports Multicast: " + nif.supportsMulticast()
                + ", isUp(): " + nif.isUp());
    }//from   w ww .j a v  a 2 s  .  com
}

From source file:Main.java

public static void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());

    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();

    while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Address = " + ia.getAddress());
        System.out.println(" Broadcast = " + ia.getBroadcast());
        System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
        System.out.println("");
    }//from   w w w.  ja va 2 s . com
}

From source file:net.straylightlabs.archivo.controller.TelemetryController.java

private static List<String> getNetworkInterfaces() {
    List<String> nics = new ArrayList<>();
    try {//from w w  w .  j a  v  a  2s .co m
        for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (nic.isUp())
                nics.add(String.format(
                        "name='%s' isLoopback=%b isP2P=%b isVirtual=%b multicast=%b addresses=[%s]\n",
                        nic.getDisplayName(), nic.isLoopback(), nic.isPointToPoint(), nic.isVirtual(),
                        nic.supportsMulticast(), getAddressesAsString(nic)));
        }
    } catch (SocketException e) {
        logger.error("Error fetching network interface list: ", e);
    }
    return nics;
}

From source file:com.chiralBehaviors.slp.hive.configuration.MulticastConfiguration.java

public NetworkInterface getNetworkInterface() throws SocketException {
    if (networkInterface == null) {
        for (Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); intfs
                .hasMoreElements();) {//  w ww.ja va2s .  c o  m
            NetworkInterface intf = intfs.nextElement();
            if (intf.supportsMulticast()) {
                return intf;
            }
        }
        throw new IllegalStateException("No interface supporting multicast was discovered");
    }
    NetworkInterface iface = NetworkInterface.getByName(networkInterface);
    if (iface == null) {
        throw new IllegalArgumentException(
                String.format("Cannot find network interface: %s ", networkInterface));
    }
    return iface;
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

/**
 * Gets all IPv6 network interfaces on the local host.
 * //w w w  .ja va  2  s.  com
 * @return the list NetworkInterfaces
 */
private List<NetworkInterface> getAllIPv6NetIfs() throws SocketException {
    List<NetworkInterface> netIfs = new ArrayList<NetworkInterface>();
    Enumeration<NetworkInterface> localInterfaces = NetworkInterface.getNetworkInterfaces();
    if (localInterfaces != null) {
        while (localInterfaces.hasMoreElements()) {
            NetworkInterface netIf = localInterfaces.nextElement();
            // for multicast, the loopback interface is excluded
            if (netIf.supportsMulticast() && !netIf.isLoopback()) {
                Enumeration<InetAddress> ifAddrs = netIf.getInetAddresses();
                while (ifAddrs.hasMoreElements()) {
                    InetAddress ip = ifAddrs.nextElement();
                    if (ip instanceof Inet6Address) {
                        netIfs.add(netIf);
                        break; // out to next interface
                    }
                }
            }
        }
    } else {
        log.error("No network interfaces found!");
    }
    return netIfs;
}

From source file:com.jagornet.dhcpv6.server.DhcpV6Server.java

/**
 * Gets the IPv6 network interfaces for the supplied interface names.
 * //from   www  .ja  v a2  s  . c om
 * @param ifnames the interface names to locate NetworkInterfaces by
 * 
 * @return the list of NetworkInterfaces that are up, support multicast,
 * and have at least one IPv6 address configured
 * 
 * @throws SocketException the socket exception
 */
private List<NetworkInterface> getIPv6NetIfs(String[] ifnames) throws SocketException {
    List<NetworkInterface> netIfs = new ArrayList<NetworkInterface>();
    for (String ifname : ifnames) {
        if (ifname.equals("*")) {
            return getAllIPv6NetIfs();
        }
        NetworkInterface netIf = NetworkInterface.getByName(ifname);
        if (netIf == null) {
            // if not found by name, see if the name is actually an address
            try {
                InetAddress ipaddr = InetAddress.getByName(ifname);
                netIf = NetworkInterface.getByInetAddress(ipaddr);
            } catch (UnknownHostException ex) {
                log.warn("Unknown interface: " + ifname + ": " + ex);
            }
        }
        if (netIf != null) {
            if (netIf.isUp()) {
                // for multicast, the loopback interface is excluded
                if (netIf.supportsMulticast() && !netIf.isLoopback()) {
                    boolean isV6 = false;
                    List<InterfaceAddress> ifAddrs = netIf.getInterfaceAddresses();
                    for (InterfaceAddress ifAddr : ifAddrs) {
                        if (ifAddr.getAddress() instanceof Inet6Address) {
                            netIfs.add(netIf);
                            isV6 = true;
                            break;
                        }
                    }
                    if (!isV6) {
                        System.err.println("Interface is not configured for IPv6: " + netIf.getDisplayName());
                        return null;
                    }
                } else {
                    System.err.println("Interface does not support multicast: " + netIf.getDisplayName());
                    return null;
                }
            } else {
                System.err.println("Interface is not up: " + netIf.getDisplayName());
                return null;
            }
        } else {
            System.err.println("Interface not found or inactive: " + ifname);
            return null;
        }
    }
    return netIfs;
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

/**
 * Gets the IPv6 network interfaces for the supplied interface names.
 * /*from   w  w w.  j  ava  2  s .co  m*/
 * @param ifnames the interface names to locate NetworkInterfaces by
 * 
 * @return the list of NetworkInterfaces that are up, support multicast,
 * and have at least one IPv6 address configured
 * 
 * @throws SocketException the socket exception
 */
private List<NetworkInterface> getIPv6NetIfs(String[] ifnames) throws SocketException {
    List<NetworkInterface> netIfs = new ArrayList<NetworkInterface>();
    for (String ifname : ifnames) {
        if (ifname.equals("*")) {
            return getAllIPv6NetIfs();
        }
        NetworkInterface netIf = NetworkInterface.getByName(ifname);
        if (netIf == null) {
            // if not found by name, see if the name is actually an address
            try {
                InetAddress ipaddr = InetAddress.getByName(ifname);
                netIf = NetworkInterface.getByInetAddress(ipaddr);
            } catch (UnknownHostException ex) {
                log.warn("Unknown interface: " + ifname + ": " + ex);
            }
        }
        if (netIf != null) {
            if (netIf.isUp()) {
                // for multicast, the loopback interface is excluded
                if (netIf.supportsMulticast() && !netIf.isLoopback()) {
                    boolean isV6 = false;
                    List<InterfaceAddress> ifAddrs = netIf.getInterfaceAddresses();
                    for (InterfaceAddress ifAddr : ifAddrs) {
                        if (ifAddr.getAddress() instanceof Inet6Address) {
                            netIfs.add(netIf);
                            isV6 = true;
                            break;
                        }
                    }
                    if (!isV6) {
                        System.err.println("Interface is not configured for IPv6: " + netIf);
                        return null;
                    }
                } else {
                    System.err.println("Interface does not support multicast: " + netIf);
                    return null;
                }
            } else {
                System.err.println("Interface is not up: " + netIf);
                return null;
            }
        } else {
            System.err.println("Interface not found or inactive: " + ifname);
            return null;
        }
    }
    return netIfs;
}