//通過dns查詢後的結果會緩存起來,成功結果永久緩存,失敗結果會緩存10s,經過下面的方法設置成功和失敗的緩存時間
// 0爲不緩存,-1爲永不過時,其它單位爲s
Security.setProperty("networkaddress.cache.ttl", "10");
Security.setProperty("networkaddress.cache.negative.ttl", "3");緩存
//若是傳入的是ip地址,則不會進行dns查詢
InetAddress address=InetAddress.getByName("www.baidu.com");
//InetAddress.getByName()傳入ip地址,再調用getHostName()不會由於查詢dns失敗而異常
System.out.println(address.getHostName());//www.baidu.com
System.out.println(address.getHostAddress());//180.97.33.107
System.out.println(Arrays.toString(address.getAddress()));//[-76, 97, 33, 107]
System.out.println(address.toString());//www.baidu.com/180.97.33.107網絡
//equals()有相同的ip地址則相等;hashCode()根據ip地址計算測試
//System.out.println(address.equals(address2));
//System.out.println(address.hashCode());spa
//經過echo端口測試機器是否是能夠訪問
address.isReachable(1000);dns
//獲取硬件網絡口的信息
NetworkInterface inter=NetworkInterface.getByName("eth3");
System.out.println(inter.getDisplayName());
System.out.println(inter.getName());ip