Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

I have a question about Pinging many devices and displaying their status on a webform.

I am dynamically creating a table. A row is created for every computer in a database, and one of the cells is the IP address of the device.

The last cell of the table shows whether the device is online or offline. This is determined by the ping method. Because the database has over 100 devices, pinging one at a time is not very efficient.

I am looking for suggestions to perform pings for multiple devices at the same time. I know that System.Net.Ping has the SendAsync method, however, I am not sure if this is the solution. Can someone who has done this let me know?

Thank you.
Posted
Comments
Mohammad Al Hoss 28-Apr-11 9:24am    
Are the computers on the same network, is the computer that will host the application also on the same network.

I cannot see why not. One of the System.Net.NetworkInformation.SendAsync methods will really allow you to save time if you want to ping several IPs at the same time. The only alternative I see would be creating the same number of threads (preferably using the thread pool or BackgroundWorker) and pinging synchronously each separate IP in each separate thread.

[EDIT]

Let's get back to SendAsync. How to wait for all of the ping requests? Look at this example:
http://msdn.microsoft.com/en-us/library/ms144961.aspx[^].

It shows how to wait for AutoResetEvent (just one), which is a class derived from System.Threading.WaitHandle. How to wait for them all? Use the same instance of AutoResetEvent for all pings and use System.Threading.WaitHandle.WaitAll for wait. Try it.

—SA
 
Share this answer
 
v5
Comments
SKOTAJI 28-Apr-11 12:47pm    
I also agree. Use Bagroung worker thread by calling async your problems will solve.
Sergey Alexandrovich Kryukov 28-Apr-11 14:19pm    
Thank you. Still, I think in is this particular case of the async operation will be easier and solve the problem.
--SA
blgrnboy 28-Apr-11 22:11pm    
So I implemented it, and it works like a charm. The only think I cannot figure out is how to Wait for all Async Methods to be complete before I post the results - aka. SendPings(); //Logic needed here to see if all Async Pings have returned. txt.Result += "All results have returned";
Sergey Alexandrovich Kryukov 29-Apr-11 2:19am    
Ha! If this mechanism did not exist SendAsync would not make sense. Please see my updated answer (after [EDIT]).
--SA
blgrnboy 29-Apr-11 4:22am    
I actually now have it figured out. You execute all final logic in the Page_PreRenderComplete method. This is because Async Methods are kicked off right at Page_PreRender.
If all the devices are on the same subnet, you can do as follows:
1. Delete the arp cache. ("arp -d" on the command line.)

2. Ping the broadcast address for the subnet with several pings (~10). Make sure to use the actual broadcast address, i.e. 192.168.0.255 not 255.255.255.255.

3. Look up the IP addresses in the ARP table for your computer, either by using "arp -a" on the command line or GetIpNetTable using p/Invoke. If the address is in the arp table, the computer responded to your ping.

This link gives and example using GetIpNetTable in C#:
http://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-net[^]
 
Share this answer
 
Comments
Nish Nishant 28-Apr-11 10:55am    
Voted 5, proposed as answer.
Rick Shaub 28-Apr-11 10:59am    
Thanks!
Sergey Alexandrovich Kryukov 28-Apr-11 14:18pm    
Good points. My 5, too.
--SA

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