網路程式設計——初識ip類InetAddress
阿新 • • 發佈:2021-01-22
獲取InetAddress類
- 通過InetAddress的getByName方法獲取InetAddress
- 通過InetAddress的getLocalHost方法獲取InetAddress
//查詢本機ip地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost") ;
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);
//查詢百度網站ip地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
執行結果:
InetAddress類的兩個常用方法
System.out.println(inetAddress2. getCanonicalHostName());//獲取ip
System.out.println(inetAddress2.getHostName());//獲取域名
注:Canonical是規範的意思
執行結果: