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.ephesoft.dcma.util.NetworkUtil.java

/**
 * Utility method which will fetch the ip address of the system.
 * /*from   ww  w .  j  av  a  2 s  . com*/
 * @return ip address of the system.
 */
public static String getSystemIPAddress() {

    StringBuilder input = new StringBuilder(UtilConstants.INPUT_CONST);
    boolean first = true;
    String returnAddress = null;
    try {
        for (Enumeration<NetworkInterface> enumer = NetworkInterface.getNetworkInterfaces(); enumer
                .hasMoreElements();) {
            NetworkInterface netInterface = enumer.nextElement();
            Enumeration<InetAddress> inetEnum = netInterface.getInetAddresses();
            while (inetEnum.hasMoreElements()) {
                InetAddress inetAddress = inetEnum.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    if (first) {
                        first = false;
                    } else {
                        input.append(UtilConstants.FORWARD_SLASH);
                    }
                    input.append(inetAddress.getHostAddress());
                }
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        returnAddress = IPADDRESS;
    }
    if (null == returnAddress) {
        returnAddress = first ? IPADDRESS : input.toString();
    }
    return returnAddress;
}

From source file:com.wmfsystem.eurekaserver.broadcast.Server.java

public static Set<InetAddress> getLocalAddress() throws SocketException {
    Set<InetAddress> address = new HashSet<>();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();

            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                address.add(addr);/*ww w  .  j a  va 2 s  .  co  m*/
            }
        }
    }

    return address;
}

From source file:org.wso2.carbon.analytics.message.tracer.handler.util.PublisherUtil.java

private static InetAddress getLocalAddress() {
    Enumeration<NetworkInterface> ifaces = null;
    try {// ww  w . ja va  2  s.  com
        ifaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        LOG.error("Failed to get host address", e);
    }
    if (ifaces != null) {
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils.java

public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
    InetAddress address = InetAddress.getLocalHost();
    if (address.isLoopbackAddress()) {
        for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
            if (!networkInterface.isLoopback()) {
                for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                    InetAddress a = interfaceAddress.getAddress();
                    if (a instanceof Inet4Address) {
                        return a.getHostAddress();
                    }/*from  w ww .  jav a  2 s .c o m*/
                }
            }
        }
    }
    return address.getHostAddress();
}

From source file:com.jean.farCam.SettingsActivity.java

public static String getIPAddress() {
    try {/* w w w.  j  a va 2  s .c om*/
        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();
                    if (InetAddressUtils.isIPv4Address(sAddr)) {
                        return sAddr;
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:LocalAddress.java

/**
 * Return an address of a non-loopback interface on the local host
 * /*from   w  w w  . j a v  a  2 s.  c  om*/
 * @return address
 *         the InetAddress of the local host
 */
public static InetAddress getLocalAddress() {
    InetAddress addr = null;
    try {
        addr = InetAddress.getLocalHost();
        // OK - is this the loopback addr ?
        if (!addr.isLoopbackAddress()) {
            return addr;
        }
        // plan B - enumerate the network interfaces
        Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface netIf = (NetworkInterface) ifaces.nextElement();
            Enumeration addrs = netIf.getInetAddresses();
            while (addrs.hasMoreElements()) {
                addr = (InetAddress) addrs.nextElement();
                //System.out.println( "enum: " + addr.getHostAddress() );
                if (addr instanceof Inet6Address) {
                    // probably not what we want - keep looking
                    continue;
                }
                // chose (arbitrarily?) first non-loopback addr
                if (!addr.isLoopbackAddress()) {
                    return addr;
                }
            }
        }
        // nothing so far - last resort
        return getReflectedAddress();
    } catch (UnknownHostException uhE) {
        // deal with this
    } catch (SocketException sockE) {
        // can deal?
    }

    return null;
}

From source file:net.sourceforge.subsonic.util.Util.java

/**
 * Returns the local IP address./*from   ww  w. j  a  va 2s  . c  o  m*/
 * @return The local IP, or the loopback address (127.0.0.1) if not found.
 */
public static String getLocalIpAddress() {
    try {

        // Try the simple way first.
        InetAddress address = InetAddress.getLocalHost();
        if (!address.isLoopbackAddress()) {
            return address.getHostAddress();
        }

        // Iterate through all network interfaces, looking for a suitable IP.
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = iface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                    return addr.getHostAddress();
                }
            }
        }

    } catch (Throwable x) {
        LOG.warn("Failed to resolve local IP address.", x);
    }

    return "127.0.0.1";
}

From source file:Main.java

public static String getIfIpAddress() {
    String result = "";
    try {/*from  ww  w  .j  a  v  a 2s. c om*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                //                  Log.v("","ip="+inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress() && (inetAddress.getHostAddress().startsWith("0")
                        || inetAddress.getHostAddress().startsWith("1")
                        || inetAddress.getHostAddress().startsWith("2"))) {
                    result = inetAddress.getHostAddress();
                    break;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
        result = "192.168.0.1";
    }
    //      Log.v("","getIfIpAddress result="+result);
    if (result.equals(""))
        result = "192.168.0.1";
    return result;
}

From source file:org.wso2.bam.integration.tests.agents.KPIAgent.java

private static InetAddress getLocalHostAddress() throws SocketException {
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress addr = addresses.nextElement();
            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                return addr;
            }/*  ww  w  .  j a va  2s  . c  o m*/
        }
    }

    return null;
}

From source file:com.frame.network.utils.NetworkUtil.java

public static String getLocalIpv4Address() {
    try {/*ww  w  . j av a  2s.  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()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}