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:totalcross.android.ConnectionManager4A.java

public static String getLocalIpAddress() {
    String ipv4 = null;//www . j  a  va2 s .c  o m
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                // for getting IPV4 format
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress()))
                    return ipv4;
            }
        }
    } catch (Exception ex) {
        AndroidUtils.debug("IP Address" + ex.toString());
    }
    return null;
}

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

private static String localIpsEncoded() {
    final List<String> ipList = new ArrayList<>();
    try {/*from   w w  w  .  j av  a 2  s . co  m*/
        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.zz.cluster4spring.localinfo.DefaultLocalNetworkInfoProvider.java

public static void collectInterfaceIPs(NetworkInterface aNetworkInterface, List<String> aResult,
        InetAddressAcceptor aAcceptor) {
    for (Enumeration<InetAddress> addresses = aNetworkInterface.getInetAddresses(); addresses
            .hasMoreElements();) {/*from w ww .  j  a v  a 2  s .co m*/
        InetAddress address = addresses.nextElement();
        if (aAcceptor.acceptInetAddress(aNetworkInterface, address)) {
            String hostAddress = address.getHostAddress();
            if (fLog.isInfoEnabled()) {
                fLog.info(MessageFormat.format("Found ACCEPTED IP:{0}", hostAddress));
            }
            aResult.add(hostAddress);
        } else if (fLog.isInfoEnabled()) {
            String hostAddress = address.getHostAddress();
            fLog.info(MessageFormat.format("Found SKIPPED IP:{0}", hostAddress));
        }
    }
}

From source file:stargate.commons.utils.IPUtils.java

public static Collection<String> getHostAddress() {
    if (!cached_host_addr.isEmpty()) {
        return Collections.unmodifiableCollection(cached_host_addr);
    } else {/*w  w w  .  j av a 2s.c om*/
        try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface n = (NetworkInterface) e.nextElement();
                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = (InetAddress) ee.nextElement();

                    String hostAddress = i.getHostAddress();
                    if (isProperHostAddress(hostAddress)) {
                        if (!cached_host_addr.contains(hostAddress)) {
                            cached_host_addr.add(hostAddress);
                        }
                    }

                    String hostName = i.getHostName();
                    if (isProperHostAddress(hostName)) {
                        if (!cached_host_addr.contains(hostName)) {
                            cached_host_addr.add(hostName);
                        }
                    }

                    String canonicalHostName = i.getCanonicalHostName();
                    if (isProperHostAddress(canonicalHostName)) {
                        if (!cached_host_addr.contains(canonicalHostName)) {
                            cached_host_addr.add(canonicalHostName);
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            LOG.error("Exception occurred while scanning local interfaces", ex);
        }

        return Collections.unmodifiableCollection(cached_host_addr);
    }
}

From source file:de.undercouch.gradle.tasks.download.TestBase.java

/**
 * Get a site local IP4 address from the current node's interfaces
 * @return the IP address or <code>null</code> if the address
 * could not be obtained//from  www  .j  a v  a 2s  . co  m
 * @throws SocketException if an I/O error occurs
 */
private static String findSiteLocal() throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface n = interfaces.nextElement();
        Enumeration<InetAddress> addresses = n.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress i = addresses.nextElement();
            if (i.isSiteLocalAddress() && i instanceof Inet4Address) {
                return i.getHostAddress();
            }
        }
    }
    return null;
}

From source file:org.kuali.rice.core.api.util.RiceUtilities.java

/** * @return the current environment's IP address, taking into account the Internet connection to any of the available
*         machine's Network interfaces. Examples of the outputs can be in octatos or in IPV6 format.
*
*         fec0:0:0:9:213:e8ff:fef1:b717%4 siteLocal: true isLoopback: false isIPV6: true
*         ============================================ 130.212.150.216 <<<<<<<<<<<------------- This is the one we
*         want to grab so that we can. siteLocal: false address the DSP on the network. isLoopback: false isIPV6:
*         false ==> lo ============================================ 0:0:0:0:0:0:0:1%1 siteLocal: false isLoopback:
*         true isIPV6: true ============================================ 127.0.0.1 siteLocal: false isLoopback:
*         true isIPV6: false/*from w  w  w  . j  a v a 2 s .  co  m*/
*/
private static String getCurrentEnvironmentNetworkIp() {
    Enumeration<NetworkInterface> netInterfaces = null;
    try {
        netInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Somehow we have a socket error...", e);
        return "127.0.0.1";
    }

    while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> address = ni.getInetAddresses();
        while (address.hasMoreElements()) {
            InetAddress addr = address.nextElement();
            if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()
                    && !(addr.getHostAddress().indexOf(":") > -1)) {
                return addr.getHostAddress();
            }
        }
    }
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        return "127.0.0.1";
    }
}

From source file:NotificationMessage.java

public static InetAddress getIPAddress() throws SocketException {
    InetAddress ip = null;/*from ww w.  j  a va 2 s. c om*/
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        if (netint.getName().equals("wlan0")) {
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                return inetAddress;
            }
            //displayInterfaceInformation(netint);

        }
        continue;

    }
    return ip;
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * Get IP address from first non-localhost interface
 * /* w ww.  j  av a 2 s.c om*/
 * @param ipv4 true=return ipv4, false=return ipv6
 * @return address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6
                                                            // port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

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

/**
 * Get IP addresses for all interfaces on this machine that are IPv4.
 * @return IPv4 addresses assigned to this machine
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *///w ww. j av  a 2s  . c  o m
public static Set<InetAddress> getAllLocalIpv4Addresses() throws IOException {
    Set<InetAddress> ret = new HashSet<>();

    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();

            if (addr instanceof Inet4Address && !addr.isAnyLocalAddress()) {
                ret.add(addr);
            }
        }
    }

    return ret;
}

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

/**
 * Get IP addresses for all interfaces on this machine that are IPv6.
 * @return IPv6 addresses assigned to this machine
 * @throws IOException if there's an error
 * @throws NullPointerException if any argument is {@code null}
 *//*from  w w  w. j  av a 2 s .  c o  m*/
public static Set<InetAddress> getAllLocalIpv6Addresses() throws IOException {
    Set<InetAddress> ret = new HashSet<>();

    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();

            if (addr instanceof Inet6Address && !addr.isAnyLocalAddress()) {
                ret.add(addr);
            }
        }
    }

    return ret;
}