Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to ping 200s of IPs in a loop in every 10 seconds. So, this loop gets executed in every 10 seconds. I was using this code to ping the IPs

Java
for (i = 0; i <= 200; i++ )
{
   ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows? "-n" : "-c", "1", *SOMEIP*);
   Process proc = processBuilder.start();

   int returnVal = proc.waitFor();
}


This is just the part of my code. I am creating separate thread for every ping because if 100 IPs are down, then it will take more than 300 seconds to ping each & every IP sequentially. So, created separate threads. But the problem was that whenever the loop gets executed, then the PCs CPU usage reaches to 90%, which is not good for a critical system. So I change the program to this one.

Java
for (i = 0; i <= 200; i++ )
{
   InetAddress inet = InetAddress.getByName(*SOMEIP*);
   System.out.println(inet.isReachable(3000) ? "Host is reachable" : "Host is NOT reachable");

}

In the above code also, I am creating separate thread for each Ping because of the same problem. Now, here I am getting another problem. This code is giving unexpected result. The IPs which are pingable are also showing not reachable sometimes using above code. Why this is happening ?

Is there any other solution to this problem ?

Thanks
Posted
Updated 26-Aug-15 22:11pm
v2

1 solution

See the comments at https://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#isReachable(int)[^]. What exactly are you trying to achieve?
 
Share this answer
 
Comments
Member 11632116 27-Aug-15 5:03am    
I clearly said everything in my question what I want to achieve!

I want to ping multiple IP address in multithreaded environment where isReachable is giving unexpected results.
Richard MacCutchan 27-Aug-15 5:11am    
Then you need to read the documentation again, which explain why that may happen.
Member 11632116 27-Aug-15 6:07am    
Is there any solution ?
Richard MacCutchan 27-Aug-15 7:31am    
Solution to what? Some IP addresses may respond, others may not. It depends on too many unknowns, and you have no control over what happens across a local or remote network.

And, as I asked before, what exactly are you trying to achieve, or what problem are you trying to solve?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900