Quote (Toilet @ Dec 7 2014 03:45pm)
From abduct:
Select() is not meant to block, or rather not block for a significate amount of time. As per the &tv value you passed you are timing the funciton out at 0 seconds. This means if there is no FILE DESCRIPTOR ready to be read or written to before the time out is reached then it will return nothing. This explains the behavour you are getting. If you wish to correct this simply install your code within a loop so that select is called every iteration, or expand the timeout so that it gets a chance to look for descriptors that are in need of reading.
Yea sorry the codes in a while loop, so it runs continuously until I manually shut down the program.
Sorry guess I should have made it clearer. In my code I do have the FD statements inside of a while loop as shown below.
Code
int retval;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
while(;;){
FD_ZERO(&readfds);
FD_SET(fileno(stdin), &readfds); // stdin fd(0)
FD_SET(socket, &readfds); // socket listening on
retval = select(socket + 1, &readfds, NULL, NULL, &tv);
if(FD_ISSET(0, &readfds)){
fgets(buf, 1024, stdin);
printf("%s\n", buf);
fflush(stdout);
}
if(FD_ISSET(socket + 1, &readfds)){
printf("message received");
fflush(stdout);
}
}