Java Utililty Methods Network Interface Get

List of utility methods to do Network Interface Get

Description

The list of methods to do Network Interface Get are organized into topic(s).

Method

voidenumerateInterfaces()
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, "");
ListgetAllAvailableInterfaces()
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;
ListgetAllAvailableInterfaces()
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()) {
...
ListgetAllAvailableInterfaces()
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()) {
...
SetgetAllInterfaces()
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;
ListgetAllInterfaces()
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;
SetgetAllLocalInterfaces()
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");
NetworkInterfacegetAvailableNetworkInterface()
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;
...
StringgetBroadcast()
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) {
...