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.synapse.transport.passthru.api.PassThroughNHttpGetProcessor.java

/**
* Whatever this method returns as the IP is ignored by the actual http/s listener when
* its getServiceEPR is invoked. This was originally copied from axis2
*
* @return Returns String.// www  .  j  a v  a2 s. c  o  m
* @throws java.net.SocketException if the socket can not be accessed
*/
protected static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";

    while (e.hasMoreElements()) {
        NetworkInterface networkInterface = (NetworkInterface) e.nextElement();
        Enumeration addresses = networkInterface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress ip = (InetAddress) addresses.nextElement();
            if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
                return ip.getHostAddress();
            }
        }
    }
    return address;
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static List<String> getLocalInetAddress() {
    List<String> inetAddressList = new ArrayList<String>();
    try {//w  w  w  .j  a v a 2  s . co  m
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        while (enumeration.hasMoreElements()) {
            NetworkInterface networkInterface = enumeration.nextElement();
            Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
            while (addrs.hasMoreElements()) {
                inetAddressList.add(addrs.nextElement().getHostAddress());
            }
        }
    } catch (SocketException e) {
        throw new RuntimeException("get local inet address fail", e);
    }

    return inetAddressList;
}

From source file:comikit.droidscript.DroidScriptServer.java

public static String[] getIpAddresses() {
    try {/*from  w  w  w.  j  av  a  2  s . c  o  m*/
        List<String> ipaddresses = new ArrayList<String>();
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface interf = interfaces.nextElement();
            Enumeration<InetAddress> adresses = interf.getInetAddresses();
            while (adresses.hasMoreElements()) {
                InetAddress address = adresses.nextElement();
                if (!address.isLoopbackAddress()) {
                    ipaddresses.add(address.getHostAddress().toString());
                }
            }
        }

        if (0 < ipaddresses.size()) {
            return ipaddresses.toArray(new String[1]);
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.opendaylight.lispflowmapping.implementation.lisp.MapServer.java

private static InetAddress getLocalAddress() {
    try {/*w w  w  .  j  av  a2  s. c  om*/
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();
            LOG.debug("Interface " + current.toString());
            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                // Skip loopback and link local addresses
                if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) {
                    continue;
                }
                LOG.debug(current_addr.getHostAddress());
                return current_addr;
            }
        }
    } catch (SocketException se) {
        LOG.debug("Caught socket exceptio", se);
    }
    return null;
}

From source file:sce.JobStoreTXCustom.java

public static String getIpAddress() {
    if (ip == null) {
        try {/*from w  w  w .  java 2s  . c o m*/
            for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        String ipAddress = inetAddress.getHostAddress().toString();
                        ip = (ip == null ? "" : ip + ";") + ipAddress;
                    }
                }
            }
        } catch (SocketException e) {
            return "";
        }
    }
    return ip;
}

From source file:be.deadba.ampd.SettingsActivity.java

private static String getLocalIpAddress() {
    try {/*from  www  . j  a  v a 2 s. co  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();
                String hostAddress = inetAddress.getHostAddress();
                if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(hostAddress)) {
                    return hostAddress;
                }
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:ee.ria.xroad.proxy.opmonitoring.OpMonitoringBuffer.java

private static String getIpAddress() {
    try {//from w w  w.  ja v a  2s. c  o m
        if (ipAddress == null) {
            NetworkInterface ni = list(getNetworkInterfaces()).stream()
                    .filter(OpMonitoringBuffer::isNonLoopback).findFirst()
                    .orElseThrow(() -> new Exception(NO_INTERFACE_FOUND));

            Exception addressNotFound = new Exception(NO_ADDRESS_FOUND + ni.getDisplayName());

            ipAddress = list(ni.getInetAddresses()).stream().filter(addr -> !addr.isLinkLocalAddress())
                    .findFirst().orElseThrow(() -> addressNotFound).getHostAddress();

            if (ipAddress == null) {
                throw addressNotFound;
            }
        }

        return ipAddress;
    } catch (Exception e) {
        log.error("Cannot get IP address of a non-loopback network interface", e);

        return "0.0.0.0";
    }
}

From source file:org.ngrinder.recorder.util.NetworkUtil.java

private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6)
        throws SocketException {
    Enumeration<?> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }//from w  w  w  . java 2 s .c o m
        if (StringUtils.containsIgnoreCase(i.getDisplayName(), "Host-Only")) {
            continue;
        }
        for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:org.votingsystem.util.HttpHelper.java

public static String getLocalIP() throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            if (inetAddress.isSiteLocalAddress()) {
                String inetAddressStr = inetAddress.toString();
                while (inetAddressStr.startsWith("/"))
                    inetAddressStr = inetAddressStr.substring(1);
                return inetAddressStr;
            }/*w w w .  ja va  2  s.  co m*/

        }
    }
    return null;
}

From source file:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectPublicIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {/* w  w w.  j  a  v a  2s  .c om*/
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
        }

        if (!isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (Inet4Address.class.isInstance(inetAddress)) {
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()
                            && !inetAddress.isLinkLocalAddress() && !inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    }

    return null;
}