I read that I can make the socket non-blocking and use select () and pass the timeout value for select() , but I'm looking for a solution to block sockets.
You can do this when blocking a socket:
sock = socket(...); bind(sock, ...); listen(sock, ...); while (program_running()) { timeval timeout = {1, 0}; fd_set fds; FD_ZERO(&fds); FD_SET(sock, &fds); select(sock+1, &fds, NULL, NULL, &timeout); if (FD_ISSET(sock, &fds)) { client = accept(sock, ...); // do things with client }
From MSDN accepts documentation of the functions :
The readfds parameter defines sockets that should be checked for readability. If the socket is currently in the listening state, it will be marked as readable if a request for an incoming request has been received to guarantee accept, without blocking .
purplepsycho
source share