Ping multiple IP adresses
-
Hi, What would be the fastest way to ping 255 IP addresses? Are there any example with using multithreading, as I am quite new to threading.
The multi-threading itself isn't problematic; getting it all done in a reasonable time may be a bit of a problem. I would use say four to eight identical instances of a class that starts a thread which, in a loop: - fetches an IP address out of a shared queue; - calls Network.Ping(); - stuffs the result in another shared queue. Of course, both queues need their lock. You should not pre-assign IP addresses to threads, as you can't predict how long a ping operation will take; typically those that succeed run fast, and the failing ones have to wait for their time-out. And adding more threads won't help much as I expect you will end up running in network limitations. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Wednesday, November 10, 2010 10:49 AM
-
The multi-threading itself isn't problematic; getting it all done in a reasonable time may be a bit of a problem. I would use say four to eight identical instances of a class that starts a thread which, in a loop: - fetches an IP address out of a shared queue; - calls Network.Ping(); - stuffs the result in another shared queue. Of course, both queues need their lock. You should not pre-assign IP addresses to threads, as you can't predict how long a ping operation will take; typically those that succeed run fast, and the failing ones have to wait for their time-out. And adding more threads won't help much as I expect you will end up running in network limitations. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
modified on Wednesday, November 10, 2010 10:49 AM
For my application I can set time-out to less then 250ms, so waiting is not a issue. When using lock, does this mean the first thread that access "variable" locks the access and if other threads are trying to access same "variable" wait (like thread.sleep) until it is released?
-
For my application I can set time-out to less then 250ms, so waiting is not a issue. When using lock, does this mean the first thread that access "variable" locks the access and if other threads are trying to access same "variable" wait (like thread.sleep) until it is released?
1. 250msec * 255 IP adr / 8 threads is still 8 seconds. 2. yes, said locks would be exclusive, but only while accessing the queue, not while pinging (hence only a small fraction of the time). Like so (in pseudo-code!):
lock(IPadrLock) {
IPadr=IPadrQueue.Dequeue();
}
result=ping(IPadr);
lock(resultLock) {
resultQueue.Enqueue(result);
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
1. 250msec * 255 IP adr / 8 threads is still 8 seconds. 2. yes, said locks would be exclusive, but only while accessing the queue, not while pinging (hence only a small fraction of the time). Like so (in pseudo-code!):
lock(IPadrLock) {
IPadr=IPadrQueue.Dequeue();
}
result=ping(IPadr);
lock(resultLock) {
resultQueue.Enqueue(result);
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Thank you for your answer. I will also look into "Ping.SendAsync" method, like this one http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/1627b5e9-e18c-441e-aebf-efb2a58d86a7[^]
It is polite to up vote a helpful answer.
Never underestimate the power of human stupidity RAH