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:org.kei.android.phone.cellhistory.towers.MobileNetworkInfo.java

/** Get IP For mobile */
public static String getMobileIP(final boolean useIPv4) {
    try {/*from  w  ww  .  j  a  v  a2  s.c  o  m*/
        final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (final NetworkInterface intf : interfaces) {
            final List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (final InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    final String sAddr = addr.getHostAddress().toUpperCase(Locale.US);
                    final boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            final int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (final Exception ex) {
        Log.e(MobileNetworkInfo.class.getSimpleName(), "Exception in Get IP Address: " + ex.getMessage(), ex);
    }
    return TowerInfo.UNKNOWN;
}

From source file:com.alibaba.jstorm.utils.NetWorkUtils.java

public static List<String> host2Ip(List<String> servers) {
    if (servers == null || servers.size() == 0) {
        return new ArrayList<String>();
    }/*from   w w w .  jav a 2 s  . c o m*/

    Set<String> ret = new HashSet<String>();
    for (String server : servers) {
        if (StringUtils.isBlank(server)) {
            continue;
        }

        InetAddress ia;
        try {
            ia = InetAddress.getByName(server);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            LOG.info("Fail to get address of ", server);
            continue;
        }
        if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
            ret.add(NetWorkUtils.ip());
        } else {
            ret.add(ia.getHostAddress());
        }
    }

    return JStormUtils.mk_list(ret);
}

From source file:org.apache.synapse.transport.passthru.util.PassThroughTransportUtils.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.//from   www  .  j av  a 2  s  . c  o m
 * @throws java.net.SocketException if the socket can not be accessed
 */
public static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";

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

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

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

private static InetAddress getLocalAddress() {
    try {/*from   w  ww.jav  a  2  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:otsopack.commons.network.coordination.spacemanager.HttpSpaceManager.java

public static String getIpAddress() {
    try {/* w w  w. j  a  va  2  s .c  om*/
        final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netIf : Collections.list(nets)) {
            // If the network is active
            if (isUp(netIf)) {
                Enumeration<InetAddress> addresses = netIf.getInetAddresses();
                for (InetAddress addr : Collections.list(addresses))
                    // If the IP address is IPv4 and it's not the local address, store it
                    if (addr.getAddress().length == 4 && !addr.isLoopbackAddress())
                        return addr.getHostAddress();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.cellbots.logger.localServer.LocalHttpServer.java

/**
 * Returns the IP addresses of this device.
 * /*w w w  .ja v a2 s.  com*/
 * @return IP addresses as a String. Addresses are separated by newline.
 */
public static String getLocalIpAddresses() {
    String ipAddresses = "";
    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();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = inetAddress.getHostAddress().toString();
                    if (ipAddress.split("\\.").length == 4) {
                        ipAddresses = ipAddresses + ipAddress + ":8080\n";
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("", ex.toString());
    }
    return ipAddresses;
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * Returns the first real IP address it finds
 *
 * @return Real IP address or null if nothing available
 *//*www.  j a  v  a  2  s .  c  o  m*/
public static String getFirstIp4Address() {

    // Get all the physical adapters

    List<NetworkInterface> adapters = getNetworkAdapters();
    if (adapters.size() > 0) {
        for (NetworkInterface adapter : adapters) {

            // Loop through all the addresses

            Enumeration<InetAddress> addresses = adapter.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();

                // Only interested in non-loopback and IPv4 types

                if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                    return address.getHostAddress();
                }
            }
        }
    }
    return null;
}

From source file:com.nokia.dempsy.messagetransport.tcp.TcpReceiver.java

private static InetAddress getFirstNonLocalhostInetAddress() throws SocketException {
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = (NetworkInterface) netInterfaces.nextElement();
        for (Enumeration<InetAddress> loopInetAddress = networkInterface.getInetAddresses(); loopInetAddress
                .hasMoreElements();) {/*from   www.ja v a  2  s  . co m*/
            InetAddress tempInetAddress = loopInetAddress.nextElement();
            if (!tempInetAddress.isLoopbackAddress() && tempInetAddress instanceof Inet4Address)
                return tempInetAddress;
        }
    }
    return null;
}

From source file:net.grinder.util.NetworkUtil.java

/**
 * Get the local host address, try to get actual IP.
 * /*  ww  w.j ava 2 s. c  om*/
 * @return ip form of host address
 */
public static String getLocalHostAddress() {
    InetAddress localHost = null;
    try {
        localHost = InetAddress.getLocalHost();
    } catch (Exception e) {
        LOGGER.error("Error while get localhost address", e);
    }
    if (localHost != null && !localHost.isLoopbackAddress()) {
        return localHost.getHostAddress();
    }
    return getLocalHostAddress("www.google.com", 80);
}

From source file:org.basdroid.common.NetworkUtils.java

/**
 * Get IP address from first non-localhost interface
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string//from   w  w w . j  a  v a2 s  .c o  m
 */
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            List<InetAddress> inetAddresses = Collections.list(intf.getInetAddresses());
            for (InetAddress inetAddr : inetAddresses) {
                if (!inetAddr.isLoopbackAddress()) {
                    String address = inetAddr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(address);
                    if (useIPv4) {
                        if (isIPv4) {
                            return address;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = address.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? address : address.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}