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 findAppropriateIp() {
    String result = "unknown";

    Enumeration<NetworkInterface> e;
    try {//www.  j  a  v  a2  s .c  o m
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        return result;
    }
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress inetAddr = ee.nextElement();
            String ipAddr = inetAddr.getHostAddress();

            Matcher m = ipAddrPattern.matcher(ipAddr);
            if (m.matches()) {
                if (!m.group(1).equals("127")) {
                    result = ipAddr;
                }
            }
        }
    }

    return result;
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*w w  w  .ja v  a  2  s  . c  om*/
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress() {
    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().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4)
                        return sAddr;
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}

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//from  w w  w  .  j a va 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().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 HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {/*  w  ww . ja v  a2 s .  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:Main.java

/**
 * Get all device ip addresses/*from   www  .ja  v  a2  s. co m*/
 * @return {@link array}
 */
public static String[] getLocalIpAddress() {
    ArrayList<String> addresses = new ArrayList<String>();
    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()) {
                    //IPAddresses.setText(inetAddress.getHostAddress().toString());
                    boolean isIPv4 = isIPv4Address(inetAddress.getHostAddress().toString());
                    if (isIPv4) {
                        addresses.add(inetAddress.getHostAddress().toString());
                    }
                }
            }
        }
    } catch (SocketException ex) {
        String LOG_TAG = null;
        Log.e(LOG_TAG, ex.toString());
    }

    return addresses.toArray(new String[0]);
}

From source file:eu.codebits.plasmas.util.NetworkInterfaces.java

/**
 *
 * @param interfaceName//from www.j  a v  a2 s  .  c o  m
 * @return
 */
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)
                continue;//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 (SocketException ex) {
    }
    return "";
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null && wifiInfo.getIpAddress() != 0) {
        return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress());
    } else {//  w w  w .  j  av a2 s  .c o m
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) {
                            return ipAddress;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String getGPRS_IP() {
    try {/*from w ww  .  j ava  2s  .c o  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:camp.pixels.signage.util.NetworkInterfaces.java

public static String getMACAddress(String interfaceName) {
    try {/*from  w  w  w .j av a2s. co  m*/
        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;
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                continue;//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);
            //Log.d("getMACAddress", intf.getName());
            //Log.d("getMACAddress", buf.toString());
            return buf.toString();
        }
    } catch (SocketException ex) {
    }
    return "";
}

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;
        }//from w  w w  .  ja v a  2  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");
}