本文介绍了在java中Ping值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个问题已经以不同的方式处理,但我已经检查了stackoverflow,但我找不到我想要的答案。
I know that this question has been approached under different ways, but I have checked stackoverflow and I didn't found the answer I was looking for.
要制作很简单:有没有办法让时间ping值到 Windows 下的IP服务器?
To make it simple : Is there a way to get the Time ping value to an IP server under Windows ?
I知道如何检查某些服务器是否可以访问,但我希望有精确的值,就像我们可以在终端上阅读一样。
I know how to check if some servers are reachable, but I would like to have precise values, like we can read on terminal.
谢谢为了你的帮助和理解。
Thank you for your help and understanding.
推荐答案
你可以这样做:
//The command to execute
String pingCmd = "ping " + ip + " -t";
//get the runtime to execute the command
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(pingCmd);
//Gets the inputstream to read the output of the command
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
//reads the outputs
String inputLine = in.readLine();
while ((inputLine != null)) {
if (inputLine.length() > 0) {
........
}
inputLine = in.readLine();
}
更新:根据您的需要
public class PingDemo {
public static void main(String[] args) {
String ip = "localhost";
String time = "";
//The command to execute
String pingCmd = "ping " + ip;
//get the runtime to execute the command
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(pingCmd);
//Gets the inputstream to read the output of the command
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
//reads the outputs
String inputLine = in.readLine();
while ((inputLine != null)) {
if (inputLine.length() > 0 && inputLine.contains("time")) {
time = inputLine.substring(inputLine.indexOf("time"));
break;
}
inputLine = in.readLine();
}
System.out.println("time --> " + time);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
写得很匆忙。
这篇关于在java中Ping值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!