Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
while(true){
          If(!PCisConnected())
                //Do Something and close app;
          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.
Posted
Updated 28-Sep-11 14:03pm
v2

1 solution

I would rather ping the PC periodically, in a separate thread. You will need to use implementation of ICMP protocol, see http://en.wikipedia.org/wiki/Ping[^].

—SA
 
Share this answer
 
v2
Comments
André Kraak 29-Sep-11 0:40am    
Good answer, it was my first though when reading the title. +5.
Sergey Alexandrovich Kryukov 29-Sep-11 1:13am    
Thank you, André.
--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