InetAddress表示IP地址 其常见的API如下
import java.net.InetAddress;
public class TestDamo {
public static void main(String[] args) throws Exception {
//InetAddress表示IP地址 其常见的API如下
System.out.println(InetAddress.getLocalHost());//返回本主机的地址对象
//DESKTOP-7O00068/192.168.8.38
System.out.println(InetAddress.getByName("www.baidu.com"));
//得到指定主机的IP地址对象,,参数是域名或IP地址
//www.baidu.com/183.232.231.174
System.out.println(InetAddress.getLocalHost().getHostName());//获取此IP地址的主机名
//DESKTOP-7O00068
System.out.println(InetAddress.getLocalHost().getHostAddress());//返回IP地址的字符串
//192.168.8.38
// 测试在5秒内能否ping到接通isReachable(5000)
System.out.println(InetAddress.getByName("www.baidu.com").isReachable(5000));
//true
}
}