在Java中ping多个服务器 [英] Ping multiple servers in Java

查看:430
本文介绍了在Java中ping多个服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序向服务器发送ping请求。该列表很大,如果 IP 无法访问,则需要时间转到下一个 IP

I have a program which sends a ping request to the servers. The list is large and if an IP is unreachable it takes time to go to the next IP.

我希望,对于每个 IP ,它应该创建一个新线程&同时处理所有这些。

I wish that, for every IP, it should create a new thread & process all of them simultaneously.

以下是代码:

for (int i = 0; i < 89; i++)
{
     ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", buttons[i].getText());
     Process proc = processBuilder.start();
     returnVal = proc.waitFor();                       
}

如何使此代码ping所有 IP s,每个都在一个单独的线程中?

How can I make this code to ping all IPs, each in a separate thread ?

推荐答案

如果没有怎么办?其他人也建议使用ProcessBuilder

我有三个类 - PingParallel 是我的主类, PingTask 是每个线程执行的任务, PingResult 是否有结果代码(我们可以添加更多信息,状态信息等)。

I have three classes - PingParallel is my main class, PingTask is the task performed by each thread, and PingResult is having result code (we can add some more info also, status message etc.).


PingParallel



package com.test.thread;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class PingParallel {

    public static void main(String[] args) {
        int totalIps = 89;
        ExecutorService executor = Executors.newFixedThreadPool(totalIps);
        List<Future<PingResult>> list = new ArrayList<Future<PingResult>>();
        Callable<PingResult> callable = null;
        for(int i=0; i< totalIps; i++){
            callable = new PingTask("127.0.0"+i); // Get the ipAddres buttons[i].getText());
            Future<PingResult> future = executor.submit(callable);
            list.add(future);
        }
        for(Future<PingResult> fut : list){
            try {
                System.out.println(new Date()+ "::"+fut.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }
}




PingTask



package com.test.thread;

import java.net.InetAddress;
import java.util.concurrent.Callable;

public class PingTask implements Callable<PingResult> {

  private String ipAddress;

  public PingTask(String ipAddress) {
    this.ipAddress = ipAddress;
  }

  @Override
  public PingResult call() {
    InetAddress inet = null;
    try {
      inet = InetAddress.getByName(ipAddress);
      int resultCode = inet.isReachable(5000) ? 0 : -1;
      return new PingResult(ipAddress, resultCode);
    } catch (Exception e) {
      e.printStackTrace();
      return new PingResult(ipAddress, -1);
    }
  }
}




PingResult



package com.test.thread;

public class PingResult {

  private String ipAddress;
  private int resultCode;

  public PingResult(String ipAddress, int resultCode) {
    this.ipAddress = ipAddress;
    this.resultCode = resultCode;
  }

  public String getIpAddress() {
    return ipAddress;
  }

  public int getResultCode() {
    return resultCode;
  }

  public String toString() {
    return "IpAddress :: "+ ipAddress + " Result Code : "+ resultCode;
  }
}

这篇关于在Java中ping多个服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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