Skip to content

Instantly share code, notes, and snippets.

@chomado
Created October 24, 2013 15:14
Show Gist options
  • Save chomado/7139018 to your computer and use it in GitHub Desktop.
Save chomado/7139018 to your computer and use it in GitHub Desktop.
複数のネットワークインターフェイスを持つホストのすべてのIPアドレスを取得する。
// 複数のネットワークインターフェイスを持つホストもあるので、すべてのIPアドレスを取得する。
import java.net.*;
public class GetIPAddress2 {
public static void main(String[] args) {
String server_name = "localhost";
if (args.length > 0) {
server_name = args[0];
}
try {
InetAddress[] ia = InetAddress.getAllByName(server_name); //重要
for (int i=0; i<ia.length; i++) {
System.out.println("host name: " + ia[i].getHostName());
System.out.println("IP address: " + ia[i].getHostAddress());
}
} catch (java.io.IOException e) {
e.printStackTrace();
return;
}
}
}
// 実行:
// % java GetIPAddress2 www.google.com
// host name: www.google.com
// IP address: 173.194.38.112
// host name: www.google.com
// IP address: 173.194.38.113
// host name: www.google.com
// IP address: 173.194.38.115
// host name: www.google.com
// IP address: 173.194.38.114
// host name: www.google.com
// IP address: 173.194.38.116
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment