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:Main.java

public static String getLocalIpAddress() {
    try {/*from   w w w . j  ava  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()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public synchronized static Inet4Address getLocalIpAddress() {
    try {/*from   www . j a  va2 s . com*/
        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 ((Inet4Address) inetAddress);
                    }
                }
            }
        }
    } catch (SocketException ex) {
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*  w  w w .  ja  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();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return null;

}

From source file:Main.java

public static String getIpAddress() {
    try {/*w  w  w  .ja  v  a 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() && inetAddress instanceof Inet4Address) {
                    // if (!inetAddress.isLoopbackAddress() && inetAddress
                    // instanceof Inet6Address) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getLocalIpBy3G() {
    try {// w  w  w.  j  a va 2s .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() && inetAddress instanceof Inet4Address) {
                    // if (!inetAddress.isLoopbackAddress() && inetAddress
                    // instanceof Inet6Address) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.splout.db.common.GetIPAddresses.java

/**
 * Returns all available IP addresses.//  w w  w  .j a v a 2s  . c o m
 * <p/>
 * In error case or if no network connection is established, we return an empty list here.
 * <p/>
 * Loopback addresses are excluded - so 127.0.0.1 will not be never returned.
 * <p/>
 * The "primary" IP might not be the first one in the returned list.
 *
 * @return Returns all IP addresses (can be an empty list in error case or if network connection is missing).
 * @throws SocketException
 * @since 0.1.0
 */
public static Collection<InetAddress> getAllLocalIPs() throws SocketException {
    LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>();

    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
    if (nifs == null)
        return listAdr;

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        // We ignore subinterfaces - as not yet needed.
        Enumeration<InetAddress> adrs = nif.getInetAddresses();
        while (adrs.hasMoreElements()) {
            InetAddress adr = adrs.nextElement();
            if (adr != null && !adr.isLoopbackAddress()
                    && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) {
                log.info("Available site local address: " + adr);
                listAdr.add(adr);
            }
        }
    }
    return listAdr;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {// w ww  .ja  va  2 s  .co m
        String ipv4;
        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }
        }
    } catch (SocketException ex) {
    }

    return null;
}

From source file:Main.java

public static String localIPAddress() {
    try {//from w  ww .j  a va2  s.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();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = inetAddress.getHostAddress().toString();
                    int p = ipAddress.indexOf("%");
                    if (p > 0)
                        ipAddress = ipAddress.substring(0, p);
                    //if (MagicBooleans.trace_mode) System.out.println("--> localIPAddress = "+ipAddress);
                    return ipAddress;
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "127.0.0.1";
}

From source file:Main.java

/**
 * Get the local IP address.//  w  w  w .  j  a  va2  s  .  c o m
 * 
 * @return string containing the current local IP address
 */
public static String getLocalIpAddress() {
    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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        android.util.Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface(Context c) {
    try {/*from   ww w .ja  v a 2 s .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();
                if (inetAddress.isLoopbackAddress())
                    break; // break inner loop, continue with outer loop

                return intf; // this is not the loopback and it has an IP address assigned
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    // nothing found
    return null;
}