Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

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

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

From source file:Main.java

public static Collection<InetAddress> getAllAvailableAddresses() {
    Set<InetAddress> retval = new HashSet<InetAddress>();
    Enumeration en;/* w w  w . ja v a 2  s . co m*/

    try {
        en = NetworkInterface.getNetworkInterfaces();
        if (en == null)
            return retval;
        while (en.hasMoreElements()) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            Enumeration<InetAddress> addrs = intf.getInetAddresses();
            while (addrs.hasMoreElements())
                retval.add(addrs.nextElement());
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return retval;
}

From source file:Main.java

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

From source file:Main.java

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

    try {/* w  w w . j av 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();
                allIPAdresses += (i.getHostAddress() + "\n");
            }
        }
    }

    return allIPAdresses;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w  w  w .ja v  a  2 s.  c  o  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

/**
 * Get IP address from first non-localhost interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*w  ww.  jav  a  2s.c  om*/
 */
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

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

    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 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 void getLocalIP() {
    String ipaddress = "";
    try {/*from  w ww .  j  a va 2s .  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()) {
                    ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
    }
}

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 ww  .  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();
                    //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

public static String findAppropriateIp() {
    String result = "unknown";

    Enumeration<NetworkInterface> e;
    try {/* w  w  w. jav  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

public static String GetWantedIPAdress_JAVA() {
    String wantedIP = "";
    Enumeration<NetworkInterface> e = null;

    try {// www .  j  a v  a  2 s  .  co  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;
}