Java サンプル - 指定したホストのIPアドレスを取得
以下のサンプルでは、InetAddress クラスの InetAddress.getByName() メソッドを使用して、指定したホスト(URL)の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.example.com");
}
catch (UnknownHostException e) {
System.exit(2);
}
System.out.println(address.getHostName() + "=" + address.getHostAddress());
System.exit(0);
}
}
上記のコードを実行すると、出力結果は次のようになります:
www.example.com=222.73.134.120その他の拡張
Java サンプル