Sometimes it is needed to make a socket non-blocking so that I/O requests on the socket don’t block. For a non-blocking socket, when I/O requests like read() and write() return a negative value, the errno should be checked against -EWOULDBLOCK code to find if the I/O request would have blocked.

The following utility function sets non-blocking flag of a socket.

int socketSetNonBlocking(int sockfd)
{
  unsigned int flags;

  flags = fcntl(sockfd. F_GETFL, 0);
  if(flags < 0) return -1;
  if(fcntl(sockfd, F_SETFL, flags|O_NONBLOCK) < 0) return -1;
  return 0;
}

Similarly the following function makes a non-blocking socket a blocking socket.

int socketSetBlocking(int sockfd)
{
  unsigned int More >