JSP中如何统计ping主机的次数?
我的代码在这里,我从中得到的结果是真还是假,无论它是否 ping 到我在其中提到的主机,
try
{
InetAddress address = InetAddress.getByName("192.168.1.125");
boolean reachable=address.isReachable(10000));
out.print(PingHost.DrawTable());
out.print("Is host reachable? " + reachable);
}
catch(Exception e)
{
out.print(e.printStackTrace());
}
我想计算它尝试 ping 主机的次数(如果它没有完全成功 ping 通)第一次,最大 ping 次数为 10
希望您的建议
提前致谢
My code is here from which I get result either true or false that weather it ping to the host I mention in it or not,
try
{
InetAddress address = InetAddress.getByName("192.168.1.125");
boolean reachable=address.isReachable(10000));
out.print(PingHost.DrawTable());
out.print("Is host reachable? " + reachable);
}
catch(Exception e)
{
out.print(e.printStackTrace());
}
I want to count the no of times it try to ping the host if it is not ping success fully fro the first time and the max no of count for ping would be 10
Hopes for your suggestions
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:给出 10000 毫秒(10 秒)作为超时时间太多了。我建议应该在 1000 毫秒左右。
Note: giving 10000ms (10 seconds) as timeout is too much. I suggest it should be around 1000 ms.
假设
address.isReachable(10000))
正在执行 ping,并返回 true 或 false,那么您想要这样的东西:我认为您最好找到一本关于编程的好书,以提出与此类似的解决方案不应该是您需要询问的问题。
Assuming that
address.isReachable(10000))
is doing the ping, and returns true or false, then you want something like this:I think you'd do well to find a good book on programming, to come up with a solution similar to this should not be something you need to ask about.
我首先会问为什么这段代码需要驻留在 JSP 中。如果主机无法访问,则对该 JSP 的请求将永远无法返回给您。任何使用成员变量来跟踪计数的解决方案也会出现问题,因为它会遇到并发问题。
您最好在 servlet 上编写 LaceySnr 的 代码 并在单独的线程上生成该代码。
I would first question why this code needs to reside in a JSP. A request to this JSP will take forever to get back to you if the host is unreachable. Any solution that uses a member variable to track the count will also be problematic since it will run into concurrency issues.
You are better off writing LaceySnr's code on a servlet and spawning that code on a separate thread.