Programmatic Access to Network Parameters : NetworkInterface « Network « Java Tutorial






import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

public class Main {
  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
      displayInterfaceInformation(netint);
    }
  }

  private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
      System.out.printf("InetAddress: %s%n", inetAddress);
    }
    System.out.printf("%n");
  }
}








19.8.NetworkInterface
19.8.1.Interface Lister
19.8.2.Programmatic Access to Network Parameters
19.8.3.An updated version of the ListNets program that uses the NetworkInterface enhancements
19.8.4.Get MAC address of a host: java.net.NetworkInterface.getHardwareAddress().
19.8.5.Report By Name
19.8.6.Enumeration of NetworkInterface
19.8.7.Report By Address
19.8.8.NetworkInterface