Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active December 26, 2015 10:39
Show Gist options
  • Save chomado/7138364 to your computer and use it in GitHub Desktop.
Save chomado/7138364 to your computer and use it in GitHub Desktop.
アドレスを調べる
import java.net.*;
public class GetIPAddress {
public static void main(String[] args) {
String server_name = "localhost";
if (args.length > 0) {
server_name = args[0];
}
try {
InetAddress ia = InetAddress.getByName(server_name);
System.out.println("host name: " + ia.getHostName());
System.out.println("IP address: " + ia.getHostAddress());
} catch (java.io.IOException e) {
e.printStackTrace();
return;
}
}
// 実行:
// % java GetIPAddress www.yahoo.co.jp
// host name: www.yahoo.co.jp
// IP address: 124.83.171.240
// % java GetIPAddress 124.83.171.240
// host name: f7.top.vip.ogk.yahoo.co.jp
// IP address: 124.83.171.240
// % java GetIPAddress localhost
// host name: localhost
// IP address: 127.0.0.1
// % java GetIPAddress www.yaaaahoo.co.jp // 存在しないホスト
// java.net.UnknownHostException: www.yaaaahoo.co.jp
// at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
// at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:848)
// at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
// at java.net.InetAddress.getAllByName0(InetAddress.java:1152)
// at java.net.InetAddress.getAllByName(InetAddress.java:1082)
// at java.net.InetAddress.getAllByName(InetAddress.java:1018)
// at java.net.InetAddress.getByName(InetAddress.java:968)
// at GetIPAddress.main(GetIPAddress.java:9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment