non blocking socket select returns 1 after connect

前端 未结 2 442
遇见更好的自我
遇见更好的自我 2020-12-21 10:29

First of all I would like to say that this is another problem than this one: Similar but not the same

My code looks like this:

struct addrinfo hints,         


        
相关标签:
2条回答
  • 2020-12-21 10:29

    This is correct behavior, because a pending error causes a socket to become both readable and writable. As with any other system call, you need to check for error conditions explicitly. You could include the socket in the exception fdset (the fourth argument to select()), and pick up a notification directly, or you could check later for an error in a number of ways.

    Be careful of writing to the socket, as a SIGPIPE could happen. In general, you should set a SIGPIPE handler and handle the error synchronously with EPIPE.

    0 讨论(0)
  • 2020-12-21 10:53

    I don't know where in the man pages you see that it should time out.

    If there is no firewall dropping the packets, the connection will be refused pretty fast (one packet from your host, one packet reply). So an "event" on the connecting socket will come in as soon as the reset is received. This will wake up select, with (at least) one active socket.

    The first attempt to read from or write to that socket will return the underlying connect error.

    0 讨论(0)
提交回复
热议问题