使用getaddrinfo连接套接字超时

时间:2018-06-20 13:21:53

标签: c sockets getaddrinfo gethostbyname

我花了数小时试图找出我的问题,在写问题时才找到解决方案(当您需要正式化问题并加以解释时,它总是有帮助的)。我发布了它,希望对您有所帮助。

使用getaddrinfo,如果我尝试将套接字连接到服务器,则执行(我认为是)在大量网站以及getaddrinfo的手册页示例代码中正在解释的内容,则它会失败并显示“连接”超时”错误消息: (简化代码以使其更简洁)

void connect_UsingGetAddrInfo_Wrong (std::string host, unsigned short int port, int& socketfd)
{
    //simplified loops & error handling for concision
    int x;

    int domain = AF_INET;         // IP_v4
    int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.  

    addrinfo hints, *addr;
    //fine-tune hints according to which socket you want to open
    hints.ai_family = domain; 
    hints.ai_socktype = socketType; 
    hints.ai_protocol = 0;           // no enum : possible value can be read in /etc/protocols
    hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;

    x =  getaddrinfo(hostname, NULL, &hints, &addr);
    //shall rather loop on addr linked list, but this is not the topic here.

    socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    x = connect(socketfd, addr->ai_addr, addr->ai_addrlen);
}

但是,我能够使用gethostbyname方法将套接字连接到同一服务器。

void connect_UsingGetHostByName_Deprecated (std::string host, unsigned short int port, int& socketfd)
{
    //simplified loops & error handling for concision
    int x;

    int domain = AF_INET;         // IP_v4
    int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.  

    struct hostent DNS, *r;
    char buf[1024];
    x = gethostbyname_r(hostname.c_str(), & DNS, buf, sizeof(buf), & r, & err));
    socketfd = socket(domain, socketType, 0);

    //server.
    sockaddr_in server;
    memset(&server, 0x00, sizeof(server));
    server.sin_family=domain;
    server.sin_port=htons(port);
    memcpy(& server.sin_addr.s_addr, DNS.h_addr, (size_t) DNS.h_length);
    x = connect(socketfd, (struct sockaddr *) & server, sizeof(server));
}

运行代码显示两个版本均正确检索服务器的有效IP地址。第一个仍然无法连接,并且会超时。 为什么呢

1 个答案:

答案 0 :(得分:2)

它一直失败的原因是:要检索addrinfo,我已将“服务”字段留为NULL。它仍然会返回成功并为您提供一个地址(您可以将其与getnameinfo映射到正确的IP地址)。该地址仍然无法用于连接您的套接字!

我在这里找到了两种方法的混合版本: https://codereview.stackexchange.com/questions/17863/socket-connect-realization-gethostbyname-or-getnameinfo 这是可以使用的功能,但我不购买铸件

void connect_UsingGetAddrInfo_HYBRID (std::string host, unsigned short int port, int& socketfd)
{
    //simplified loops & error handling for concision
    int x;

    int domain = AF_INET;         // IP_v4
    int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams. 

    addrinfo hints, *addr;
    //fine-tune hints according to which socket you want to open
    hints.ai_family = domain; 
    hints.ai_socktype = socketType; 
    hints.ai_protocol = 0;           // no enum : possible value can be read in /etc/protocols
    hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;

    x =  getaddrinfo(host, NULL, &hints, &addr);
    socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);

    //here is the hybrid part
    sockaddr_in servAddr;
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = addr->ai_family;
    servAddr.sin_addr.s_addr = *((uint32_t*) & (((sockaddr_in*)addr->ai_addr)->sin_addr));
    servAddr.sin_port        = htons(port);

    x=connect(socketfd, (struct sockaddr*) &servAddr, sizeof(servAddr));
}

最后,它帮助我找到了根本原因:

void connect_UsingGetAddrInfo_FIXED (std::string host, unsigned short int port, int& socketfd)
{
    //simplified loops & error handling for concision
    int x;

    int domain = AF_INET;         // IP_v4
    int socketType = SOCK_STREAM; // Sequenced, reliable, connection-based byte streams.  

    addrinfo hints, *addr;
    //fine-tune hints according to which socket you want to open
    hints.ai_family = domain; 
    hints.ai_socktype = socketType; 
    hints.ai_protocol = 0;           // no enum : possible value can be read in /etc/protocols
    hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;

    //Precise here the port !
    const char* service = std::to_string(port).c_str();

    x =  getaddrinfo(host, service, &hints, &addr);
    socketfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    x = connect(socketfd, addr->ai_addr, addr->ai_addrlen);
}

希望有一天有帮助!