I need to copy files to a remote PC. Before I do that, I need to check whether the PC is reachable. I tried using ACE_OS::stat()
to check whether a directory exists. On some platforms, this has a timeout, which would be sufficient for me, but on other platforms, the program gets stuck.
ACE offers the ACE_Ping_Socket, so I assume that it does what I want. However, I cannot make it work, not even with LOCALHOST. Does anybody know how to do this?
#include "ace/Ping_Socket.h"
#include "ace/INET_Addr.h"
int main(int argc, char * argv[])
{
ACE_INET_Addr addr;
// instead of 127.0.0.1, insert IP of a remote PC
int i_set = addr.set("127.0.0.1:0");
ACE_Ping_Socket s;
int i_open = s.open(addr);
// if open works, PC is reachable, at least this is the idea...
// i_set = 0, but i_open = -1, even for 127.0.0.1
return 0;
}
I am also open for alternatives to ACE :-)
The following works nicely. pacHost can be an IP address (e.g. "127.0.0.1") or a hostname (e.g. ACE_LOCALHOST).
static bool isReachable(unsigned int uiPort, const char * pacHost, unsigned int uiTimeOutSeconds)
{
ACE_INET_Addr srvr(uiPort, pacHost);
ACE_SOCK_Connector connector;
ACE_SOCK_Stream peer;
const ACE_Time_Value timeout (uiTimeOutSeconds);
if (-1 == connector.connect(peer, srvr, &timeout))
{
return false;
}
peer.close();
return true;
}
In my case, the port is 445 for using the SMB protocol.