I'm currently working on an app that requires the need to ping network devices to check that they are online. The app could potentially have to check several hundred devices, and so I've made it a multi threaded application.
The problem I am getting is this:
I am pinging 2 IP Addresses,
I start Thread-2 first. The ping requests from this thread timeout(or host unreachable) as expected. While Thread 2 is running, I start Thread 1 after a small delay.
On Thread 1, I get a response from IP address 1 (as I should), but I also get a response from IP address 2 on Thread 2 even though there is no device on this IP address! The response from IP Address 2 is received whenever a response from IP Address is received. When Thread 1 sleeps without pinging IP Address 1, the Thread 2 produces timeout results as expected. No resource is shared between Thread 1 and Thread 2 and every ping is done through an individual dedicated ICMP Client object .
I believe that the second thread is seeing the response from IP address 1 and not working out that it isn't a response from its own ping request but I can't figure out why it happens.
The ping code is same as the sample ping.cpp provided in poco-1.9.0/Net/samples/Ping/src/Ping.cpp.
Anyone have any ideas or suggestions?
Thanks
There is nothing in the ICMP protocol which allows the networking stack to determine which ICMP packet to route to which receiving socket. You need to add an identifier to each packet you send and do some book keeping to make sure your code routes it to the right thread.
You have multiple threads that can send packets, but you probably want just one thread receiving packets and dispatching to the correct thread or dispatch to all threads and let each thread decide whether it's relevant based on an identifier it added when it sent the packet.