2
votes

I am using the following code to call android equipment ping command:

public static String pingServer() {
PingResult result = new PingResult();
String jsonString = null;
try {
String command = "ping -c 3 192.168.8.185"; 
Process p = Runtime.getRuntime().exec(command);
int status = p.waitFor();
InputStream input = p.getInputStream(); 
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}


String bufferStr = buffer.toString();
        System.out.println(bufferStr);

} catch (Exception e) {
System.out.println("---------------exception-----------ping");
System.out.println(e.getMessage());
// e.printStackTrace();

}
}

BufferStr always obtain the last line data.

3 packets transmitted, 3 received, 0% packet loss, time 2000ms

The details result can be get. enter image description here

1
that's real nice and all, but, what was your question again ? - njzk2

1 Answers

1
votes

It may be simpler and easier to use InetAddress.isReachable:

if (InetAddress.getByName("192.168.8.185").isReachable(6000))
    ...