c++csocketsaceselect-function

Is select function internaly call tcp connect?


Code Sinnpet:

int CreateaTCPSocket()
{
    int iSockID =  ACE_OS::socket(......);
    ACE_OS::set_flags(iSockID,O_NONBLOCK);
    ACE_OS::bind();
    if (ACE_OS::connect(iSockID ,....) < 0)
    {
        if (ACE_OS::select(.....,timeout) <= 0)
        {
            return INVALID_HANDLE;
        }
    }
    return iSockID;
}

My question is when connect is failed for non-block error and select is called and say select return success then again we need to call connect or select function internal do connect?


Solution

  • For both blocking and non-blocking sockets you only need to call connect() once.

    When the socket is non-blocking and connect() returns EINPROGRESS it needs to wait till the socket becomes ready for write using select(). When select() reports that socket is ready for write it can be either that connect() succeeded or failed.

    To check whether a non-blocking connect() succeeded you can call getsockopt(..., SOL_SOCKET, SO_ERROR, ...) that reports non-zero error on failure, or call getpeername() which only succeeds on a connected socket.