InetAddress.isReachable(超时)无法访问其他域中的主机

时间:2017-11-17 08:35:59

标签: java multithreading sockets dns ping

我正在开发一个关于创建一个pinger的项目,该项目将在给定.csv文件的情况下大约ping 400 usps。 .csv文件传递给JTable,对于该表的每一行,我创建并运行将IP ping到java程序正在运行的另一个域的线程。

问题是,无法通过java.net.InetAddress类的isReachable()方法访问这些IP。我可以从同一台PC的cmd.exe ping ping命令,该程序与java程序运行的.csv文件成功引用的IP相同。

我使用了3种不同的方法来测试我的程序(原始的isReachable(),一种使用套接字试图在端口7上打开套接字的方法,以及使用Windows ping命令的最后一种方法)并且只使用一种方法使用windows的ping命令管理到达所有IP。

那说程序有效,但它使用了太多资源,因为现在我为每个线程打开了一个让Win ping命令工作的进程。另外,因为我正在测量ping的响应时间,所以它远离真正的ping命令。

请注意,使用Java的isReachable()方法并编辑IP部分的.csv文件,为www.google.com提供外部IP,因此它确实做出了很好的响应。只有另一个域上的IP才响应。我已经要求管理员在ICMP,TCP / UDP端口7,22上打开防火墙的路径,再次测试我的程序(尚未实现),看看我是否有更多运气。

我的问题是使用域(172.)上的InetAddress.isReachable()到域(10.)库有什么问题,如果有更多要求来自管理团队,那么方法可以做到设计的目的。

我正在粘贴我的帖子的代码:

package my.pinger3;

import java.io.IOException;
import javax.swing.JTable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PingerThread3 implements Runnable {

//Declaring global variables
JTable jt;
int timeout = 400;  //Initialise values to something resonable
int sleepTime = 10000;  //Initialise values to something resonable
long startTime;
long endTime;
boolean boolVal = false;
int countAll;
int countFailed;
int row = 0;
final int COL_IP = 2;
final int COL_DEL = 3;
final int COL_SEN = 4;
final int COL_OK = 5;
final int COL_REA = 6;
//Define the openPort var that tells the isReachable() method what port   to use for socket connection
final int openPort = 7;   //Use the 7port as some say that is the open port for ICMP 

//Constructor
PingerThread3(JTable jt, int row, int timeout, int sleepTime) {
    super();
    this.jt = jt;
    this.row = row;
    this.timeout = timeout;
    this.sleepTime = sleepTime;

    //Initialise global variables
    startTime = 0;
    endTime = 0;
    countAll = 0;
    countFailed = 0;
}

@Override
public void run() {
    try {
        //InetAddress address = InetAddress.getByName((String) jt.getValueAt(row, COL_IP));
        //System.out.println((String)jt.getValueAt(row, COL));
        while (true) {
            //Catch the time before reachable() method is called
            startTime = System.currentTimeMillis();
            //Check if the address is reachable
            /*
            Use the InetAddress isReachable(timeout) method through isReachable(address)
            */
            //boolVal = isReachable(address);
            /*
            Use the socket isReachsble Version method
            */
            //boolVal = isReachable(jt.getValueAt(row, COL_IP).toString(), openPort, timeout);
            /*
            Use the hardcoded Widnows ping command method
            */
            boolVal = isReachable(jt.getValueAt(row, COL_IP).toString());
            //Catch the time after reachable() method has been called
            endTime = System.currentTimeMillis();

            //Display the Delay column data in the specific cell
            jt.setValueAt((Long) (endTime - startTime), row, COL_DEL);

            /*
            Display the number of times the reachable() was called which actually is
            not equal to the number of requests done on the IP (ICMP port 7) given.
             */
            jt.setValueAt((Integer) countAll, row, COL_SEN);

            if (boolVal) {
                jt.setValueAt("reached", row, COL_REA);
            } else {
                jt.setValueAt("NOT reached", row, COL_REA);

                //Increase the counter for failed operations
                countFailed++;
                //Set the color of this cell to red

            }

            //Display the %OK of packets send/received
            jt.setValueAt(calcOK(countAll, countFailed), row, COL_OK);

            Thread.sleep(sleepTime);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException ex) {
        Logger.getLogger(PingerThread3.class.getName()).log(Level.SEVERE, null, ex);
    }
}

/*
The InetAddress isReachable() method
*/
private boolean isReachable(InetAddress ia) {
    Boolean boolLocalVal = false;
    try {
        boolLocalVal = ia.isReachable(timeout);
        //increase the counter for all calls of this method
        countAll++;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return boolLocalVal;
}

/*
The Socket version of isReachable
 */
private boolean isReachable(String addr, int openPort, int timeOutMillis) throws IOException {
    // Any Open port on other machine
    // openPort =  22 - ssh, 80 or 443 - webserver, 25 - mailserver etc.
    Socket soc = new Socket();
    try {
        countAll++;
        soc.connect(new InetSocketAddress(addr, openPort), timeOutMillis);
        return true;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }finally {
        soc.close();
    }
}

/*
The hard coded Windows ping version of isReachable() note that this is the worst 
solution in resources and accuracy based observations
*/
private boolean isReachable(String add) throws IOException, InterruptedException {
    countAll++;
    // in case of Linux change the 'n' to 'c'
    Process p1 = java.lang.Runtime.getRuntime().exec("ping -n 1 "+add);
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal == 0);
    p1.destroy();
    return reachable;
}

private Double calcOK(int cntAll, int cntFailed) {
    Double value = ((Double.valueOf(cntAll) - Double.valueOf(cntFailed)) / Double.valueOf(cntAll)) * 100;
    return Math.round(value * 100.0) / 100.0;
}
}

0 个答案:

没有答案