Example usage for java.net InetAddress isLoopbackAddress

List of usage examples for java.net InetAddress isLoopbackAddress

Introduction

In this page you can find the example usage for java.net InetAddress isLoopbackAddress.

Prototype

public boolean isLoopbackAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a loopback address.

Usage

From source file:com.oneops.inductor.Config.java

/**
 * Retruns the inductor IP address (IPV4 address). If there are multiple
 * NICs/IfAddresses, it selects the first one. Openstack VMs normally has
 * only one network interface (eth0).//  w ww.  j a v a 2  s .  com
 * 
 * @return IPV4 address of inductor with interface name. Returns
 *         <code>null</code> if it couldn't find anything.
 */
private String getInductorIPv4Addr() {
    try {
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        while (nics.hasMoreElements()) {
            NetworkInterface nic = nics.nextElement();
            if (nic.isUp() && !nic.isLoopback()) {
                Enumeration<InetAddress> addrs = nic.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress add = addrs.nextElement();
                    // Print only IPV4 address
                    if (add instanceof Inet4Address && !add.isLoopbackAddress()) {
                        // Log the first one.
                        String ip = add.getHostAddress() + " (" + nic.getDisplayName() + ")";
                        logger.info("Inductor IP : " + ip);
                        return ip;
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.warn("Error getting inductor IP address", e);
        // Skip any errors
    }
    return null;
}

From source file:cn.apputest.ctria.section2.Lucha2Activity.java

public String getLocalIpAddressGPRS() {
    try {//from w  ww . j a  va 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();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreferenceIpAddress", "ex.toString()");
    }
    return null;
}

From source file:com.att.android.arodatacollector.utils.AROCollectorUtils.java

/**
 * Returns the IP Address of the device during the trace cycle when it is
 * connected to the network./*from   w  w  w .  j av  a2  s  .  c o  m*/
 * 
 * @return The IP Address of the device.
 * 
 * @throws java.net.SocketException
 */
public String getLocalIpAddress() throws SocketException {
    for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
            .hasMoreElements();) {
        final NetworkInterface intf = en.nextElement();
        for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                .hasMoreElements();) {
            final InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()) {
                return inetAddress.getHostAddress();
            }
        }
    }
    return null;
}

From source file:com.lfv.yada.net.client.ClientNetworkManager.java

private InetAddress getLocalHostFix() throws SocketException {
    // Hack for getting the localhost inetaddress on linux systems
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface networkInterface = (NetworkInterface) e.nextElement();
        Enumeration e2 = networkInterface.getInetAddresses();
        while (e2.hasMoreElements()) {
            InetAddress ip = (InetAddress) e2.nextElement();
            if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                return ip;
            }/*  ww w. j  a v a2  s.  co  m*/
        }
    }
    return null;
}

From source file:com.cloud.utils.net.NetUtils.java

public static InetAddress getFirstNonLoopbackLocalInetAddress() {
    final InetAddress[] addrs = getAllLocalInetAddresses();
    if (addrs != null) {
        for (final InetAddress addr : addrs) {
            if (s_logger.isInfoEnabled()) {
                s_logger.info(/*from   w  w  w  .  j  a  v  a 2s.c  om*/
                        "Check local InetAddress : " + addr.toString() + ", total count :" + addrs.length);
            }

            if (!addr.isLoopbackAddress()) {
                return addr;
            }
        }
    }

    s_logger.warn("Unable to determine a non-loopback address, local inet address count :" + addrs.length);
    return null;
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondPublisher.java

private String getHostAddress() {
    String address = "127.0.0.1";
    try {/*w ww .j a  v a 2  s.  com*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface ni = en.nextElement();
            Enumeration<InetAddress> ads = ni.getInetAddresses();
            while (ads.hasMoreElements()) {
                InetAddress ip = ads.nextElement();
                if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) {
                    return ip.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        log.error("", e);
    }
    return address;
}

From source file:org.parosproxy.paros.core.proxy.ProxyThread.java

private boolean isRecursive(HttpRequestHeader header) {
    try {//  w  ww .ja  va 2 s.  c om
        if (header.getHostPort() == inSocket.getLocalPort()) {
            String targetDomain = header.getHostName();
            if (API.API_DOMAIN.equals(targetDomain)) {
                return true;
            }
            InetAddress targetAddress = InetAddress.getByName(targetDomain);
            if (parentServer.getProxyParam().isProxyIpAnyLocalAddress()) {
                if (targetAddress.isLoopbackAddress() || targetAddress.isSiteLocalAddress()
                        || targetAddress.isAnyLocalAddress()) {
                    return true;
                }
            } else if (targetAddress.equals(inSocket.getLocalAddress())) {
                return true;
            }
        }
    } catch (Exception e) {
        // ZAP: Log exceptions
        log.warn(e.getMessage(), e);
    }
    return false;
}

From source file:de.mangelow.throughput.NotificationService.java

private String getIPAddress() {

    try {/*from w  w w. j a  va 2 s.  c  o  m*/
        String ipv4;
        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        if (nilist.size() > 0) {
            for (NetworkInterface ni : nilist) {
                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
                if (ialist.size() > 0) {
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress()
                                && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                            return ipv4;
                        }
                    }
                }

            }
        }

    } catch (SocketException ex) {
        if (D)
            ex.printStackTrace();
    }

    return "";

}

From source file:com.material.katha.wifidirectmp3.DeviceDetailFragment.java

public byte[] getLocalIPAddress() {
    try {/*from   ww w .j  a v  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()) {
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    } catch (NullPointerException ex) {
        // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
    }
    return null;
}