Hello there,
i'm coding a C++ console application that, very often, should detect if a PC on the same lan is still connected. I have this kind of situation:
HOME LAN (3pc, 1 router)
CLIENT ------- ROUTER ------- PC
My application, runs on the client and it should detect if the PC becomes unreachable (for example is shut down, or the cable is disconnected).
I have some code that does:
while(true){
If(!PCisConnected())
Sleep(1000);
}
At this point i need to code the
PcISConnected()
function. What's the best and the most affordable way to detect if it disconnects?
To be more precise, the PC is not a real PC but rather a LAN UNIT that can reply to PING (ICMP ECHO) and that has a Web Server + Ftp Server. So basically they way i can detect a disconnection is trough ICMP or TCP 80/21.
My 1st attempt weren't so good in this LAN environment while they're very great in an WAN environment.
I created an infinite loop in which i create a TCP Socket to the port 80 and call the connect() function.
It worked great BUT: if the PC is disconnected, it tooks an average 20-30seconds before throwing an error (while it worked very well if the PC was a webserver in the world, somewhere).
My 2nd attempt has a similiar issue:
Instead of putting the connect() in an infinite loop, i put the send().
So i've tryied to send a message to the PC.
The issue is that, even if i disconnect the PC from the LAN, the send iterate 10-20 times before throwing an error. So for around 20times, it says that the message was succesfully sent even if the PC is disconnected (as i said before, the same test with a webserver around the worl, worked great and the send iterated only 2times before throwing an error).
And the 3rd attempt is ICMP but i didn't found any suitable solution for my project (which is being coded trough Eclipse). But i found ICMP not so affordable since sometimes, a ping fails even if the host is up and that failure will lead application to malfunction cause the host is still up.
How can i solve this issue? What's the best approach i can take here?
It should be affordable and fast as i should detect a disconnection in 2-3 seconds, no more.
I know for that, i need to continuosly send message to the PC, but that's not an issue.
Thanks you all.