2012-01-11 63 views

回答

5
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."); 
} 

但是,這將僅適用於Windows系統中使用ICMP的ping。

0
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; 
0

TENGO埃斯特codigo對hacer平聯合國EQUIPO偏遠落後地區,佩羅老闕quiero obtener兒子洛毫秒(milisegundos)。 String ip =「127.0.0.1」; String pingResult =「」;

String pingCmd = "ping " + ip; 
    try { 
     Runtime r = Runtime.getRuntime(); 
     Process p = r.exec(pingCmd); 
     BufferedReader in = new BufferedReader(new 
     InputStreamReader(p.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) { 
      System.out.println(inputLine); 
      pingResult += inputLine; 


     } 
     in.close(); 

    } catch (IOException e) { 
     System.out.println(e); 
    } 
} 

public static void main(String[] args) { 

    String ip = "google.com"; 
    runSystemCommand("ping " + ip); 
+0

請用英文發表您的答案。 – 2018-02-26 03:30:42