InetAddress类表示互联网协议 (IP) 地址
有两个子类InetAddress InetAddress
通过 InetAddress可以方便获取一个域名下的IP也可以获取一个IP的主机名
下面是例子通过程序查看cto主机的IP信息cto是不让ping的
代码
import javaioIOException;
import InetAddress;
public class TestInetAddress {
public static void main(String[] args) throws IOException {
test();
}
public static void test() throws IOException {
//获取本机的IP地址
InetAddress address = InetAddressgetLocalHost();
Systemoutprintln(addressgetHostAddress());
Systemoutprintln(addresstoString());
Systemoutprintln();
//获取cot的IP地址
InetAddress address = InetAddressgetByName();
Systemoutprintln(getHostAddress:\t + addressgetHostAddress());
Systemoutprintln(getHostName:\t + addressgetHostName());
Systemoutprintln();
//获取我博客的IP地址
InetAddress address = InetAddressgetByName(lav);
Systemoutprintln(getHostAddress:\t + addressgetHostAddress());
Systemoutprintln(getHostName:\t + addressgetHostName());
//直接用ip地址构建
Systemoutprintln(InetAddressgetByName());
Systemoutprintln();
//获取域名下所有的IP地址
//InetAddress[] address = InetAddressgetAllByName();
InetAddress[] address = InetAddressgetAllByName();
for (InetAddress address : address) {
//Systemoutprintln(addressgetHostAddress());
Systemoutprintln(address);
}
InetAddress address = InetAddressgetByName();
Systemoutprintln(addressisReachable());
}
}
运行输出结果
lavasoft/
getHostAddress:
getHostName:
getHostAddress:
getHostName: lav
/
/
/
/
false
Process finished with exit code