Example usage for java.net NetworkInterface isUp

List of usage examples for java.net NetworkInterface isUp

Introduction

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

Prototype


public boolean isUp() throws SocketException 

Source Link

Document

Returns whether a network interface is up and running.

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());
    }/*  www.j a v a2  s . com*/
}

From source file:uk.ac.horizon.ubihelper.j2se.Server.java

/**
 * @param args//from www.  j  a  v a  2 s . c  o  m
 */
public static void main(String[] args) {
    InetAddress bestAddress = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            if (!ni.isUp() || ni.isVirtual() || ni.isLoopback())
                continue;
            logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName());
            Enumeration<InetAddress> as = ni.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address) {
                    Inet4Address ip = (Inet4Address) a;
                    logger.info("- IPv4 address " + ip.getHostAddress());
                    if (ip.isSiteLocalAddress()) {
                        logger.info("-- site local!");
                        if (bestAddress == null)
                            bestAddress = ip;
                    } else
                        bestAddress = ip;
                }
            }
        }
    } catch (Exception e) {
        logger.severe("Could not list NetworkInterfaces: " + e);
        System.exit(-1);
    }
    if (bestAddress == null) {
        logger.severe("Could not find an IP address to bind to - using localhost");
        try {
            bestAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            logger.severe("Could not get localhost address: " + e);
            System.exit(-2);
        }
    }
    int port = 0;
    if (args.length == 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException nfe) {
            System.err.println("Usage: <server-port>");
            System.exit(-3);
        }
    }
    Server server = new Server();
    server.init(bestAddress, port);
}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }//  w w w.ja  v  a2 s.co  m
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:Main.java

/**
 * @return The MAC hardware address of the first network interface of this host.
 *//*from ww  w  .  ja va  2s.  c o  m*/
public static byte[] getFirstNetworkInterfaceHardwareAddress() {
    try {
        Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface iface : Collections.list(interfaceEnumeration)) {
            if (!iface.isLoopback() && iface.isUp() && iface.getHardwareAddress() != null) {
                return iface.getHardwareAddress();
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Could not discover first network interface hardware address");
    }
    throw new RuntimeException("Could not discover first network interface hardware address");
}

From source file:Main.java

public static InetAddress getHostAddress() {
    try {//from   www . j a v a  2 s  .c om
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            // filters out 127.0.0.1 and inactive interfaces
            if (iface.isLoopback() || !iface.isUp())
                continue;

            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address)
                    return addr;
            }
        }
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
    return null;
}

From source file:org.stem.utils.Utils.java

public static List<InetAddress> getIpAddresses() {
    try {/*from w  w  w  . j a v  a  2 s. co m*/
        List<InetAddress> addrList = new ArrayList<InetAddress>();
        Enumeration<NetworkInterface> enumNI = NetworkInterface.getNetworkInterfaces();
        while (enumNI.hasMoreElements()) {
            NetworkInterface ifc = enumNI.nextElement();
            if (ifc.isUp()) {
                Enumeration<InetAddress> enumAdds = ifc.getInetAddresses();
                while (enumAdds.hasMoreElements()) {
                    InetAddress addr = enumAdds.nextElement();
                    addrList.add(addr);
                }
            }
        }
        return addrList;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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("");
    }//w  w  w.  j  a va  2  s.co  m
}

From source file:io.galeb.router.sync.HttpClient.java

private static String localIpsEncoded() {
    final List<String> ipList = new ArrayList<>();
    try {//ww  w .  java 2s.com
        Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
        while (ifs.hasMoreElements()) {
            NetworkInterface localInterface = ifs.nextElement();
            if (!localInterface.isLoopback() && localInterface.isUp()) {
                Enumeration<InetAddress> ips = localInterface.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress ipaddress = ips.nextElement();
                    if (ipaddress instanceof Inet4Address) {
                        ipList.add(ipaddress.getHostAddress());
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    String ip = String.join("-", ipList);
    if (ip == null || "".equals(ip)) {
        ip = "undef-" + System.currentTimeMillis();
    }
    return ip.replaceAll("[:%]", "");
}

From source file:com.vaadin.tests.tb3.PrivateTB3Configuration.java

/**
 * Tries to automatically determine the IP address of the machine the test
 * is running on./*from  w  ww.  ja va 2s .com*/
 * 
 * @return An IP address of one of the network interfaces in the machine.
 * @throws RuntimeException
 *             if there was an error or no IP was found
 */
private static String findAutoHostname() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface nwInterface = interfaces.nextElement();
            if (!nwInterface.isUp() || nwInterface.isLoopback() || nwInterface.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = nwInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isLoopbackAddress()) {
                    continue;
                }
                if (address.isSiteLocalAddress()) {
                    return address.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        throw new RuntimeException("Could not enumerate ");
    }

    throw new RuntimeException("No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found.");
}

From source file:org.droidupnp.Main.java

private static InetAddress getLocalIpAdressFromIntf(String intfName) {
    try {/*from w  w w .j a  v a  2  s.c o  m*/
        NetworkInterface intf = NetworkInterface.getByName(intfName);
        if (intf.isUp()) {
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
                    return inetAddress;
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "Unable to get ip adress for interface " + intfName);
    }
    return null;
}