Here you can find the source of enumerateInterfaces()
Parameter | Description |
---|---|
SocketException | an exception |
public static void enumerateInterfaces() throws SocketException
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class Main { /********************************************************************************************** * We must use -Djava.net.preferIPv4Stack=true to get expected results * for broadcast address and prefix length. *//from w ww.ja v a2 s .co m * @throws SocketException */ public static void enumerateInterfaces() throws SocketException { final Enumeration<NetworkInterface> interfaces; // Returns all the interfaces on this machine. // Returns null if no network interfaces could be found on this machine. for (interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface networkInterface; networkInterface = interfaces.nextElement(); if ((networkInterface.isUp()) && (!networkInterface.isVirtual())) { // Begin with no indent reportInterface(networkInterface, ""); } } } /*********************************************************************************************** * Show all details of a Network Interface. * May be called recursively. * * @param networkinterface * @param indent * * @throws SocketException */ private static void reportInterface(final NetworkInterface networkinterface, String indent) throws SocketException { final byte[] arrayMAC; final Enumeration<NetworkInterface> interfaces; // Get a List of all or a subset of the InterfaceAddresses of this network interface // Get an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface if ((networkinterface.getInterfaceAddresses().isEmpty()) && (!networkinterface.getSubInterfaces().hasMoreElements())) { return; // has no addresses or child interfaces } System.out.print(indent); System.out.print("/"); System.out.print(networkinterface.getDisplayName()); System.out.print("/"); // Returns whether a network interface is a loopback interface if (networkinterface.isLoopback()) { System.out.print(" [loopback]"); } // Returns whether a network interface is a point to point interface if (networkinterface.isPointToPoint()) { System.out.print(" [ptp]"); } // Returns the hardware address (usually MAC) of the interface if it has one // and if it can be accessed given the current privileges arrayMAC = networkinterface.getHardwareAddress(); if ((arrayMAC != null) && (arrayMAC.length > 0)) { System.out.print(", hardware="); for (final byte byteMAC : arrayMAC) { System.out.print(':'); System.out.print(Integer.toHexString(byteMAC & 0xff)); } } System.out.println(); indent = indent + " "; // Now the InterfaceAddress information for (final InterfaceAddress interfaceAddress : networkinterface.getInterfaceAddresses()) { System.out.print(indent); System.out.print(interfaceAddress.getAddress()); // Returns an InetAddress for this address if (interfaceAddress.getAddress().isLoopbackAddress()) { System.out.print(" [loopback]"); } System.out.print(", broadcast="); System.out.print(interfaceAddress.getBroadcast()); System.out.print(", prefixLength="); System.out.print(interfaceAddress.getNetworkPrefixLength()); //System.out.print(", searchedBroadcast="); //System.out.print(BroadcastAddress.instance().getBroadcastAddress(addr.getAddress())); try { final String strHostname; strHostname = interfaceAddress.getAddress().getCanonicalHostName(); if (strHostname != null) { System.out.print(", host="); System.out.print(strHostname); } } catch (Exception exception) { // Just ignore any Exceptions } System.out.println(); } // Now recurse down through the subinterfaces for (interfaces = networkinterface.getSubInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface childInterface; childInterface = interfaces.nextElement(); if (childInterface.isUp()) { reportInterface(childInterface, indent); } } } }