NetworkInterface 对象表示物理硬件和虚拟地址网络接口,不能任意构造。与 InetAddress类一样,有一些静态工厂方法可以返回与某个网络接口关联的 NetworkInterface 对象。可以通过 IP 地址、名字或枚举来请求一个 NetworkInterface。
名字的格式与平台有关。在典型的 UNIX 系统上,以太网接口名的形式为 eth0、eth1 等。 本地回送地址的名字可能类似于“lo”。在 Windows 上,名字是类似“CE31”和“ELX100” 的字符串,取自这个特定网络接口的厂商名和硬件模型名。
/*** 类NetworkInterface的基本用法*/public class UseNetIf {public static void main(String[] args) throws SocketException, UnknownHostException {InetAddress address = InetAddress.getByName("127.0.0.1");NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(address);System.out.println(byInetAddress);System.out.println("-----------------------------");Enumeration<NetworkInterface> networkInterfaces= NetworkInterface.getNetworkInterfaces();while(networkInterfaces.hasMoreElements()){NetworkInterface networkInterface = networkInterfaces.nextElement();System.out.println(networkInterface+"===============");Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();while(inetAddresses.hasMoreElements()){System.out.println(networkInterface.getDisplayName()+"-"+inetAddresses.nextElement());}}}}
