My program accepts up to 4 connections (using select function). Once they're connect, they have 5 seconds to send a string, indicating that they want to stay connected. Those that do not send within 5 seconds, or has the wrong passcode, will be disconnected.
I've created a small timer program, that is forked whenever a connection is established. The forked timer will send back a signal to the original program if 5 seconds are gone. In which case, the signal handler will close the file descriptor, and clear the connection.
My problem is, whenever the signal handler is triggered, select() returns -1, indicating it has failed. Does anyone know why this is happening? Or if there's another timing mechanism I could use?
I believe this is the intended behavior of select()
: return -1 with errno appropriately set if a signal occurs.
I don't think threads are the way to go here. I assume you want a program with roughly this structure (pardon the java-esque naming, but you get the point):
int fdsThatResponded[FDCOUNT];
memset(fdsThatResponded, 0, sizeof(int)*FDCOUNT);
while (time_elapsed < 5) {
ret = select(......);
if (-1 == ret) {
handleError();
}
checkWhichFdAndHandleAppropriately();
reinitializeTimerForSelectWithRemainingTime();
}
Does that help at all?