List of usage examples for java.net InetAddress isLoopbackAddress
public boolean isLoopbackAddress()
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 {//www . j a va 2 s . com 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:de.madvertise.android.sdk.MadvertiseUtil.java
/** * Fetch the address of the enabled interface * /*from w w w . j av a 2 s . com*/ * @return ip address as string */ public static String getLocalIpAddress(MadvertiseViewCallbackListener listener) { 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()) { if (inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress().toString(); } } } } } catch (SocketException e) { if (listener != null) { listener.onError(e); } e.printStackTrace(); } if (listener != null) { listener.onError(new IllegalArgumentException("Couldn't obtain the local ip address")); } return ""; }
From source file:hu.netmind.beankeeper.node.impl.NodeManagerImpl.java
/** * Get the server addresses from interfaces. *//*from w w w . j a va 2 s . c o m*/ public static String getHostAddresses() { try { Enumeration interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); // Copy from enumeration to addresses vector, but filter loopback addresses ArrayList addresses = new ArrayList(); while (interfaceEnumeration.hasMoreElements()) { NetworkInterface intf = (NetworkInterface) interfaceEnumeration.nextElement(); // Remove loopback addresses Enumeration addressEnumeration = intf.getInetAddresses(); while (addressEnumeration.hasMoreElements()) { InetAddress address = (InetAddress) addressEnumeration.nextElement(); // Insert to addresses only if not loopback and not link local if ((!address.isLoopbackAddress()) && (!address.isLinkLocalAddress())) addresses.add(address); } } // Pick one address from the remaining address space logger.debug("server available local addresses: " + addresses); // Now, multiple addresses are in the list, so copy all of them // into the result string. StringBuffer ips = new StringBuffer(); for (int i = 0; i < addresses.size(); i++) { InetAddress address = (InetAddress) addresses.get(i); if (ips.length() > 0) ips.append(","); ips.append(address.getHostAddress()); } return ips.toString(); } catch (StoreException e) { throw e; } catch (Exception e) { throw new StoreException("exception while determining server address", e); } }
From source file:org.brandroid.openmanager.fragments.DialogHandler.java
private static String getNetworkInterfaces() { String ret = "IP Address:"; try {/* ww w . j av a 2 s .c o m*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface ni = en.nextElement(); for (Enumeration<InetAddress> enumIP = ni.getInetAddresses(); enumIP.hasMoreElements();) { InetAddress ip = enumIP.nextElement(); if (!ip.isLoopbackAddress()) ret += " " + ip.getHostAddress(); } } } catch (SocketException e) { Logger.LogError("Couldn't get Network Interfaces", e); } ret += "\n"; return ret; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static String getIp() { String localip = null;// IP?IP String netip = null;// IP try {//w w w. j a va2s . c o m Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// ?IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP localip = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
From source file:org.apache.axis2.util.Utils.java
/** * Returns the ip address to be used for the replyto epr * CAUTION:/*from w ww. j a v a 2 s . co m*/ * This will go through all the available network interfaces and will try to return an ip address. * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found * then this will return this will return 127.0.0.1. * This will <b>not<b> consider IPv6 addresses. * <p/> * TODO: * - Improve this logic to genaralize it a bit more * - Obtain the ip to be used here from the Call API * * @return Returns String. * @throws java.net.SocketException */ 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.mozilla.SUTAgentAndroid.SUTAgentAndroid.java
public static InetAddress getLocalInetAddress() 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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress; }// w w w . j a v a 2 s .c o m } } return null; }
From source file:org.adblockplus.android.ProxyService.java
/** * Checks if specified host is local.//w ww . jav a 2 s . c o m */ private static final boolean isLocalHost(String host) { if (host == null) return false; try { if (host.equalsIgnoreCase("localhost")) return true; String className = "android.net.NetworkUtils"; Class<?> c = Class.forName(className); /* * InetAddress address = NetworkUtils.numericToInetAddress(host); */ Method method = c.getMethod("numericToInetAddress", String.class); InetAddress address = (InetAddress) method.invoke(null, host); if (address.isLoopbackAddress()) return true; } catch (Exception e) { Log.w(TAG, null, e); } return false; }
From source file:com.datatorrent.stram.client.StramClientUtils.java
public static InetSocketAddress getRMWebAddress(Configuration conf, boolean sslEnabled, String rmId) { rmId = (rmId == null) ? "" : ("." + rmId); InetSocketAddress address;/* w w w . ja v a 2 s. c o m*/ if (sslEnabled) { address = conf.getSocketAddr(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + rmId, YarnConfiguration.DEFAULT_RM_WEBAPP_HTTPS_ADDRESS, YarnConfiguration.DEFAULT_RM_WEBAPP_HTTPS_PORT); } else { address = conf.getSocketAddr(YarnConfiguration.RM_WEBAPP_ADDRESS + rmId, YarnConfiguration.DEFAULT_RM_WEBAPP_ADDRESS, YarnConfiguration.DEFAULT_RM_WEBAPP_PORT); } LOG.info("rm webapp address setting {}", address); LOG.debug("rm setting sources {}", conf.getPropertySources(YarnConfiguration.RM_WEBAPP_ADDRESS)); InetSocketAddress resolvedSocketAddress = NetUtils.getConnectAddress(address); InetAddress resolved = resolvedSocketAddress.getAddress(); if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) { try { resolvedSocketAddress = InetSocketAddress .createUnresolved(InetAddress.getLocalHost().getCanonicalHostName(), address.getPort()); } catch (UnknownHostException e) { //Ignore and fallback. } } return resolvedSocketAddress; }
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Given an InetAddress, checks to see if the address is a local address, by comparing the address with all the * interfaces on the node./*from w ww . j a v a 2 s .c o m*/ * * @param addr address to check if it is local node's address * * @return true if the address corresponds to the local node */ public static boolean isLocalAddress(InetAddress addr) { // Check if the address is any local or loop back boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress(); // Check if the address is defined on any interface if (!local) { try { local = NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { local = false; } } return local; }