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 getIpAddress() {
    String ipaddress = "";

    try {// w ww.  j  a  v  a  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()) {
                    ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ipaddress = null;
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return ipaddress;
}

From source file:Main.java

public static String getGPRS_IP() {
    try {// w  ww  . ja va2s  .co  m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enu = intf.getInetAddresses(); enu.hasMoreElements();) {
                InetAddress inetAddress = enu.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    if (inetAddress.getHostAddress().toString().contains("::")) {
                        continue;
                    }
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static String getIPAdd() {
    try {//w  ww  . j  av a 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() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    try {/*from w w w  . j  a v a 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()) {

                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("system", ex.toString());
    }
    return null;
}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {//w  w  w  . j a v  a 2  s.  c o m
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return null;
    }

    while (interfaces.hasMoreElements()) {
        NetworkInterface iface = interfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();

        /* Check if we have a non-local address. If so, this is the active
         * interface.
         *
         * This isn't a perfect heuristic: I have devices which this will
         * still detect the wrong interface on, but it will handle the
         * common cases of wifi-only and Ethernet-only.
         */
        while (inetAddresses.hasMoreElements()) {
            InetAddress addr = inetAddresses.nextElement();

            if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
                return iface;
            }
        }
    }

    return null;
}

From source file:Main.java

public static void getLocalIP() {
    String ipaddress = "";
    try {/*from  www.  ja  v a  2 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()) {
                    ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
    }
}

From source file:Main.java

public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {//from   www. j a v  a2s.  c  om
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            String name = intf.getName();
            ArrayList<String> addresses = new ArrayList<String>();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    addresses.add(inetAddress.getHostAddress().toString());
                }
            }
            result.put(name, addresses);
        }
        return result;
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }/*  w w w.java2  s .  c o  m*/
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:Main.java

public static String getIpAddress() {
    try {/*from  ww  w. j  av  a2 s.  c  o m*/
        Enumeration en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface ex = (NetworkInterface) en.nextElement();
            Enumeration enumIpAddr = ex.getInetAddresses();

            while (enumIpAddr.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }

        return null;
    } catch (SocketException var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getIpInfo() {
    String ipInfo = null;//  w  w w .  j a v  a2  s . c  om

    try {
        Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces();

        LOOP: while (faces.hasMoreElements()) {
            Enumeration<InetAddress> addresses = faces.nextElement().getInetAddresses();

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

                if (!inetAddress.isLoopbackAddress()) {
                    ipInfo = inetAddress.getHostAddress().toString();

                    break LOOP;
                }
            }
        }

    } catch (Exception e) {
    }

    if (TextUtils.isEmpty(ipInfo)) {
        ipInfo = "";
    }

    return ipInfo;
}