sockets and stdin program. Need assistance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<winsock.h>
#include<iostream.h>
#include<conio.h>
 // file descriptor for standard input
int main(void)
{
struct timeval tv;
fd_set readfds;
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
int STDIN =_fileno(stdin);
FD_SET(STDIN, &readfds);
// don’t care about writefds and exceptfds:
select(STDIN+1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(STDIN, &readfds))
cout<<"A key was pressed!\n";
else
cout<<"Timed out.\n";
getch();
return 0;
}



I intended the above program to wait and watch for 2.5 before timing out or print "a key was pressed !" if a user presses a key. But almost immediately it prints "A key was pressed", even if was not. Any suggestions?
Last edited on
Topic archived. No new replies allowed.