Javaで簡単なコンソールピンガーを作成するタスクがあります。次のコードを試しましたが、2つの主な問題があります。
まず、インターネットに接続している場合でも(コンソールから任意のサイトにpingを実行できます)、実行するとコードがfalseを返します。
第二に、pingの応答時間を追跡することは可能ですか?
コードは次のとおりです。
try {
InetAddress address = InetAddress.getByName(the_link);
System.out.println(the_link);
// Try to reach the specified address within the timeout
// periode. If during this periode the address cannot be
// reach then the method returns false.
boolean reachable = address.isReachable(5000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e) {
e.printStackTrace();
}