JSP中如何统计ping主机的次数?

发布于 2024-12-24 17:13:37 字数 461 浏览 0 评论 0原文

我的代码在这里,我从中得到的结果是真还是假,无论它是否 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-12-31 17:13:37
final static int MAX_PINGS = 10;
final static int TIMEOUT= 10000;
int countFailed = 0;

for (int i=0; i<MAX_PINGS; i++){
    if (address.isReachable(TIMEOUT)){
         System.out.println("Pinged successfully");
         break;
    }else{
         countFailed++;
    }
 }

注意:给出 10000 毫秒(10 秒)作为超时时间太多了。我建议应该在 1000 毫秒左右。

final static int MAX_PINGS = 10;
final static int TIMEOUT= 10000;
int countFailed = 0;

for (int i=0; i<MAX_PINGS; i++){
    if (address.isReachable(TIMEOUT)){
         System.out.println("Pinged successfully");
         break;
    }else{
         countFailed++;
    }
 }

Note: giving 10000ms (10 seconds) as timeout is too much. I suggest it should be around 1000 ms.

能否归途做我良人 2024-12-31 17:13:37

假设 address.isReachable(10000)) 正在执行 ping,并返回 true 或 false,那么您想要这样的东西:

int counter = 0;

do
{
    counter ++; 
    if(address.isReachable(10000))
    {
        break;
    }
}
while (counter < 10)

// now counter contains the number of attempts

我认为您最好找到一本关于编程的好书,以提出与此类似的解决方案不应该是您需要询问的问题。

Assuming that address.isReachable(10000)) is doing the ping, and returns true or false, then you want something like this:

int counter = 0;

do
{
    counter ++; 
    if(address.isReachable(10000))
    {
        break;
    }
}
while (counter < 10)

// now counter contains the number of attempts

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.

怎会甘心 2024-12-31 17:13:37

我首先会问为什么这段代码需要驻留在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文