Is select function internaly call tcp connect?

589 Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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.

1
On

why do call select once connect failed? What do you want to achieve with that? select notifies the caller of any activity on the resources given as argument. These activities could be

  • an error (example: tcp socket: peer disconnected)
  • data available (a blocking read can be performed without actually blocking)
  • write possible (after last write failed with buffer full)

I guess in the scenario you described, select would always return EBADF (in some OSes i know) since the descriptor is not yet oopened or has already been closed.

hth

Mario