Retrieving Network Interfaces in java

NetworkInterface class of java.net package provides a static method getNetworkInterfaces() that retrieves list of all the network interfaces and returns it as an enumeration. Each network interface is this list is an instance of the NetworkInterface class which in turn can have list of sub interfaces and can be retrieved by calling the getSubInterfaces() method.

Example Code

Following code prints the list of all network interfaces and sub interfaces of each network interface if they exists.

/*
 * This example is from javareference.com
 * for more information visit,
 * https://www.javareference.com
 */

import java.net.*;
import java.util.*;

/**
 * This class displays the list of all available network interfaces
 * and sub interfaces on a machine.
 *
 * @author Rahul Sapkal([email protected])
 */
public class NetworkInterfacesRetriever {
  /**
   * Constructor
   */
  public NetworkInterfacesRetriever() {
    try {
      displayInterfaces();
    }
    catch (SocketException se) {
      System.out.println("Socket Exception: " + se.getMessage());
    }
  }

  /**
   * This method prints the machine information and
   * list all the network interfaces with sub interfaces
   * of each network interface if they exists
   *
   * @throws SocketException
   */
  private void displayInterfaces() throws SocketException {
    //Get the Network interfaces using the NetworkInterface class
    try {
      Enumeration<NetworkInterface> ifEnum = NetworkInterface.getNetworkInterfaces();

      InetAddress machine = InetAddress.getLocalHost();
      System.out.println("\nMachine Information");
      System.out.println("------------------------------------------");
      System.out.println("\tMachine Name: " + machine.getHostName());
      System.out.println("\tMachine IP Address: " + machine.getHostAddress());
      System.out.println("------------------------------------------\n\n");

      if (ifEnum != null) {
        System.out.println("Network Interfaces");
        System.out.println("------------------------------------------\n");

        //display the enum
        for (NetworkInterface netIf : Collections.list(ifEnum)) {
          System.out.println("Display Name: " + netIf.getDisplayName());
          System.out.println("Interface Name: " + netIf.getName());

          //get the addresses of this interface
          Enumeration<InetAddress> ifAddrEnum = netIf.getInetAddresses();
          System.out.print("Addresses: ");

          for (InetAddress ifAddr : Collections.list(ifAddrEnum)) {
            System.out.print(ifAddr.toString() + "  ");
          }

          displaySubInterfaces(netIf);

          System.out.println("\n---------------------------------------\n");
        }
      } else {
        //if the enumeration is null, mean no network interfaces were found
        System.out.println("No network interfaces were found");
      }
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
  }

  /**
   * This method takes a network interface as a paramter
   * and prints its sub interfaces if they exists
   *
   * @param netIf
   * @throws SocketException
   */
  private void displaySubInterfaces(NetworkInterface netIf) throws SocketException {
    Enumeration<NetworkInterface> subNetIfs = netIf.getSubInterfaces();

    for (NetworkInterface subNetIf : Collections.list(subNetIfs)) {
      System.out.println("\n\t-------------------------------");
      System.out.println("\tSub Interface Display name: " + subNetIf.getDisplayName());
      System.out.println("\tSub Interface Name: " + subNetIf.getName());
    }
  }

  /**
   * Main method to run as an Application
   *
   * @param args
   */
  public static void main(String[] args) {
    NetworkInterfacesRetriever netIf = new NetworkInterfacesRetriever();
  }
}

Demonstration

The following image depicts the output from the above example.

0

Leave a Reply