List of utility methods to do Network Interface Check
boolean | isLoopbackNetworkInterface(NetworkInterface anInterface) is Loopback Network Interface if (anInterface == null) return false; if (isLoopbackMethod != null) { try { return (Boolean) isLoopbackMethod.invoke(anInterface); } catch (Throwable t) { boolean hasLoopback = false; Enumeration<InetAddress> allIntfAddr = anInterface.getInetAddresses(); while (allIntfAddr.hasMoreElements()) { InetAddress anAddr = allIntfAddr.nextElement(); if (anAddr.isLoopbackAddress()) { hasLoopback = true; break; return hasLoopback; |
boolean | isNetworkAvailable() Short method to check whether or not the user has an active internet connection try { final URL url = new URL("http://www.google.com"); final URLConnection conn = url.openConnection(); conn.connect(); return true; } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { ... |
boolean | isNetworkException(Exception e) is Network Exception if (e == null) { return false; if (e instanceof SocketException) { return true; if (e instanceof SocketTimeoutException) { return true; ... |
boolean | isNetworkProblem(final Throwable ex) is Network Problem if (ex == null) { return false; } else if (ex instanceof SocketTimeoutException || ex instanceof SocketException) { return true; } else { return isNetworkProblem(ex.getCause()); |
boolean | isUpAndNotLoopback(final NetworkInterface ni) is Up And Not Loopback return ni != null && !ni.isLoopback() && ni.isUp();
|
void | printInterface(NetworkInterface netIf) print Interface out.printf("Display name: %s\n", netIf.getDisplayName()); out.printf("Name: %s\n", netIf.getName()); displaySubInterfaces(netIf); out.printf("\n"); |
Map | retrieveNetworkInterfaces() Retrieves all InetAddress for the running VM, indexed by their name. final Map<String, InetAddress> namedAddresses = new HashMap<String, InetAddress>(); final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) { final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { if ((inetAddress instanceof Inet6Address) || inetAddress.isAnyLocalAddress() || inetAddress.isLinkLocalAddress() || inetAddress.isLoopbackAddress() || inetAddress.isMulticastAddress()) { ... |
void | sortInterfaces(List sort Interfaces Collections.sort(interfaces, new Comparator<NetworkInterface>() { @Override public int compare(NetworkInterface o1, NetworkInterface o2) { return Integer.compare(o1.getIndex(), o2.getIndex()); }); |