blocking select() from multiple sockets blocking select() from multiple sockets unix unix

blocking select() from multiple sockets


If you detect that a connection is closed, remove the socket from the fd set, otherwise select is going to report them (Socket 4 hung up).. select is not edge triggered, if you don't handle the event, it's going to report it again.


Indeed, if recv returns 0 (and not -1, with errno=EWOULDBLOCK), the socket is closed. You should call close() on it as well, and take it out of the select() call. Otherwise it will remain in WAIT1 and release select() each time.


You are using FD_ISSET incorrectly. You need to be passing a socket ID to the "fd" parameter, not an index:

if (FD_ISSET(i, &read_fds))...

needs to be

if (FD_ISSET(sockfd[i], &read_fds))...

Likewise with recv.