csocketsnetwork-programmingnonblocking

accept4 blocks though SOCK_NONBLOCK is set


I'm trying to accept a socket non-blockingly:

accept4(s, (struct sockaddr *) &peerAddress, &len,SOCK_NONBLOCK);

where as s is a fd, peerAddres is an address, and len, is its length. I wish that accept won't block the thread. Though, once I debug, the process is stuck at this line, while no connection is pending. What is my mistake?


Solution

  • SOCK_NONBLOCK just sets the newly accepted socket to non-blocking. It does not make accept itself non-blocking. For this one would need to set the listen socket non-blocking before calling accept.