Is there any way to decrease the timeout when I call connect() function from Winsock? I think it's almost 30 seconds, I want to put 5 seconds.
The easiest way is to use the socket in non-blocking mode while connecting, and use select() with a timeout of 5 seconds to check if the socket is writable. After select() exits, the connection is either established or not.  If not, consider the connection timed out and perform error handling as needed.
You can also call the ConnectEx() function and pass it the OVERLAPPED.hEvent structure with a pre-created event, for which you can wait with WaitForSingleObject() as short as you want.
ConnectEx() is available in WindowsXP and above...
//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 the ConnectEx() again on this socket.
            DoWarning("ConnectEx() failed with CONNECTION REFUSED: ", 0 )
        else if (iRes =  WSAENETUNREACH)    //After this error, it is OK to try the ConnectEx() again on this socket.
            DoWarning("ConnectEx() failed with NETWORK UNREACHABLE: ", 0 )
        else if (iRes =  WSAETIMEDOUT)  //After this error, it is OK to try the ConnectEx() again on this socket.
        {
            DoWarning("ConnectEx() TIMED OUT Immediately:", 0 );
            Result= ERROR_TIMEOUT;
        }
        else
            DoError("ConnectEx() failed with unexpected error: ", iRes )
    }
    WSACloseEvent(Overlapped.hEvent);
    
    return Result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With