select always returns -1 while trying to read from socket and stdin
- by Aleyna
Hello
I have the following code implemented on C++(Linux) to check on my listening socket and stdin using select. select however keeps returning -1 no matter what I try to do! What's wrong with that code :s I will appreciate any help. Thanks
highsock = m_sock; //listening socket
memset((char *) &connectlist, 0, sizeof(connectlist));
memset((char *) &socks, 0, sizeof(socks));
int readsocks;
struct timeval timeout;
timeout.tv_sec = 60;
timeout.tv_usec = 0;
while (1) {
updateSelectList();
//cout << "highest sock: " << highsock << endl;
tempreadset = readset;
readsocks = select(highsock+1, &tempreadset, NULL, NULL, &timeout);
//cout << "# ready: " << readsocks << endl;
if (readsocks < 0) {
if (errno == EINTR)
continue;
cout << "select error" << endl;
}
if (readsocks > 0) {
readFromSocks();
}
}
void readFromSocks() {
if (FD_ISSET(STDIN, &readset)) {
...
} else if (FD_ISSET(m_sock, &readset)) {
...
}
}
void updateSelectList() {
FD_ZERO(&readset);
FD_SET(STDIN, &readset);
FD_SET(m_sock, &readset);
for (int i=0; i<MAXCONNECTIONS; i++) {
if (connectlist[i] != 0) {
FD_SET(connectlist[i], &readset);
if (connectlist[i] > highsock)
highsock = connectlist[i];
}
}
highsock = max(max(m_sock, STDIN), highsock);
}