Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

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

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

From source file:org.apache.smscserver.test.TestUtil.java

public static InetAddress findNonLocalhostIp() throws Exception {
    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        Enumeration<InetAddress> ips = nif.getInetAddresses();

        while (ips.hasMoreElements()) {
            InetAddress ip = ips.nextElement();
            if ((ip instanceof java.net.Inet4Address) && !ip.isLoopbackAddress()) {
                return ip;
            } else {
                // IPv6 not tested
            }/*from w w  w . j a  v a2s  .  c o m*/
        }
    }

    return null;
}

From source file:Main.java

public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {//from  w ww  .j a  v  a2 s.c o  m
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            String name = intf.getName();
            ArrayList<String> addresses = new ArrayList<String>();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    addresses.add(inetAddress.getHostAddress().toString());
                }
            }
            result.put(name, addresses);
        }
        return result;
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

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

public static List<InetAddress> getIpAddresses() {
    try {/*from   w ww  .  j  av a  2  s. c  om*/
        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:uk.ac.horizon.ubihelper.j2se.Server.java

public static InetAddress getInetAddress(NetworkInterface ni) {
    // IPv4?/*w ww. j  a  v a  2 s  . co  m*/
    Enumeration<InetAddress> as = ni.getInetAddresses();
    while (as.hasMoreElements()) {
        InetAddress a = as.nextElement();
        if (a instanceof Inet4Address)
            return a;
    }
    // any
    return ni.getInetAddresses().nextElement();
}

From source file:massbank.svn.SVNUtils.java

/**
 * //w  ww. j a  v  a2s . c  om
 */
public static String getLocalIPAddress() {
    String address = "";
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface network = interfaces.nextElement();
            Enumeration<InetAddress> addresses = network.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress inet = addresses.nextElement();
                if (!inet.isLoopbackAddress() && inet instanceof Inet4Address) {
                    return inet.getHostAddress();
                }
            }
        }
        address = InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
    }
    return address;
}

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;
        }/*from w ww . j  av a  2s.  c  o  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:com.groupon.odo.proxylib.Utils.java

/**
 * This function returns the first external IP address encountered
 *
 * @return IP address or null/*  w ww  .  ja va 2 s .c  om*/
 * @throws Exception
 */
public static String getPublicIPAddress() throws Exception {
    final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";

    String ipAddr = null;
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();

            // Pick the first non loop back address
            if ((!i.isLoopbackAddress() && i.isSiteLocalAddress()) || i.getHostAddress().matches(IPV4_REGEX)) {
                ipAddr = i.getHostAddress();
                break;
            }
        }
        if (ipAddr != null) {
            break;
        }
    }

    return ipAddr;
}

From source file:com.apporiented.hermesftp.utils.NetUtils.java

/**
 * Returns the network address of a particular network interface.
 * // www  .j a  v a 2  s  .c  o  m
 * @param ni The network interface.
 * @return The machine address of a particular network interface.
 */
public static InetAddress getMachineAddress(NetworkInterface ni) {
    Enumeration<InetAddress> addrs = ni.getInetAddresses();
    InetAddress mAddr = null;
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr.isSiteLocalAddress()) {
            mAddr = addr;
        }
    }
    return mAddr;
}

From source file:Main.java

public static InetAddress getHostAddress() {
    try {//  w  ww. j a v a  2 s .  c  o m
        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:com.cats.version.utils.Utils.java

public static String getLocalHostIp() {
    String regex = "[\\d]+.[\\d]+.[\\d]+.[\\d]+";
    Enumeration<?> e = null;
    try {//from w  w  w.  ja v  a 2s. c  om
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration<?> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();
            String ip = i.getHostAddress();
            if (ip.matches(regex) && !ip.equals("127.0.0.1")) {
                return ip;
            }
        }
    }
    return null;
}