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:com.google.appinventor.components.runtime.BlockyTalky.java

private static String getIPAddress(boolean useIPv4) {
    try {/*from   w ww . j ava  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:net.centro.rtb.monitoringcenter.infos.NodeInfo.java

private static String detectLoadBalancerIpAddress() {
    Enumeration<NetworkInterface> networkInterfaces = null;
    try {//from w w  w .j a  v  a 2  s  . c  om
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("Unable to obtain network interfaces!", e);
        return null;
    }

    for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
        boolean isLoopback = false;
        try {
            isLoopback = networkInterface.isLoopback();
        } catch (SocketException e) {
            logger.debug("Unable to identify if a network interface is a loopback or not");
            continue;
        }

        if (isLoopback) {
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (!inetAddress.isLoopbackAddress() && Inet4Address.class.isInstance(inetAddress)) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    }

    return null;
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.Configurator.java

/**
 * Retrieve host name, mac address of the device
 *
 * @return details Map<String, String>
 *//*ww  w .j a v  a2 s . co m*/
protected static Map<String, String> getDeviceDetails() {
    InetAddress ip;
    String hostName = "";
    String macAddress = ConfigConstants.DEFAULT_MAC_ADDRESS;
    Map<String, String> details = new HashMap();
    try {
        ip = InetAddress.getLocalHost();
        hostName = ip.getHostName();
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
            for (; enumeration.hasMoreElements();) {
                InetAddress address = enumeration.nextElement();
                if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()
                        && address.isSiteLocalAddress()) {
                    byte[] mac = networkInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            //Construct mac address
                            sb.append(String.format("%02X%s", mac[i],
                                    (i < mac.length - 1) ? ConfigConstants.DELIMITER : ""));
                        }
                        macAddress = sb.toString();
                        break;
                    }
                }
            }
        }
    } catch (UnknownHostException | SocketException e) {
        log.error("Error while retrieving mac address", e);
        Runtime.getRuntime().exit(1);
    }
    details.put(ConfigConstants.HOST_NAME, hostName);
    details.put(ConfigConstants.MAC_ADDRESS, macAddress);
    return details;
}

From source file:com.max2idea.android.fwknop.Fwknop.java

public static String getLocalIpAddress() {
    try {/*from www .  j  av a  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()) {
                    Log.v("Internal ip", inetAddress.getHostAddress().toString());
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Internal IP", ex.toString());
    }
    return null;
}

From source file:net.grinder.util.NetworkUtils.java

static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
    Enumeration<?> en = getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface i = (NetworkInterface) en.nextElement();
        if (!i.isUp()) {
            continue;
        }//  www  . java 2s. co m
        for (Enumeration<?> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
            InetAddress addr = (InetAddress) en2.nextElement();
            if (!addr.isLoopbackAddress()) {
                if (addr instanceof Inet4Address) {
                    if (preferIPv6) {
                        continue;
                    }
                    return addr;
                }
                if (addr instanceof Inet6Address) {
                    if (preferIpv4) {
                        continue;
                    }
                    return addr;
                }
            }
        }
    }
    return null;
}

From source file:com.vmware.aurora.global.Configuration.java

/**
 * Gets the fqdn of cms server. If it's not presented, the method tries to
 * retrieve the ip address of first network interface and reports it as the
 * fqdn.//from  w w  w  .  j av a  2  s .co m
 * 
 * @return The FQDN of CMS.
 */
public static String getCmsFQDN() {
    String fqdn = Configuration.getString("cms.mgmtnet.fqdn");
    if (fqdn == null || fqdn.isEmpty()) {
        try {
            // try to retrieve the ip addr of eth0
            NetworkInterface net = NetworkInterface.getByName("eth0");
            Enumeration<InetAddress> addresses = net.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr instanceof Inet4Address) {
                    String ip = addr.toString();
                    fqdn = ip.substring(ip.lastIndexOf("/") + 1);
                    break;
                }
            }
        } catch (Exception e) {
            logger.info("Error in retrieving ip of eth0", e);
            throw CommonException.INTERNAL(e);
        }
    }
    return fqdn;
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * Gets the ip address.//from  w  w  w.j  a  va  2 s.  c  o  m
 *
 * @param context Context of application
 * @return Returns ip address
 */
public static String getIPAddress(final Context context) {
    Context appContext = context.getApplicationContext();
    WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager cManager = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo network = cManager.getActiveNetworkInfo();
    String en0Ip = null;
    if (network != null) {
        switch (network.getType()) {
        case ConnectivityManager.TYPE_ETHERNET:
            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 instanceof Inet4Address
                                && !inetAddress.getHostAddress().equals("127.0.0.1")) {
                            en0Ip = inetAddress.getHostAddress();
                            break;
                        }
                    }
                }
            } catch (SocketException e) {
                Log.e("DConnectUtil", "Get Ethernet IP Error", e);
            }
        }
    }

    if (en0Ip != null) {
        return en0Ip;
    } else {
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        return String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
    }
}

From source file:net.grinder.util.NetworkUtils.java

private static List<InetAddress> getAllLocalNonLoopbackAddresses(boolean onlyIPv4) {
    List<InetAddress> addresses = new ArrayList<InetAddress>();
    final Enumeration<NetworkInterface> networkInterfaces;
    try {/*from w w  w  .java 2  s.c o m*/
        networkInterfaces = getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {

            final NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (networkInterface.isUp()) {
                final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    final InetAddress inetAddress = inetAddresses.nextElement();
                    if (onlyIPv4 && inetAddress instanceof Inet6Address) {
                        continue;
                    }
                    if (!inetAddress.isLoopbackAddress()) {
                        addresses.add(inetAddress);
                    }
                }
            }
        }
    } catch (SocketException e) {
        LOGGER.error("Error while resolving non look back local addresses.", e);
    }
    return addresses;
}

From source file:com.carreygroup.JARVIS.Demon.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*from  w w  w .  j a  v  a2  s .  c  o  m*/
 */
public static String getLoaclIPAddress(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:org.basdroid.common.NetworkUtils.java

/**
 * Get IP address from first non-localhost interface
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*from  www .  j a va  2 s .  co m*/
 */
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            List<InetAddress> inetAddresses = Collections.list(intf.getInetAddresses());
            for (InetAddress inetAddr : inetAddresses) {
                if (!inetAddr.isLoopbackAddress()) {
                    String address = inetAddr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(address);
                    if (useIPv4) {
                        if (isIPv4) {
                            return address;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = address.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? address : address.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}