select always returns -1 while trying to read from socket and stdin
Posted
by Aleyna
on Stack Overflow
See other posts from Stack Overflow
or by Aleyna
Published on 2010-04-25T01:27:45Z
Indexed on
2010/04/25
1:33 UTC
Read the original article
Hit count: 363
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);
}
© Stack Overflow or respective owner