ping application using runtime.exce(pingComand)
Hi,
I am writing an application that is going to automate the ping command. I am not using the isReachable() because the server that i will be testing my machines have the firewall - and i have tested the code works and does what it is supposed to do.
I am using the:
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
where i am pinging, and getting the result from the dos:
InputStreamReader(p.getInputStream()));
and then changing the gui appropriattly (red == dead, green ==alive). The code is working, but the gui does not get updated until the last process (ping) has been executed. As i am pinging about 20 machines, it is taking a long time for the gui to be updated.
I want to update the gui after (or maybe during the ping as well ==yellow) each i get the result of the r.exec(pingCmd);, is there a way to make any threads sleep or, i am stuck.
Regards,
Amir
Everything is a Small Matter Of Programming (SMOP(tm)).
You can start 20 (or however many) threads, each of which execs ping() to a different machine, and updates a specific element of an array with the result status.
Your main thread can loop around examining the array to update your blinking lights (hopefully with a little sleep in there..). Also left as a task for the reader reader is killing off the threads that don't finish updating their status in a predetermined amount of time..
This is the method that i am using to do a ping, and then read the text returned (as would be seen on does when you do a 'ping ipAddress').
public String doPingCMD(String sIP) {
String ip = sIP;
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) {
pingResult += inputLine;
}
in.close();
}
catch (IOException e) {
System.out.println(e);
}
return pingResult;
}
This is doing a ping and returning the accurate text but it only does this one at a time. So imagin when i am having to do the same thing (one after another for 20 maichines (20 ip addresses) - it is taking very long - and the gui does not get updated untile the very last ping executes.
But what thread is doing the call to doPingCMD?
I suspect this is being done in an event handler, such that when someone types in a text fiedl and clicks the "ping" button then it issues the doPingCMD call. In that case the thread is the event dispatch thread and it will be blocked doing your ping work and so will be unable to update the GUI.