Using select() for non-blocking sockets to connect always returns 1

前端 未结 2 1886
有刺的猬
有刺的猬 2020-12-10 08:17

This question is very similar (or almost identical) to In a non blocking socket connect, select() always returns 1; however, I can\'t seem to find where my code is faltering

相关标签:
2条回答
  • 2020-12-10 08:28

    When connecting in non-blocking mode and select() indicates the connection is writeable, you are then supposed to call connect() again. Doing so will return -1 with errno == ECONNRESET or whatever it is.

    0 讨论(0)
  • 2020-12-10 08:42

    What do you expect select() to return? Consider that select() is normally used to wait for multiple file descriptors - if you were connecting two, how would you know which one succeeded/failed based purely on the return value of select? You wouldn't, obviously.

    Which is why select() just tells you which file descriptors have changed in some way, and you're supposed to determine independently what that was. In the case of connect(), you should call getsockopt() to retrieve the result of the connection attempt. See this answer where I explain how to do a non-blocking connect().

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