1

を試すために次のコードを書きましたping。しかし、実行すると、次の例外がスローされます。

java.net.UnknownHostException: http://localhost:8084/server/index.jsp
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at Tester.main(Tester.java:10)

import java.net.InetAddress;

class Tester {
public static void main(String args[]) {
    try {
      InetAddress address = InetAddress.getByName("http://localhost:8084/server/index.jsp");
      boolean isReachable = address.isReachable(2000);
      if(isReachable)
        System.out.println("The address is reachable");
      else
        System.out.println("The address is not reachable");

    } catch(Exception exc) {
       exc.printStackTrace();
      }
}
}

なんでそうなの?サーバーは実行中であり、ページはWebブラウザーで正常に開いています。

4

3 に答える 3

5

問題は次の行にあります。

InetAddress address = InetAddress.getByName(
        "http://localhost:8084/server/index.jsp");

メソッドにはInetAddress.getByName(String)ホスト名が必要です。URL 文字列を指定しました。そのアドレスのホスト名コンポーネントは"localhost".

URL に関連付けられたホストに「ping」する場合は、URL を解析して、次のようなホスト名コンポーネントを抽出する必要があります。

String hostname = new URL(str).getHost();

ただし、URL の形式が正しくない場合や、ホスト名コンポーネントがない場合に対処する必要があります。


ICMP_PING リクエストを(通常は) に送信するのは無意味なので、実際に他のホスト名をテストしようとしていると思います。"localhost"127.0.0.1

于 2012-12-10T12:49:53.523 に答える
0

getByNameは、URLではなく、ホスト名またはIPアドレスをパラメーターとして受け取ります。

于 2012-12-10T12:50:42.267 に答える
0

リクエストfirewallをブロックする背後のドメインping

于 2012-12-10T12:48:10.247 に答える