A small bug in Socket::connect


#1

Hi all,

The ::connect function in Win32 and Linux (most probably Mac too) is blocking by default. This means that:

 while (timeOutMillisecs > 0 || timeOutMillisecs < 0)
    {
        // This is useless too, as it's not modified in the loop and checked before the loop
        if (handle < 0)
            return false;

        if (::connect (handle, (struct sockaddr*) &servTmpAddr, sizeof (struct sockaddr_in)) >= 0)
        {
            connected = true;
            break;
        }

        if (timeOutMillisecs > 0)
        {
            const int timeToSleep = jmin (timeOutMillisecs, 1000);
            timeOutMillisecs -= timeToSleep;
            Thread::sleep (timeToSleep);
        }
    }

will never time out, unless there is an error, which means looping forever.
In both linux and Win32 you have to put the socket in non blocking mode before calling connect, and then select the socket until it’s writable (meaning it’s connected), or it timed out.

Please have a look to my other thread, as I’m adding & correcting the functionnalities, and will post there


#2