You need to reinitialize the descriptor sets before each call to select()
becuase select()
modifies the descriptor sets. You can do this either by using FD_ZERO()
/FD_SET()
inside the loop or by initializing 'prototype' sets that you copy over the ones passed to select()
:
fd_set readset, writeset;
int maxfd = 0;
maxfd = max(maxfd, JohnDoe.read_fd());
maxfd = max(maxfd, JohnDoe.write_fd());
maxfd = max(maxfd, JoeSmith.read_fd());
maxfd = max(maxfd, JoeSmith.write_fd());
maxfd = max(maxfd, JaneSmith.read_fd());
maxfd = max(maxfd, JaneSmith.write_fd());
int numready;
int count = 0;
while (count < 10) {
FD_ZERO(&readset);
FD_SET(JohnDoe.read_fd(), &readset);
FD_SET(JoeSmith.read_fd(), &readset);
FD_SET(JaneSmith.read_fd(), &readset);
FD_ZERO(&writeset);
FD_SET(JohnDoe.write_fd(), &writeset);
FD_SET(JoeSmith.write_fd(), &writeset);
FD_SET(JaneSmith.write_fd(), &writeset);
numready = select(maxfd + 1, &readset, &writeset, NULL, NULL);
// etc...
}