C – I want the accept system call to be non-blocking. How can I make accepting system calls non-blocking?

I want the accept system call to be non-blocking. How can I make accepting system calls non-blocking?… here is a solution to the problem.

I want the accept system call to be non-blocking. How can I make accepting system calls non-blocking?

This is the statement I’m using :

m_stat_arr_nClient_sockfd[nIndex]= accept(nServerSocket,(struct sockaddr *)&client_address, (socklen_t *)&client_len);

This is a blocking call, so how can I make it a non-blocking call?

Solution

You must use fcntl to set your nServerSocket to non-blocking;

int flags = fcntl(nServerSocket, F_GETFL, 0);
fcntl(nServerSocket, F_SETFL, flags | O_NONBLOCK);

Once done, calling accept() on that socket will no longer block.

Related Problems and Solutions