I am writing a program that receives UDP messages and also receives data from users, however my STDIN still blocks the selection. When I FD_CLR, stdin fd works fine before selecting a program, indicating that the stdin socket is always ready to read data from it. I tried introducing temporary television to time, but this does not seem to work. Should I close the socket somewhere or call FD_CLR, where I am not? The end result should not be a blocking STDIN, but it is currently blocking. Thank you.
int
wait_for_input(){
fd_set fds;
int maxfd, sd, err, n;
struct sockaddr_in addr;
char stdbuf[BUFLEN];
unsigned char udpbuf[BUFLEN];
memset(stdbuf,0x0,sizeof(udpbuf));
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
printf("Failed to Open UDP socket");
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(host_list[0]->port);
err = bind(sd,(struct sockaddr *) &addr,sizeof(addr));
if(err < 0){
printf("ERROR: Cant bind port");
}
struct timeval tv;
while(1){
FD_ZERO(&fds);
FD_SET(STDIN_FILENO,&fds);
FD_SET(sd,&fds);
tv.tv_sec = 1;
tv.tv_usec = 0;
fflush(stdout);
select(sd+1,&fds,NULL,NULL,&tv);
if(FD_ISSET(sd,&fds)){
n = recv(sd,udpbuf,sizeof(udpbuf),0);
unpack(udpbuf);
recompute_my_dv();
fflush(stdout);
}
if(FD_ISSET(STDIN_FILENO, &fds)){
fgets(stdbuf,sizeof(stdbuf),stdin);
parse(stdbuf);
printf("server> ");
fflush(stdout);
FD_CLR(STDIN_FILENO,&fds);
}
}
return 0;
}
source
share