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.ow2.proactive.scheduler.util.SchedulerStarter.java

public static boolean isThisMyIpAddress(InetAddress addr) {
    // Check if the address is a valid special local or loop back
    if (addr.isAnyLocalAddress() || addr.isLoopbackAddress())
        return true;

    // Check if the address is defined on any interface
    try {//from ww  w.  j  a  v a 2 s .  c o m
        return NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
        return false;
    }
}

From source file:comikit.droidscript.DroidScriptServer.java

public static String[] getIpAddresses() {
    try {// w  w  w .ja v 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.ngrinder.recorder.util.NetworkUtil.java

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

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

static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
    Enumeration<?> en = getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }//w  w w . ja  v  a2 s.c  o  m
        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:sce.JobStoreTXCustom.java

public static String getIpAddress() {
    if (ip == null) {
        try {/*from  w w w  .j a  va  2  s . com*/
            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:com.google.appinventor.components.runtime.BlockyTalky.java

private static String getIPAddress(boolean useIPv4) {
    try {/*w w  w  .  j av a2s .co  m*/
        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 ex) {
    } // for now eat exceptions
    return "";
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.Configurator.java

/**
 * Retrieve host name, mac address of the device
 *
 * @return details Map<String, String>
 *//*w  ww  . ja v  a 2  s . c  o m*/
protected static Map<String, String> getDeviceDetails() {
    InetAddress ip;
    String hostName = "";
    String macAddress = ConfigConstants.DEFAULT_MAC_ADDRESS;
    Map<String, String> details = new HashMap();
    try {
        ip = InetAddress.getLocalHost();
        hostName = ip.getHostName();
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
            for (; enumeration.hasMoreElements();) {
                InetAddress address = enumeration.nextElement();
                if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()
                        && address.isSiteLocalAddress()) {
                    byte[] mac = networkInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            //Construct mac address
                            sb.append(String.format("%02X%s", mac[i],
                                    (i < mac.length - 1) ? ConfigConstants.DELIMITER : ""));
                        }
                        macAddress = sb.toString();
                        break;
                    }
                }
            }
        }
    } catch (UnknownHostException | SocketException e) {
        log.error("Error while retrieving mac address", e);
        Runtime.getRuntime().exit(1);
    }
    details.put(ConfigConstants.HOST_NAME, hostName);
    details.put(ConfigConstants.MAC_ADDRESS, macAddress);
    return details;
}

From source file:at.alladin.rmbt.shared.Helperfunctions.java

public static boolean isIPLocal(final InetAddress adr) {
    return adr.isLinkLocalAddress() || adr.isLoopbackAddress() || adr.isSiteLocalAddress();
}

From source file:com.max2idea.android.fwknop.Fwknop.java

public static String getLocalIpAddress() {
    try {/*from  w  w  w. j av  a2  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()) {
                    Log.v("Internal ip", inetAddress.getHostAddress().toString());
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Internal IP", ex.toString());
    }
    return null;
}

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

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