Can't seem to get a timeout working when connecting to a socket

前端 未结 2 1723
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 05:03

I\'m trying to supply a timeout for connect(). I\'ve searched around and found several articles related to this. I\'ve coded up what I believe should work but unfortunately I

2条回答
  •  面向向阳花
    2021-01-21 05:30

    From http://lxr.free-electrons.com/source/net/unix/af_unix.c:

    441 static int unix_writable(const struct sock *sk)
    442 {
    443         return sk->sk_state != TCP_LISTEN &&
    444                (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
    445 }
    

    I'm not sure what these buffers are that are being compared, but it looks obvious that the connected state of the socket is not being checked. So unless these buffers are modified when the socket becomes connected it would appear my unix socket will always be marked as writable and thus I can't use select() to determine when the non-blocking connect() has finished.

    and based on this snippet from http://lxr.free-electrons.com/source/net/unix/af_unix.c:

    1206 static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
    1207                                int addr_len, int flags)
    .
    .
    .
    1230         timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
    .
    .
    .
    1271         if (unix_recvq_full(other)) {
    1272                 err = -EAGAIN;
    1273                 if (!timeo)
    1274                         goto out_unlock;
    1275 
    1276                 timeo = unix_wait_for_peer(other, timeo);
    .
    .
    .
    

    it appears setting the send timeout might be capable of timing out the connect. Which also matches the documentation for SO_SNDTIMEO at http://man7.org/linux/man-pages/man7/socket.7.html.

    Thanks, Nick

提交回复
热议问题