1. 相关方法
- static InetAddress getLocalhost()
- static InetAddress getByName()
- String getHostName()
- String getHostAddress()
2. 案例
@Test
public void testInetAddress() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
// 输出 DESKTOP-SA9MTAS/169.254.252.83
System.out.println(localHost);
InetAddress baidu = InetAddress.getByName("www.baidu.com");
// 输出 www.baidu.com/110.242.68.4
System.out.println(baidu);
// 输出 www.baidu.com
System.out.println(baidu.getHostName());
// 110.242.68.4
System.out.println(baidu.getHostAddress());
}