可以访问Java,但不会产生与cmd ping相同的结果

时间:2017-08-23 08:40:39

标签: java ping inet

static void pingTestAndUpdate(String ip) throws UnknownHostException, IOException {
    //Check if pingable
    boolean reachable = false;
    reachable = InetAddress.getByName(ip).isReachable(200);
    String status;
    if (reachable == true) {
        status = "Responding";
    }
    else if (reachable == false) {
        status = "Not responding";
    }
    else {
        status = "Error";
    }

    System.out.println(ip + " is: " + status);
}

输出结果为:

  

10.130.72.247是:没有回复

如果我从命令行ping相同的IP地址,我得到:

Pinging 10.130.72.247 with 32 bytes of data:
Reply from 10.130.72.247 bytes=32 time=1ms TTL=128
Reply from 10.130.72.247 bytes=32 time=1ms TTL=128
Reply from 10.130.72.247 bytes=32 time=1ms TTL=128
Reply from 10.130.72.247 bytes=32 time=1ms TTL=128

Ping statistics for 10.130.72.247:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss).
Approximate round trip times in milliseconds:
    Minimum = 1ms, Maximum = 1ms, Average = 1ms

我认为这与我们拥有的子网255.255.252.0有关,但我找不到一个简单的方法来解决这个问题,有人可以帮助我吗?

static void pingTestAndUpdate(String ip) throws UnknownHostException, IOException {
    //To Check if pingable
    boolean reachable;
        reachable = InetAddress.getByName(ip).isReachable(200);
    System.out.println(ip + " is: " + reachable);
}

返回false但是从cmd到同一地址的ping成功。 结果应为true,但即使地址可以访问,目前也是假的。

0 个答案:

没有答案