【问题标题】:How to ping another PC in the same local network using ACE?如何使用 ACE ping 同一本地网络中的另一台 PC?
【发布时间】:2017-02-16 08:42:51
【问题描述】:

我需要将文件复制到远程 PC。在此之前,我需要检查 PC 是否可以访问。我尝试使用ACE_OS::stat() 检查目录是否存在。在某些平台上,这有一个超时,这对我来说已经足够了,但在其他平台上,程序就会卡住。

ACE 提供了 ACE_Ping_Socket,所以我假设它可以满足我的需求。但是,我无法使其工作,即使使用 LOCALHOST 也是如此。有人知道怎么做吗?

#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;
}

我也对 ACE 的替代品持开放态度 :-)

【问题讨论】:

    标签: c++ ping ace


    【解决方案1】:

    以下效果很好。 pacHost 可以是 IP 地址(例如“127.0.0.1”)或主机名(例如 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;
    }
    

    在我的例子中,使用 SMB 协议的端口是 445。

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多