【网络编程】2.InetAddress

1. 相关方法

  • static InetAddress getLocalhost()
    • 获取本机InetAddress对象
  • static InetAddress getByName()
    • 获取指定主机名/域名获取IP地址对象
  • 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());
}
posted @ 2021-07-31 17:37  haojinglei  阅读(62)  评论(0)    收藏  举报