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 InetAddress getLocalInetAddress() {
    InetAddress ip = null;//ww  w  .jav  a  2  s . c  o m
    try {
        Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
        while (en_netInterface.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();
            Enumeration<InetAddress> en_ip = ni.getInetAddresses();
            while (en_ip.hasMoreElements()) {
                ip = en_ip.nextElement();
                if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                    break;
                else
                    ip = null;
            }
            if (ip != null) {
                break;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return ip;
}

From source file:Main.java

public static String getIpAddress(boolean alwaysGetWifi) {

    try {/*  w ww. java 2s.  c  o m*/
        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:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {/*from w ww .jav 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 String getHostIP() {
    String hostIp = null;/* w  ww.  j  a  v a 2  s. co  m*/
    try {
        Enumeration nis = NetworkInterface.getNetworkInterfaces();
        InetAddress ia = null;
        while (nis.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) nis.nextElement();
            Enumeration<InetAddress> ias = ni.getInetAddresses();
            while (ias.hasMoreElements()) {
                ia = ias.nextElement();
                if (ia instanceof Inet6Address) {
                    continue;// skip ipv6
                }
                String ip = ia.getHostAddress();
                if (!"127.0.0.1".equals(ip)) {
                    hostIp = ia.getHostAddress();
                    break;
                }
            }
        }
    } catch (SocketException e) {
        Log.i("yao", "SocketException");
        e.printStackTrace();
    }
    return hostIp;

}

From source file:Main.java

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

From source file:Main.java

public static String getLocalIpAddress() throws SocketException {
    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();
            }//from  www  .ja  va 2  s  . c o m
        }
    }
    return null;
}

From source file:Main.java

@Nullable
public static String getLocalIPAddress() {
    try {/*from   ww w.  j  av a 2 s  .co  m*/
        for (Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); eni
                .hasMoreElements();) {
            NetworkInterface ni = eni.nextElement();
            for (Enumeration<InetAddress> eia = ni.getInetAddresses(); eia.hasMoreElements();) {
                InetAddress ia = eia.nextElement();
                if (!ia.isLoopbackAddress() && (ia instanceof Inet4Address)) {
                    return ia.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getIP() {
    try {/*  w  w w .  j  a v  a  2  s .  c om*/
        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 addr1 = addr.getHostAddress().toUpperCase().replaceAll("^/", "");
                    if (InetAddressUtils.isIPv4Address(addr1)) {
                        return addr1;
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** Finds a network interface of sub-interface with the given name */
public static NetworkInterface getByName(String name) throws SocketException {
    if (name == null)
        return null;
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface intf = en.nextElement();
        if (intf.getName().equals(name))
            return intf;
        Enumeration<NetworkInterface> en2 = intf.getSubInterfaces();
        while (en2.hasMoreElements()) {
            NetworkInterface intf2 = en2.nextElement();
            if (intf2.getName().equals(name)) {
                return intf2;
            }//from ww w.  j  a v  a2s.  com
        }
    }
    return null;
}

From source file:Main.java

public static String GetIPAdresses_JAVA() {
    String allIPAdresses = "";
    Enumeration<NetworkInterface> e = null;

    try {//from   ww w  .  jav  a 2s.  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();
                allIPAdresses += (i.getHostAddress() + "\n");
            }
        }
    }

    return allIPAdresses;
}