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

[956 byte] By [Amir@Citya] at [2007-10-3 3:10:22]
# 1

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..

shankar.unnia at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 2
This probably won't work as ping is not threadsafe or parallelism-safe right down to the IP level so the parallel pings will only confuse each other.More to the point, why all these pings? Are you sure you need them? Wouldn't a timed connect do just as well?
ejpa at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 3
I need to keep a record of the machines being alive/dead to the network. I do not know what do yuo mean by time cconnect.I am still stuck, i am trying the thread suggestion given above but i am not managing to get a correct solution.
Amir@City@a at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 4
What thread is doing the pinging? I suspect it is the event dispatch thread, in which case requesting an update to the GUI will have no effect until after the ping loop has completed. We need to see more context for this code to help out.
davidholmesa at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 5

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.

Amir@City@a at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 6

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.

davidholmesa at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 7
Yes that's exacly what it is doing, when i click the button this method is called. And the gui never gets updated - untill the very last ping is executed. Is there a way to solve this problem, and how. Regards,Amir
Amir@City@a at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 8
This may come as a shock, but you could always ...RUN IT IN ANOTHER THREAD!
ejpa at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...
# 9
As EJB says you need to run the ping command in another thread and have it submit GUI updates to the event thread using invokeLater().I strongly recommend you take some time to read/learn about the threading model in Swing, use of SwingWorker etc before continuing.
davidholmesa at 2007-7-14 21:01:08 > top of Java-index,Core,Core APIs...