java inetaddress isreachable显示意外结果 [英] java inetaddress isreachable showing unexpected results

查看:122
本文介绍了java inetaddress isreachable显示意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每10秒钟内循环ping 200个IP。因此,此循环每10秒执行一次。我正在使用此代码来ping IP.



I am trying to ping 200s of IPs in a loop in every 10 seconds. So, this loop gets executed in every 10 seconds. I was using this code to ping the IPs

for (i = 0; i <= 200; i++ )
{
   ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", *SOMEIP*);
   Process proc = processBuilder.start();

   int returnVal = proc.waitFor();
}





这只是我代码的一部分。我正在为每个ping创建单独的线程,因为如果100个IP关闭,那么ping每个并且需要超过300秒。每个IP顺序。所以,创建了单独的线程。但问题是,无论何时执行循环,PC的CPU使用率都会达到90%,这对关键系统来说并不好。所以我把程序更改为这个。





This is just the part of my code. I am creating separate thread for every ping because if 100 IPs are down, then it will take more than 300 seconds to ping each & every IP sequentially. So, created separate threads. But the problem was that whenever the loop gets executed, then the PCs CPU usage reaches to 90%, which is not good for a critical system. So I change the program to this one.

for (i = 0; i <= 200; i++ )
{
   InetAddress inet = InetAddress.getByName(*SOMEIP*);
   System.out.println(inet.isReachable(3000) ? "Host is reachable" : "Host is NOT reachable");

}



在上面的代码中,由于同样的问题,我正在为每个Ping创建单独的线程。现在,我在这里遇到另一个问题。此代码给出了意想不到的结果。可ping的IP有时也使用上面的代码显示无法访问。为什么会这样?



这个问题还有其他解决方案吗?



谢谢


In the above code also, I am creating separate thread for each Ping because of the same problem. Now, here I am getting another problem. This code is giving unexpected result. The IPs which are pingable are also showing not reachable sometimes using above code. Why this is happening ?

Is there any other solution to this problem ?

Thanks

推荐答案

请参阅 https://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#isReachable(int) [ ^ ] 。你究竟想要实现什么目标?
See the comments at https://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#isReachable(int)[^]. What exactly are you trying to achieve?


这篇关于java inetaddress isreachable显示意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆