Java 實例 - 獲取指定主機的IP地址
以下實例演示了如何使用 InetAddress 類的 InetAddress.getByName() 方法來獲取指定主機(網址)的IP地址:
Main.java 檔
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIP {
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getByName("www.xuhuhu.com");
}
catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() + "=" + address.getHostAddress());
System.exit(0);
}
}
以上代碼運行輸出結果為:
www.xuhuhu.com=222.73.134.120