Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

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

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:org.panbox.desktop.common.gui.PanboxClientGUI.java

private DefaultComboBoxModel<Object> generateNetworkInterfacesModel() {
    DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<>();
    try {/*from   w w  w  .j  a  va2  s.com*/
        List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nic : nics) {
            List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
            if (addrs.size() > 0 && !nic.isLoopback() && nic.isUp()) {
                model.addElement(nic);
            }
        }
    } catch (SocketException e) {
        logger.warn(
                "An exception occurred while iterating over available network interfaces. Will return unfinished list: ",
                e);
    }
    return model;
}

From source file:cgeo.geocaching.cgBase.java

public static String getLocalIpAddress() {
    try {//from  w  ww . j av a  2  s.  c o m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        // nothing
    }

    return null;
}

From source file:carnero.cgeo.cgBase.java

public String getLocalIpAddress() {
    try {//from  ww w.ja v  a 2s .c om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException e) {
        // nothing
    }

    return null;
}

From source file:com.ah.be.admin.restoredb.RestoreAdmin.java

private static String getEth0Ip() throws SocketException, UnknownHostException {
    String eth0Ip = null;//from   ww w  . j a  v a 2 s  . c  o m

    overloop: for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
            .getNetworkInterfaces(); networkInterfaces.hasMoreElements();) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        String networkInterfaceName = networkInterface.getName();

        if ("eth0".equalsIgnoreCase(networkInterfaceName)) {
            Enumeration<InetAddress> eth0Addresses = networkInterface.getInetAddresses();

            while (eth0Addresses.hasMoreElements()) {
                InetAddress eth0Address = eth0Addresses.nextElement();

                if (eth0Address instanceof Inet4Address) {
                    eth0Ip = eth0Address.getHostAddress();
                    break overloop;
                }
            }
        }
    }

    if (eth0Ip == null) {
        InetAddress localAddress = InetAddress.getLocalHost();

        if (localAddress instanceof Inet4Address) {
            eth0Ip = localAddress.getHostAddress();
        }
    }

    return eth0Ip;
}

From source file:org.wso2.carbon.apimgt.impl.utils.APIUtil.java

private static InetAddress getLocalAddress() {
    Enumeration<NetworkInterface> ifaces = null;
    try {//  www  .j  a v  a  2s.com
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        log.error("Failed to get host address", e);
    }
    if (ifaces != null) {
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();

            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
    }

    return null;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public String getHostOrIP() {
    try {//from w  w  w .  j  av  a2  s .  com
        InetAddress i = java.net.InetAddress.getLocalHost();
        if (i.isLoopbackAddress()) {
            Enumeration<NetworkInterface> nie = NetworkInterface.getNetworkInterfaces();
            while (nie.hasMoreElements()) {
                NetworkInterface current = nie.nextElement();
                if (!current.isLoopback()) {
                    Enumeration<InetAddress> iae = current.getInetAddresses();
                    while (iae.hasMoreElements()) {
                        InetAddress currentI = iae.nextElement();
                        if (!currentI.isLoopbackAddress()) {
                            return currentI.getHostAddress();
                        }
                    }
                }
            }
        }
        return i.getHostAddress();
    } catch (Throwable t) {
        com.codename1.io.Log.e(t);
        return null;
    }
}