List of utility methods to do Network Interface Get
void | enumerateInterfaces() We must use -Djava.net.preferIPv4Stack=true to get expected results for broadcast address and prefix length. final Enumeration<NetworkInterface> interfaces; for (interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface networkInterface; networkInterface = interfaces.nextElement(); if ((networkInterface.isUp()) && (!networkInterface.isVirtual())) { reportInterface(networkInterface, ""); |
List | getAllAvailableInterfaces() get All Available Interfaces List<NetworkInterface> retval = new ArrayList<NetworkInterface>(10); NetworkInterface intf; for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { intf = (NetworkInterface) en.nextElement(); retval.add(intf); return retval; |
List | getAllAvailableInterfaces() Returns all the available interfaces, including first level sub interfaces. List<NetworkInterface> allInterfaces = new ArrayList<>(); for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces .hasMoreElements();) { NetworkInterface intf = interfaces.nextElement(); allInterfaces.add(intf); Enumeration<NetworkInterface> subInterfaces = intf.getSubInterfaces(); if (subInterfaces != null && subInterfaces.hasMoreElements()) { while (subInterfaces.hasMoreElements()) { ... |
List | getAllAvailableInterfaces() get All Available Interfaces List<NetworkInterface> allInterfaces = new ArrayList<>(); for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces .hasMoreElements();) { NetworkInterface networkInterface = interfaces.nextElement(); allInterfaces.add(networkInterface); Enumeration<NetworkInterface> subInterfaces = networkInterface.getSubInterfaces(); if (subInterfaces.hasMoreElements()) { while (subInterfaces.hasMoreElements()) { ... |
Set | getAllInterfaces() Get all the host interfaces including the loopback one Set<NetworkInterface> result = new HashSet<NetworkInterface>(); try { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { result.add(e.nextElement()); } catch (SocketException e) { return result; |
List | getAllInterfaces() Get all interfaces, including virtual interfaces. List<NetworkInterface> allInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()).stream() .flatMap(m -> { List<NetworkInterface> l = Collections.list(m.getSubInterfaces()); l.add(m); return l.stream(); }).collect(Collectors.toList()); return allInterfaces; |
Set | getAllLocalInterfaces() Get all the interfaces which are not loopback ones Set<NetworkInterface> result = new HashSet<NetworkInterface>(); try { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface ni = e.nextElement(); Enumeration<InetAddress> set = ni.getInetAddresses(); if (set.hasMoreElements()) { InetAddress address = set.nextElement(); ... |
InetAddress[] | getAllLocalUsingNetworkInterface() Utility method that delegates to the methods of NetworkInterface to determine addresses for this machine. try { List<InetAddress> addresses = new ArrayList<InetAddress>(); Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface ni = e.nextElement(); for (Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements();) { addresses.add(e2.nextElement()); return addresses.toArray(new InetAddress[] {}); } catch (SocketException ex) { throw new UnknownHostException("127.0.0.1"); |
NetworkInterface | getAvailableNetworkInterface() get Available Network Interface try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface.isLoopback()) continue; if (networkInterface.isUp()) return networkInterface; ... |
String | getBroadcast() get Broadcast System.setProperty("java.net.preferIPv4Stack", "true"); for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum .hasMoreElements();) { NetworkInterface ni = niEnum.nextElement(); if (!ni.isLoopback()) { for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) { InetAddress bcast = interfaceAddress.getBroadcast(); if (bcast != null) { ... |