ZH.sd
ZH.sd

Reputation: 123

Get system error 115(operation in progress) on blocked socket connect()

In general, system error 115 occurs on a non blocking socket connect(), and the connection needs to be further checked through the select() interface.

But I didn't use fcntl() to set socket to O_NONBLOCK, just set a send timeout as shown below: setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, &len);

So, In my scenario, is the cause of the 115 error in connect() the same as that of a non block socket? And how do I know how long the connect interface is blocked when I get a 115 error?

Code example:

int timeout = 0;
socklen_t len = sizeof(timeout);
ret = setsockopt(real_fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
if (-1 == ret)
{
    LOG(" error setsockopt");
    return SOCKET_FAIL;
}

ret = connect(fd, (struct sockaddr *) &dest, sizeof(dest));
if (0 != ret)
{
    LOG("error connect = %s", strerror(errno));
    return ret;
}

Result: Sometimes get system error "Operation now in progress". The error number is 115, which is also defined as EINPROGRESS.

Upvotes: 1

Views: 4103

Answers (1)

ZH.sd
ZH.sd

Reputation: 123

Thank you for your ideas. I found the answer in the man socket(7). EINPROGRESS errors can also occur in blocked sockets.

   SO_RCVTIMEO and SO_SNDTIMEO
          Specify  the  receiving  or  sending timeouts until reporting an
          error.  The argument is a struct timeval.  If an input or output
          function  blocks for this period of time, and data has been sent
          or received, the return value  of  that  function  will  be  the
          amount  of data transferred; if no data has been transferred and
          the timeout has been reached, then -1 is returned with errno set
          to  EAGAIN  or EWOULDBLOCK, or EINPROGRESS (for connect(2)) just
          as if the socket was specified to be nonblocking.  If the  time‐
          out  is set to zero (the default), then the operation will never
          timeout.  Timeouts only have effect for system calls  that  per‐
          form   socket   I/O   (e.g.,   read(2),   recvmsg(2),   send(2),
          sendmsg(2)); timeouts have no  effect  for  select(2),  poll(2),
          epoll_wait(2), and so on.

Upvotes: 1

Related Questions