These are chat archives for IndySockets/Indy

5th
Oct 2017
Elazar DornellesCeza
@razalE_twitter
Oct 05 2017 12:19
Hi there
Maybe someone here knows about idIcmpClient bug when using it with threads?
?
Elazar DornellesCeza
@razalE_twitter
Oct 05 2017 12:33
Hi, yep, I was just by now reading something talking about it works that way because it is based on raw protocol
mezen
@mezen
Oct 05 2017 12:47
The other answer (https://stackoverflow.com/q/12858551/4121488) shows a possible solution, but that is not implemented by indy itself
Ups, wrong copy&paste link, here is the correct: https://stackoverflow.com/a/12870358/4121488
Remy Lebeau
@rlebeau
Oct 05 2017 16:14
@razalE_twitter @mezen Yes, TIdIcmpClient uses a RAW socket, but that is not the reason why it can't be used in multiple threads. The same problem would occur in TIdUDPClient, for instance. It has to do with the nature of using connectionless sockets, like my answer explained in detail. Threads reading socket packets that belong to other threads. The other solution is simply running TIdIcmpClient in a worker thread and wrapping Ping() inside a thread lock (specifically, a TMonitor, but a TCriticalSection or TMutex would also work) so multiple threads don't overlap their calls to Ping().
Kudzu
@czhower
Oct 05 2017 17:15
UDP however has ports which allow different traffic to be routed properly even in threads.
Remy Lebeau
@rlebeau
Oct 05 2017 17:27
@czhower Fine, I shouldn't have mentioned UDP. Let me re-phrase: @razalE_twitter @mezen Yes, TIdIcmpClient uses a RAW socket, which is not bound to any particular port since ICMP doesn't use ports. As my answer explained in detail, multiple RAW sockets reading ping replies at the same time will end up reading each other's packets, that is why TIdIcmpClient.Ping() (or Microsoft's ping.exe, for that matter) doesn't work in a parallel-ping scenario. The other solution is simply running TIdIcmpClient in a worker thread and wrapping Ping() inside a thread lock (specifically, a TMonitor, but a TCriticalSection or TMutex would also work) so multiple threads don't overlap their calls to Ping(), thus they don't read each other's packets.