How to exit accepting () call on Windows?

I have a blocking accept() call in a thread that is waiting for connection requests. When the application is about to close, I want the thread waiting on accept() to accept() gracefully. I found in the documentation for Winsock that I can set a timeout value for send() and recv() , but I cannot do this for accept() .

I read that I can make the socket non-blocking and use select() , and pass the timeout value to select() , but I'm looking for a solution to block sockets.

+7
c winapi sockets winsock
source share
1 answer

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 .

+1
source share

All Articles