套接字accept()是否返回描述符阻塞或非阻塞?

4 c sockets

套接字描述符是由accept()函数在阻塞或非阻塞模式下返回的吗?

Som*_*ude 5

不,套接字不会从侦听套接字继承非阻塞状态。你必须自己让它不阻塞。

  • @RemyLebeau-TeamB 是的,但它没有明确提及非阻塞状态,所以为了安全起见,我仍然会将新接受的袜子设置为非阻塞。 (2认同)

ide*_*ixs 5

来自man 2 accept:

   int accept4(int sockfd, struct sockaddr *addr,
               socklen_t *addrlen, int flags);
Run Code Online (Sandbox Code Playgroud)

进一步向下:

   If flags is 0, then accept4() is the same as accept().   The  following
   values can be bitwise ORed in flags to obtain different behavior:

   SOCK_NONBLOCK   Set  the  O_NONBLOCK  file  status flag on the new open
                   file description.  Using this flag saves extra calls to
                   fcntl(2) to achieve the same result.
Run Code Online (Sandbox Code Playgroud)

因此,我希望从accept()返回的套接字描述符处于阻塞模式.