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 String getIpWithoutWifi() {
    try {//from  w ww . j  ava2  s.co m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumeration = intf.getInetAddresses(); en.hasMoreElements();) {
                InetAddress inetAddress = enumeration.nextElement();
                if (inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "getIpWithoutWifi error:" + e.getMessage());
    }
    return null;
}

From source file:Main.java

public static String getIfIpAddress() {
    String result = "";
    try {//from www .  j av a2s  . 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();
                //                  Log.v("","ip="+inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress() && (inetAddress.getHostAddress().startsWith("0")
                        || inetAddress.getHostAddress().startsWith("1")
                        || inetAddress.getHostAddress().startsWith("2"))) {
                    result = inetAddress.getHostAddress();
                    break;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
        result = "192.168.0.1";
    }
    //      Log.v("","getIfIpAddress result="+result);
    if (result.equals(""))
        result = "192.168.0.1";
    return result;
}

From source file:Main.java

/** get first valid ipv4 address of this device */
public static String getLocalIpAddress() {
    try {/*from  www.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() && validIP(inetAddress.getHostAddress())) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
    return null;

}

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface() {

    Enumeration<NetworkInterface> interfaces = null;
    try {//from   w w w .j  a  v  a2 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.
         */
        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 Set<InetAddress> getLocalAddresses() {
    Set<InetAddress> addresses = new HashSet<InetAddress>();

    Enumeration<NetworkInterface> networkInterfaces;
    try {//www .  j  a v  a 2s  . com
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        Log.v(TAG, "getNetworkInterfaces(): " + e.getMessage(), e);
        return null;
    }

    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        Enumeration<InetAddress> addressEnum = networkInterface.getInetAddresses();
        while (addressEnum.hasMoreElements()) {
            addresses.add(addressEnum.nextElement());
        }
    }

    return addresses;
}

From source file:com.splout.db.common.GetIPAddresses.java

/**
 * Returns all available IP addresses./*from   w ww. ja  v  a2 s.  c om*/
 * <p/>
 * In error case or if no network connection is established, we return an empty list here.
 * <p/>
 * Loopback addresses are excluded - so 127.0.0.1 will not be never returned.
 * <p/>
 * The "primary" IP might not be the first one in the returned list.
 *
 * @return Returns all IP addresses (can be an empty list in error case or if network connection is missing).
 * @throws SocketException
 * @since 0.1.0
 */
public static Collection<InetAddress> getAllLocalIPs() throws SocketException {
    LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>();

    Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
    if (nifs == null)
        return listAdr;

    while (nifs.hasMoreElements()) {
        NetworkInterface nif = nifs.nextElement();
        // We ignore subinterfaces - as not yet needed.
        Enumeration<InetAddress> adrs = nif.getInetAddresses();
        while (adrs.hasMoreElements()) {
            InetAddress adr = adrs.nextElement();
            if (adr != null && !adr.isLoopbackAddress()
                    && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) {
                log.info("Available site local address: " + adr);
                listAdr.add(adr);
            }
        }
    }
    return listAdr;
}

From source file:Main.java

public static String getLocalIpAddress() {
    String result = "";
    boolean exit = false;
    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() && !(inetAddress.toString().indexOf(":")>=0)) {
                //                       return inetAddress.getHostAddress().toString();
                //                   }
                //                  Log.v("","ip="+inetAddress.getHostAddress()+
                //                        ", name="+intf.getName());
                if (inetAddress.isSiteLocalAddress()) {
                    result = inetAddress.getHostAddress();
                    //                       Log.v("","result="+result+", name="+intf.getName()+"-");
                    if (intf.getName().equals("wlan0")) {
                        exit = true;
                        break;
                    }
                }
            }
            if (exit)
                break;
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
        result = "192.168.0.1";
    }
    //      Log.v("","getLocalIpAddress result="+result);
    if (result.equals(""))
        result = "192.168.0.1";
    return result;
}

From source file:com.wmfsystem.eurekaserver.broadcast.Server.java

public static Set<InetAddress> getLocalAddress() throws SocketException {
    Set<InetAddress> address = new HashSet<>();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();

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

            if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) {
                address.add(addr);/*from  w w w .j a  v  a  2s  .c  o  m*/
            }
        }
    }

    return address;
}

From source file:org.siddhiesb.transport.passthru.util.PassThroughTransportUtils.java

/**
 * Whatever this method returns as the IP is ignored by the actual http/s listener when
 * its getServiceEPR is invoked. This was originally copied from axis2
 *
 * @return Returns String.//w  w  w .ja va  2  s . c  o m
 * @throws java.net.SocketException if the socket can not be accessed
 */
public static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";
    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        Enumeration addresses = netface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress ip = (InetAddress) addresses.nextElement();
            if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
                return ip.getHostAddress();
            }
        }
    }
    return address;
}

From source file:com.air.mobilebrowser.NetworkUtil.java

public static String getIPAddress() {
    boolean useIPv4 = true;
    try {/*from   w  w w  . 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 "";
}