connect()中的超时无效

时间:2014-03-26 12:07:57

标签: c++ sockets winsock winsock2

我使用此代码连接到服务器,但它没有等待我设置为超时的10秒。它在连接失败后立即返回。

BOOL Connect(string server, int port, int timeout)
{
    struct sockaddr_in RemoteHost;
    TIMEVAL Timeout;
    Timeout.tv_sec = timeout;
    Timeout.tv_usec = 0;
    int con_error = 0;

#ifdef W32
    WSADATA       wsd;
    if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
    {
        DEBUG(L"Failed to load Winsock!\n");
        return FALSE;
    }
#endif

    //create socket if it is not already created
    if (s == SOCKET_ERROR)
    {
        //Create socket
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == SOCKET_ERROR)
        {
            DEBUG(L"Could not create socket");
            return FALSE;
        }
    }

    //setup address structure
    if (inet_addr(server.c_str()) == INADDR_NONE)
    {
        struct hostent *he;

        //resolve the hostname, its not an ip address
        if ((he = gethostbyname(server.c_str())) == NULL)
        {
            //gethostbyname failed
            DEBUG(L"gethostbyname() - Failed to resolve hostname\n");
            return FALSE;
        }
    }   
    else//plain ip address
    {
        RemoteHost.sin_addr.s_addr = inet_addr(server.c_str());
    }

    RemoteHost.sin_family = AF_INET;
    RemoteHost.sin_port = htons(port);

    //set the socket in non-blocking
    unsigned long iMode = 1;
    int iResult = ioctlsocket(s, FIONBIO, &iMode);
    if (iResult != NO_ERROR)
    {
        DEBUGP(L"ioctlsocket failed with error: %ld\n", iResult);
        return FALSE;
    }

    //Connect to remote server
    if ((con_error=connect(s, (struct sockaddr *)&RemoteHost, sizeof(RemoteHost))) < 0)
    {
        if (con_error != EINPROGRESS)
        {
            DEBUG(L"connect() failed");
            return FALSE;
        }       
    }

    // restart the socket mode
    iMode = 0;
    iResult = ioctlsocket(s, FIONBIO, &iMode);
    if (iResult != NO_ERROR)
    {
        DEBUGP(L"ioctlsocket failed with error: %ld\n", iResult);
        return FALSE;
    }

    fd_set Write, Err;
    FD_ZERO(&Write);
    FD_ZERO(&Err);
    FD_SET(s, &Write);
    FD_SET(s, &Err);

    // check if the socket is ready
    select(0, NULL, &Write, &Err, &Timeout);
    if (FD_ISSET(s, &Write))
    {
        return TRUE;
    }
    return FALSE;
}

2 个答案:

答案 0 :(得分:2)

当目标系统上的套接字不可用时,它可能会发回an ICMP message indicating that the socket is not open并尝试连接失败。在这种情况下,winsock函数将立即返回 - 它是设计的。

答案 1 :(得分:1)

使用WSAGetLastError找出呼叫失败的原因。 connect成功时返回0,失败时返回SOCKET_ERROR

您评论WSAGetLastError返回WSAEWOULDBLOCK,其中包含:

  

此错误从无法立即完成的非阻塞套接字上的操作返回,例如当没有数据排队等待从套接字读取时的recv。这是一个非致命错误,应该稍后重试该操作。将WSAEWOULDBLOCK报告为在非阻塞SOCK_STREAM套接字上调用connect的结果是正常的,因为必须经过一段时间才能建立连接。

您设置的非阻塞套接字上的预期行为也是如此。