Java 我想获得ping执行时间,并在ping主机之后生成字符串
我想得到ping执行时间和ping主机后的字符串结果。我怎么做呢?你检查过这个了吗 及Java 我想获得ping执行时间,并在ping主机之后生成字符串,java,ping,Java,Ping,我想得到ping执行时间和ping主机后的字符串结果。我怎么做呢?你检查过这个了吗 及 但这将仅在windows系统中使用ICMP ping。我就是这样使用它的- long start = System.currentTimeMillis(); long ping; String[] command = { "cmd.exe", "/C", "ping 192.168.1.101" }; commandProcess = Runtime.getRuntime().exec(comma
但这将仅在windows系统中使用ICMP ping。我就是这样使用它的-
long start = System.currentTimeMillis();
long ping;
String[] command = { "cmd.exe", "/C", "ping 192.168.1.101" };
commandProcess = Runtime.getRuntime().exec(command);
BufferedReader buffy = new BufferedReader(new InputStreamReader(commandProcess.getInputStream()));
String readline;
while((readline = buffy.readLine())!=null){
System.out.println(readline);
if(readline.contains("reply")){
long ping = System.currentTimeMillis();
System.out.println("Pinged in:"+ ping);
}
}
long end = System.currentTimeMillis();
String done = "Completed in times:" +start + ping +end;
private static void checkPing(String hostName) {
String[] command = { "cmd.exe", "/C", "ping " + hostName };
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
while ((readline = buff.readLine()) != null) {
if (readline.contains("Reply")) {
System.out.println("Pinged " + hostName + " in : "
+ readline.substring(readline.indexOf("time=") + 5, readline.indexOf("ms")) + " ms");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
提供每个ping请求的精确(可靠)ping延迟(以毫秒为单位)。
如果需要,您可以添加其中的四个
private static void checkPing(String hostName) {
String[] command = { "cmd.exe", "/C", "ping " + hostName };
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readline;
while ((readline = buff.readLine()) != null) {
if (readline.contains("Reply")) {
System.out.println("Pinged " + hostName + " in : "
+ readline.substring(readline.indexOf("time=") + 5, readline.indexOf("ms")) + " ms");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}