Is it possible (and safe) to make an accepting socket non-blocking?

后端 未结 2 1541
小蘑菇
小蘑菇 2020-12-16 15:28

I\'m looking for a way to interrupt an accept() call on a blocking socket. Using signals is not an option, as this is meant to be in a library and I don\'t want to clutter t

相关标签:
2条回答
  • In the question, You are saying that you do not want to use select (or poll or epoll) which are the best ways for IO multiplexing. I would recommend you using one another thread just for listening sockets while this is a bad idea!

    0 讨论(0)
  • 2020-12-16 16:09

    No idea about Windows, but the behavior you want is guaranteed by POSIX:

    If the listen queue is empty of connection requests and O_NONBLOCK is not set on the file descriptor for the socket, accept() shall block until a connection is present. If the listen() queue is empty of connection requests and O_NONBLOCK is set on the file descriptor for the socket, accept() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].

    Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

    Also, select or poll can be used to check for incoming connections by polling for the listening socket in the reading set.

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