问题描述
我想测试使用Java是否可以访问远程系统,或者换句话说使用Java发送ping".该功能应封装在具有布尔值的方法中,例如
I want to test whether a remote system is reachable using Java or in other words "send a ping" using Java. This functionality should be encapsulated in a method with boolean value, for example
public boolean isReachable(String ip) {
// what to do :-)
}
我已经测试了Java Process类,但是由于使用OutputBuffers进行复杂的输出处理,因此我认为这不是最好的方法.
I've tested the Java Process class, but I don't think that it is the best way to do this, because of the complex output handling with OutputBuffers.
Process proc = Runtime.getRuntime().exec("ping " + ip);
另一种可能性是创建一个Socket Connection并处理抛出的异常,但是如果远程系统是一个裸"的Unix系统,则另一端可能没有Socket :-)另外,我希望能够在无法访问远程系统时设置超时.
Another possibility would be creating a Socket Connection and handle thrown exceptions, but if the remote system is a "naked" unix system, there might be no Socket on the other side :-) Additionally, I'd like to be able to set a timeout, when a remote system is not reachable.
那我该怎么办呢?谢谢!
So how could I do this? Thank you!
推荐答案
InetAddress.getByName(ip).isReachable(timeout);
这篇关于如何测试远程系统是否可以访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!