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.air.mobilebrowser.NetworkUtil.java

public static String getIPAddress() {
    boolean useIPv4 = true;
    try {//from   ww w  .j  ava  2 s .c  o  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:eu.codebits.plasmas.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4/*from  w ww  .  j  a va2s. co m*/
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
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> 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 (SocketException ex) {
    }
    return "";
}

From source file:com.alibaba.napoli.metamorphosis.network.RemotingUtils.java

public static String getLocalAddress() throws Exception {
    // ????ip?//from  w  w  w . j  a  v a 2s. c om
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    InetAddress ipv6Address = null;
    while (enumeration.hasMoreElements()) {
        final NetworkInterface networkInterface = enumeration.nextElement();
        final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
        while (en.hasMoreElements()) {
            final InetAddress address = en.nextElement();
            if (!address.isLoopbackAddress()) {
                if (address instanceof Inet6Address) {
                    ipv6Address = address;
                } else {
                    // ipv4
                    return normalizeHostAddress(address);
                }
            }

        }

    }
    // ipv4?ipv6
    if (ipv6Address != null) {
        return normalizeHostAddress(ipv6Address);
    }
    final InetAddress localHost = InetAddress.getLocalHost();
    return normalizeHostAddress(localHost);
}

From source file:org.namelessrom.devicecontrol.net.NetworkInfo.java

public static String getIpAddress(final boolean useIPv4) {
    List<NetworkInterface> interfaces;
    try {//from ww  w  . j a  va  2 s. co m
        interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (Exception e) {
        interfaces = null;
    }
    if (interfaces == null)
        return "0.0.0.0";
    for (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();
                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));
                    }
                }
            }
        }
    }
    return "0.0.0.0";
}

From source file:camp.pixels.signage.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4// w ww  .j a  v a 2  s  .co  m
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
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;
            }
            if (!VALID_INTERFACES.contains(intf.getName()))
                continue;
            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 (SocketException ex) {
    }
    return "";
}

From source file:Main.java

/**
 * checks if the provided address is a global-scope ipv6 unicast address
 *//* w w w. j  a  v  a2s .co  m*/
public static boolean isGlobalAddressV6(InetAddress addr) {
    return addr instanceof Inet6Address && !addr.isAnyLocalAddress() && !addr.isLinkLocalAddress()
            && !addr.isLoopbackAddress() && !addr.isMulticastAddress() && !addr.isSiteLocalAddress()
            && !((Inet6Address) addr).isIPv4CompatibleAddress();
}

From source file:org.siddhiesb.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   ww w .j av  a 2s  .  co 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.apache.metron.enrichment.adapters.maxmind.MaxMindDbUtilities.java

/**
 * Determines if an address isn't eligible for getting appropriate results from the underlying database.
 * @param ipStr The IP String//  w  w  w . jav a2  s .  c  o m
 * @param addr The addr to be tested
 * @return True if ineligible, false otherwise
 */
public static boolean isIneligibleAddress(String ipStr, InetAddress addr) {
    return addr.isAnyLocalAddress() || addr.isLoopbackAddress() || addr.isSiteLocalAddress()
            || addr.isMulticastAddress() || !ipvalidator.isValidInet4Address(ipStr);
}

From source file:Main.java

public static String getIpAddress(boolean alwaysGetWifi) {

    try {//from w w w  .j  a  v a 2  s.  c  om
        Enumeration<NetworkInterface> enmNetI = NetworkInterface.getNetworkInterfaces();
        while (enmNetI.hasMoreElements()) {
            NetworkInterface networkInterface = enmNetI.nextElement();
            boolean b = alwaysGetWifi
                    ? networkInterface.getDisplayName().equals("wlan0")
                            || networkInterface.getDisplayName().equals("eth0")
                    : true;
            if (b) {
                Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
                while (inetAddressEnumeration.hasMoreElements()) {
                    InetAddress inetAddress = inetAddressEnumeration.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return "net name is " + networkInterface.getDisplayName() + ",ip is "
                                + inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:edu.stanford.junction.provider.jx.JunctionProvider.java

public static String getLocalIpAddress() {
    try {//from w ww.  java2  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()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("junction", ex.toString());
    }
    return null;
}