I have the following program:
#include <stdio.h>
#define STDIN 0
int main()
{
fd_set fds;
int maxfd;
// sd is a UDP socket
maxfd = (sd > STDIN)?sd:STDIN;
while(1){
FD_ZERO(&fds);
FD_SET(sd, &fds);
FD_SET(STDIN, &fds);
select(maxfd+1, &fds, NULL, NULL, NULL);
if (FD_ISSET(STDIN, &fds)){
printf("\nUser input - stdin");
}
if (FD_ISSET(sd, &fds)){
// socket code
}
}
}
The problem I face is that once input is detected on STDIN, the message "User input - stdin" keeps on printing...why doesn't it print just once and on next while loop check which of the descriptors has input ?
Thanks.
The select
function only tells you when there is input available. If you don't actually consume it, select will continue falling straight through.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With