来自Winsock的connect()函数超时

问题描述 投票:2回答:2

当我从Winsock调用connect()函数时,有没有办法减少超时?我想差不多30秒,我想放5秒钟。

c++ visual-studio sockets winapi winsock
2个回答
6
投票

最简单的方法是在连接时以非阻塞模式使用套接字,并使用超时为5秒的select()来检查套接字是否可写。在select()退出后,连接建立与否。如果不是,请考虑连接超时并根据需要执行错误处理。


1
投票

您还可以调用ConnectEx()函数并将OVERLAPPED.hEvent结构传递给预先创建的事件,您可以根据需要等待WaitForSingleObject()。

ConnectIon()仅在Windows XP及更高版本中可用...

//The HANDLE Socket MUST BE pre-bound with Bind() before calling this function
int ConnectWithTimout(HANDLE Socket, UINT remIP, WORD remPort, UINT milliseconds)
{
    int iRes, Result;
    UINT OptVal, Flags;
    OVERLAPPED Overlapped;
    sockaddr_in socket_info;


    Result= ERROR_UNEXP_NET_ERR;

    ZeroMemory(&socket_info, sizeof(socket_info));
    ZeroMemory(&Overlapped, sizeof(Overlapped));

    socket_info.sin_addr.S_addr = htonl(remIP);
    socket_info.sin_port = htons(remPort);
    socket_info.sin_family = AF_INET;

    Overlapped.hEvent = WSACreateEvent(); 
    if ( ConnectEx(Socket, &socket_info, sizeof(socket_info), NULL, 0, NULL, &Overlapped) )
        printf("WOW! Connection succeeded immediately\n");
    else
    {
        iRes = WSAGetLastError();
        if (iRes == ERROR_IO_PENDING)
        {
            iRes = WaitForSingleObject(Overlapped.hEvent, milliseconds);  //Wait for x milliseconds to connect
            if (iRes == WAIT_OBJECT_0)
            {
                if (!WSAGetOverlappedResult(socket, &Overlapped, &OptVal, FALSE, Flags))
                {
                    iRes = WSAGetLastError();
                    if (iRes == WSAEADDRINUSE)
                        DoError("WSAGetOverlappedResult() reported that the requested local address is already in use or in a TIME_WAIT state")
                    else
                        DoError("WSAGetOverlappedResult() failed with error: ", iRes);
                }
                else
                {
                    OptVal = 1;
                    iRes = setsockopt(Socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, PCHAR(&OptVal), sizeof(OptVal));
                    if (iRes == SOCKET_ERROR)
                        DoError("setsockopt(SO_UPDATE_CONNECT_CONTEXT) failed with error: ", WSAGetLastError() );

                    printf("Connected to %s : %s\n", inet_ntoa(socket_info.sin_addr), itoa(ntohs(socket_info.sin_port)));
                    Result=NO_ERROR;
                }
            }
            else
            {
                if (iRes == WAIT_TIMEOUT)
                {
                    DoWarning("ConnectEx() TIMED OUT - ", iRes);
                    Result= ERROR_TIMEOUT;
                }
                else
                    DoError("ConnectEx() failed with error: ", iRes)
            }
        }
        else if (iRes ==  WSAECONNREFUSED)  //After this error, it is OK to try to connect again on this docket.
            DoWarning("ConnectEx() failed with CONNECTION REFUSED: ", 0 )
        else if (iRes =  WSAENETUNREACH)    //After this error, it is OK to try to connect again on this docket.
            DoWarning("ConnectEx() failed with NETWORK UNREACHABLE: ", 0 )
        else if (iRes =  WSAETIMEDOUT)  //After this error, it is OK to try to connect again on this docket.
        {
            DoWarning("ConnectEx() TIMED OUT Immediately:", 0 );
            Result= ERROR_TIMEOUT;
        }
        else
            DoError("ConnectEx() failed with unexpected error: ", iRes )
    }


    WSACloseEvent(Overlapped.hEvent);

    return Result;
}
© www.soinside.com 2019 - 2024. All rights reserved.