Does Connect () return a "Running Operation" when a socket is blocked?

I have a blocking socket (at least it appears in the following code):

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { ERROR("%s: error opening socket", __func__); return (RESP_ERROR); } t.tv_sec = timeout; t.tv_usec = 0; int rf = fcntl(sock, F_GETFD); ERROR("fcntl ret=%d, ret & O_NONBLOCK = %d", rf, rf & O_NONBLOCK); if ((setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&t, sizeof (t)) < 0) || (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&t, sizeof (t)))) { strerror_r(errno, err, 254); ERROR("%s: error on setsockopt -> %s", __func__, err); close(sock); return (RESP_ERROR); } rf = fcntl(sock, F_GETFD); ERROR("after select fcntl ret=%d, ret & O_NONBLOCK = %d", rf, rf & O_NONBLOCK); if (connect(sock, (struct sockaddr *)&dst, sizeof (dst)) != 0) { strerror_r(errno, err, 254); ERROR("%s: error on connect -> %s", __func__, err); close(sock); return (RESP_ERROR); } 

And this is from the magazine:

March 6, 10:42:04 tcpclient: fcntl ret = 0, ret and O_NONBLOCK = 0

March 6, 10:42:04 tcpclient: after selecting fcntl ret = 0, ret and O_NONBLOCK = 0

March 6, 10:42:14 tcpclient: authenticate: error on connect -> Operation in progress

This seems to be a blocking socket, but returns an error typical of non-blocking? Linux - 2.6.18-308.el5. Any ideas?

+6
source share
2 answers

If timeout not 0 , the connect() call expires and returns. This happens whether the connection is established or not.

Since the timeout expires, connect() behaves as if it had been called on a non-blocking socket.

Referring to this case (literally from man connect and ignoring "immediately" below):

EINPROGRESS

The socket is non-blocking and the connection cannot be made immediately. To complete, you can select (2) or poll (2) by selecting the recording slot. After select (2) indicates writeability, use getsockopt (2) to read the SO_ERROR parameter at the SOL_SOCKET level to determine if connection () was successful (SO_ERROR is zero) or failed (SO_ERROR is one of the usual codes errors listed here, reasons for failure).


Btw: Can anyone confirm this standard behavior, but for the one mentioned somewhere explicitly ?

man 7 socket (italics mine):

SO_RCVTIMEO and SO_SNDTIMEO

Indicate the time of receipt or sending until you report an error. [...] If the data was not transmitted and the timeout was reached, then -1 is returned with an error set to EAGAIN or EWOULDBLOCK in the same way as if the socket was specified for non-blocking. [...] Timeouts affect only system calls that perform socket I / O (for example, reading (2), recvmsg (2), send (2), sendmsg (2)); timeouts do not affect selection (2), polling (2), epoll_wait (2), etc.

There are no words regarding connect() , so I'm not sure if my answer will hold.

+4
source

Try using 'if ((connect (...)) <0)'. Perhaps you are not mistaken at all.

0
source

All Articles