您的位置:首页 > 编程语言 > Java开发

Java 调用window下的ping命令实现ping的函数封装

2011-11-24 16:48 197 查看
/**
* 能否ping通IP地址
* @param server IP地址
* @param timeout 超时时长
* @return true能ping通
*/
public static boolean pingServer(String server, int timeout) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime();

String pingCommand = "ping " + server + " -n 1 -w " + timeout;
try {
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
if (line.startsWith("Reply from")||line.contains("TTL")) {
return true;
}
}

} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}

实现java ping命令 当然网上有许多方法,但是我觉着这种简单

还有一种是用黑客的洪水攻击的方法

伪造一个ip数据包进行测试

调用的jpcap外部库

正在研究。、。、、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: