1. getAllByName(String host)
给定主机名的所有 IP 地址所组成的数组。
2. getByName(String host)
给定主机名的 IP 地址。
3. getLocalHost()
本地主机的 IP 地址。
4.getLoopbackAddress()
返回回送地址
下面给出测试程序:
package com.niepengfei.test;
import java.net.InetAddress;
public class TestInetAddress {
public static void main(String[] args) throws Exception {
InetAddress[] inetAddressArray = InetAddress.getAllByName("baidu.com");
for(int i=0;i<inetAddressArray.length;i++){
InetAddress addr = inetAddressArray[i];
System.out.println(addr.getHostName()+" "+addr.getHostAddress());
}
System.out.println("-----------------------------------------------");
InetAddress address = InetAddress.getByName("baidu.com");
System.out.println(address.getHostName()+" "+address.getHostAddress());
System.out.println("-----------------------------------------------");
InetAddress locaAddress = InetAddress.getLocalHost();
System.out.println(locaAddress.getHostName()+" "+locaAddress.getHostAddress());
System.out.println("-----------------------------------------------");
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
System.out.println(loopbackAddress.getHostName()+" "+loopbackAddress.getHostAddress());
}
}
测试结果如下: