版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bobozai86/article/details/82753651
在项目中需要判断目录服务器是否在线,需要用到ping命令,调研有两种方法:
- 使用Java API的InetAddress方式
- 使用Runtime.exec调用操作系统的命令CMD
使用InetAddress实现Ping
自Java 1.5开始,java.net包中就实现了ping的功能。详见InetAddress.isReachable()方法。
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ; //超时应该在3钞以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
// 当返回值是true时,说明host是可用的,false则不可。
return status;
}
isReachable方法在Windows系统平台上的实现(native c)并没有使用ICMP,而是全完使用连接echo端口7 的方法。Native的实现源码:
/*
* Windows implementation of ICMP & RAW sockets is too unreliable for now.
* Therefore it's best not to try it at all and rely only on TCP
* We may revisit and enable this code in the future.
*/
/* Can't create a raw socket, so let's try a TCP socket */
him.sin_port = htons(7); /* Echo */
connect_rv = connect(fd, (struct sockaddr *)&him, len);
InetAddress.isReachable()通过试图连接TCP端口的方法是利用了TCP/IP协议的三次握手原理,即使对方机器在端口上没有服务,当接收到请求时会立刻拒绝,如果对方机器不在网络上则结果是超时!这个方法的实现正是利用了这一点。引用OpenJDK 6,isReachable()方法native c实现的一段注释:
/**
* connection established or refused immediately, either way it means
* we were able to reach the host!
*/
还有一个问题就是超时时间的设置,受网络影响,TCP建立连接的3次握手耗时不确定,例如:
3次握手耗时700ms,如果我们设置的超时时间比700ms小,返回的也是false,从而造成误报。
调用CMD
通过程序调用类似“ping 127.0.0.1 -n 10 -w 3000”的命令,该命令ping10次,等待每个响应的超时时间3秒。 网络通的情况会输出:
C:\Users\tgg>ping 127.0.0.1 -n 10 -w 3000 正在 Ping 127.0.0.1 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64 来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=64
127.0.0.1 的 Ping 统计信息: 数据包: 已发送 = 10,已接收 = 10,丢失 = 0 (0% 丢失), 往返行程的估
计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms
以上信息输出是根据操作系统的语言来进行本地化的,其中"ms TTL="
是不变的,我们可以通过Runtime.exec方法来调用本地CMD命令来执行以上语句,代码如下:
import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** * @author tgg */
public class Ping {
public static boolean ping(String ipAddress) throws Exception {
int timeOut = 3000 ;
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
return status;
}
public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime();
// 将要执行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
// Linux命令如下
// String pingCommand = "ping" -c " + pingTimes + " -w " + timeOut + ipAddress;
try {
if (logger.isDebugEnabled()) {
logger.debug(pingCommand);
}
// 执行命令并获取输出
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int connectedCount = 0;
String line;
// 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
}
// 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
return connectedCount == pingTimes;
} catch (Exception e) {
logger.error(e);
return false;
} finally {
try {
in.close();
} catch (IOException e) {
logger.error(e);
}
}
}
//若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+line);
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
return 1;
}
return 0;
}
private static final Logger logger = Logger.getLogger(Ping.class);
}