package com.iteye.xuj.socket; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class InetAddressExample { public static void main(String[] args) { try { //静态方法getNetworkInterfaces()获取主机的网络接口列表 Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces(); if (interfaceList == null) { System.out.println("--No interfaces found--"); } else { //获取并打印出列表中每个接口的地址 while (interfaceList.hasMoreElements()) { NetworkInterface iface = interfaceList.nextElement(); //getName()方法为接口返回一个本地名称 System.out.println("Interface " + iface.getName() + ":"); //getInetAddresses()方法获取与接口相关联的地址 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)" : "(?)")))); //getHostAddress()方法返回一个字符串来代表主机的数字型地址 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); } } } }
运行时参数:127.0.0.1 localhost
输出结果:
Interface lo: Address (v6): 0:0:0:0:0:0:0:1 Address (v4): 127.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:d801:2a2c:c88:70ef%11 Address (v4): 172.16.15.199 Interface net4: Address (v6): fe80:0:0:0:0:5efe:ac10:fc7%12 Interface net5: Address (v6): 2001:0:5ef5:79fd:1000:1d40:53ef:f038 Address (v6): fe80:0:0:0:1000:1d40:53ef:f038%13 Interface eth4: (No addresses for this interface) Interface eth5: (No addresses for this interface) Interface eth6: (No addresses for this interface) Interface eth7: (No addresses for this interface) Interface eth8: (No addresses for this interface) 127.0.0.1: 127.0.0.1/127.0.0.1 localhost: localhost/127.0.0.1 localhost/0:0:0:0:0:0:0:1
InetAddress: 创建和访问
// getAllByName() 方法用于返回一组与给定主机名相关联的所有地址的实例 static InetAddress[ ] getAllByName(String host) // getAddress() 方法返回一个适当长度的字节数组,代表地址的二进制的形式 static InetAddress getByName(String host) static InetAddress getLocalHost() byte[] getAddress()
InetAddress: 字符串表示
String toString() String getHostAddress() String getHostName() String getCanonicalHostName()
InetAddress: 检测属性
boolean isAnyLocalAddress() boolean isLinkLocalAddress() boolean isLoopbackAddress() boolean isMulticastAddress() boolean isMCGlobal() boolean isMCLinkLocal() boolean isMCNodeLocal() boolean isMCOrgLocal() boolean isMCSiteLocal() boolean isReachable(int timeout) boolean isReachable(NetworkInterface netif, int ttl, int timeout)
上述前三个方法分别检查地址实例是否属于 " 任意 " 本地地址, 本地链接地址, 以及回环地址
(匹配 127.*.*.* 或 ::1 的形式)。第 4 个方法检查其是否为一个多播地址,而 isMC...() 形式的方法检测多播地址的各种范围( scopes ) 。 最后两个方法检查是否真能与 InetAddress 地址确定的主机进行数据报文交换。
NetworkInterface: 创建, 获取信息
static Enumeration<NetworkInterface> getNetworkInterfaces() static NetworkInterface getByInetAddress(InetAddress addr) static NetworkInterface getByName(String name) Enumeration<InetAddress> getInetAddresses() String getName()
String getDisplayName()
上面第一个方法非常有用,使用它可以很容易获取到运行程序的主机的 IP 地址:通过 getNetworkInterfaces() 方法可以获取一个接口列表,再使用实例的 getInetAddresses() 方法就可以获取每个接口的所有地址。 注意: 这个列表包含了主机的所有接口, 包括不能够向网络中的其他主机发送或接收消息的虚拟回环接口。 同样, 列表中可能还包括外部不可达的本地链接地址。 由于这些列表都是无序的, 所以你不能简单地认为, 列表中第一个接口的第一个地址一定能够通过互联网访问,而是要通过前面提到的 InetAddress 类的属性检查方法,来判断一个地址不是回环地址,不是本地链接地址等等。
getName() 方法返回一个接口( interface ) 的名字(不是主机名)。