发现Java中可以用NetworkInterface类的静态方法getNetworkInterfaces()来获取当前主机的network interface列表,把书上的代码运行了一下发现,windows下有20多个interface,linux(虚拟机中)下则只有lo和enp0s3的结果。
package chap2;
import java.net.*;
import java.util.Enumeration;
public class InetAddressExample {
public static void main(String[] args){
try{
Enumeration<NetworkInterface>interfaceList=NetworkInterface.getNetworkInterfaces();
if(null==interfaceList){
System.out.println("--No intercaces found--");
}else{
while(interfaceList.hasMoreElements()){
NetworkInterface iface=interfaceList.nextElement();
System.out.println("Interface "+iface.getName()+":");
Enumeration<InetAddress> addrList=iface.getInetAddresses();
if(!addrList.hasMoreElements()){
System.out.println("\t(No addresses for this interface)");
}
while(addrList.hasMoreElements()){
InetAddress address=addrList.nextElement();
System.out.print("\tAddress "+((address instanceof Inet4Address?"(v4)"
:(address instanceof Inet6Address?"(v6)":"(?)"))));
System.out.println(": "+address.getHostAddress());
}
}
}
}catch(SocketException se){
System.out.println("Error getting network interfaces:" + se.getMessage());
}
for(String host:args){
try{
System.out.println(host + ":");
InetAddress[] addressList=InetAddress.getAllByName(host);
for(InetAddress address: addressList){
System.out.println("\t"+address.getHostName()+"/"+address.getHostAddress());
}
}catch(UnknownHostException e){
System.out.println("\tUnable to find address for "+host);
}
}
}
}
然后设定args参数为InetAddressExample www.mkp.com blah.blah 129.35.69.7,运行,windows下结果为:
Interface lo:
Address (v4): 127.0.0.1
Address (v6): 0:0:0:0:0:0:0:1
Interface net0:
(No addresses for this interface)
Interface net1:
(No addresses for this interface)
Interface net2:
(No addresses for this interface)
Interface ppp0:
(No addresses for this interface)
Interface eth0:
(No addresses for this interface)
Interface eth1:
(No addresses for this interface)
Interface eth2:
(No addresses for this interface)
Interface ppp1:
(No addresses for this interface)
Interface net3:
(No addresses for this interface)
Interface eth3:
Address (v6): fe80:0:0:0:5877:3f73:9b68:fc3a%11
Interface net4:
Address (v6): fe80:0:0:0:0:100:7f:fffe%12
Interface net5:
Address (v4): 192.168.1.107
Address (v6): fe80:0:0:0:9d04:dd45:b85f:d2d2%13
Interface net6:
(No addresses for this interface)
Interface net7:
(No addresses for this interface)
Interface eth4:
(No addresses for this interface)
Interface eth5:
(No addresses for this interface)
Interface eth6:
Address (v4): 192.168.56.1
Address (v6): fe80:0:0:0:3087:4afc:3312:d234%18
Interface net8:
(No addresses for this interface)
Interface eth7:
(No addresses for this interface)
Interface net9:
Address (v6): fe80:0:0:0:0:5efe:c0a8:3801%21
Interface net10:
Address (v6): fe80:0:0:0:dd85:135e:2753:3e9a%22
Interface eth8:
(No addresses for this interface)
Interface net11:
(No addresses for this interface)
Interface net12:
Address (v6): fe80:0:0:0:0:5efe:c0a8:16b%25
Interface net13:
(No addresses for this interface)
Interface eth9:
(No addresses for this interface)
Interface eth10:
(No addresses for this interface)
Interface eth11:
(No addresses for this interface)
Interface eth12:
(No addresses for this interface)
Interface eth13:
(No addresses for this interface)
Interface eth14:
(No addresses for this interface)
Interface eth15:
(No addresses for this interface)
Interface eth16:
(No addresses for this interface)
Interface eth17:
(No addresses for this interface)
Interface eth18:
(No addresses for this interface)
Interface eth19:
(No addresses for this interface)
Interface eth20:
(No addresses for this interface)
Interface net14:
(No addresses for this interface)
Interface eth21:
(No addresses for this interface)
Interface eth22:
(No addresses for this interface)
Interface eth23:
(No addresses for this interface)
Interface net15:
(No addresses for this interface)
Interface eth24:
(No addresses for this interface)
Interface eth25:
(No addresses for this interface)
Interface eth26:
(No addresses for this interface)
www.baidu.com:
www.baidu.com/115.239.211.110
www.baidu.com/115.239.210.27
blah.blah:
blah.blah/60.191.124.236
129.35.69.7:
129.35.69.7/129.35.69.7
而同样的代码,在虚拟机中的archlinux中运行结果为:
感觉windows下这么多interface,不知道有什么用处?这么多interface正常吗(是不是只有我的电脑上是这样的?我通过无线路由器上网。。)?