Example usage for java.net NetworkInterface getNetworkInterfaces

List of usage examples for java.net NetworkInterface getNetworkInterfaces

Introduction

In this page you can find the example usage for java.net NetworkInterface getNetworkInterfaces.

Prototype

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException 

Source Link

Document

Returns an Enumeration of all the interfaces on this machine.

Usage

From source file:Main.java

public static String getIpAddress() {
    String ipaddress = "";

    try {//w w  w .  j a va 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 GetWantedIPAdress_JAVA() {
    String wantedIP = "";
    Enumeration<NetworkInterface> e = null;

    try {/*  w  w w.  j a v a2  s.c o  m*/
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                if (i.getHostAddress().toString().contains("192.168.")
                        || i.getHostAddress().toString().startsWith("10.")) {
                    wantedIP = i.getHostAddress();
                    break;
                }
            }
        }
    }

    return wantedIP;
}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface(Context c) {
    try {/*from 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())
                    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;
}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {//w w  w  . j a  v  a  2s .  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.
         */
        if (iface.getName().startsWith("w")) {
            //this is a perfect hack for getting wifi alone

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

                if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
                    Log.d("LSSDP", "DisplayName" + iface.getDisplayName() + " Name " + iface.getName());

                    return iface;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static String getWifiIpv6() {
    try {//from ww 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 Inet6Address) {
                    String[] ipv6 = inetAddress.getHostAddress().split("%");
                    String port = ipv6[1];
                    if (port.matches("wlan\\d+")) {
                        return ipv6[0];
                    }
                }
            }
        }
    } catch (SocketException ex) { /*CONSUME*/
    }
    return null;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String getLocalHostAddress() {
    boolean useIPv4 = true;

    try {//w ww.ja v  a2  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:Main.java

public static String getLocalIpAddress(Context context) {
    try {/*from   ww w .  j a va  2  s  . c  o m*/
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Returns MAC address of the given interface name.
 *
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @return mac address or empty string/*from   w  w w  .  j  a  va  2 s  .  co m*/
 */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    /*try {
    // this is so Linux hack
    return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
    } catch (IOException ex) {
    return null;
    }*/
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 *
 * @param useIPv4 true=return ipv4, false=return ipv6
 * @return address or empty string/*  w w w  .j  a  v  a 2 s  . c o  m*/
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        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();
                    //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    boolean isIPv4 = sAddr.indexOf(':') < 0;

                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
                            return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:Main.java

/**
 * Get the local IP address./*w ww  .j a v  a 2 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;
}