I want to get the ping execution time and result in string after ping host

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 07:15:23

问题


I want to get the ping execution time and result in string after ping host. How can I do it?


回答1:


long currentTime = System.currentTimeMillis();
boolean isPinged = InetAddress.getByName(servername).isReachable(2000); // 2 seconds
currentTime = System.currentTimeMillis() - currentTime;
if(isPinged) {
    System.out.println("pinged successfully in "+ currentTime+ "millisecond");
} else {
    System.out.println("PIng failed.");
}

But this will use ICMP ping only in windows system.




回答2:


did you check this http://docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java

and

http://www.java2s.com/Code/JavaAPI/java.net/InetAddressisReachableinttimeout.htm




回答3:


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;


来源:https://stackoverflow.com/questions/8816870/i-want-to-get-the-ping-execution-time-and-result-in-string-after-ping-host

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!