Results 1 to 1 of 1
My program need get commands or messages from network sockets and stdin. I use select() to achieve that.
If I type commands, the STDIN_FILENO in select() works well. However if ...
- 02-07-2009 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 3
Endless input to STDIN_FILENO in select()
My program need get commands or messages from network sockets and stdin. I use select() to achieve that.
If I type commands, the STDIN_FILENO in select() works well. However if I just a pipe to feed the command, the program will process the command in a deal loop, i.e. it will accept the command from the pipe endless. Do I miss something?
Following is the code:
bool running = true;
while(running) {
int n = 0;
long readsize = 0;
FD_ZERO(&readSet);
FD_SET(mcastsockfd, &readSet); // multicast socket
n = max(n, mcastsockfd);
FD_SET(STDIN_FILENO, &readSet);
n = max(n, STDIN_FILENO);
FD_SET(usockfd, &readSet);
n = max(n, usockfd);
select(n+1, &readSet, NULL, NULL, NULL);
if(FD_ISSET(mcastsockfd, &readSet)) {
// receive messages from multicast socket, do something
}
else if(FD_ISSET(usockfd, &readSet)){
int newfd;
sockaddr_un cmdclient;
socklen_t cmdclientlen = sizeof(cmdclient);
if((newfd = accept(usockfd, (struct sockaddr *)&cmdclient, &cmdclientlen)) == -1) {
perror("accept local");
exit(1);
}
readsize = recv(newfd, msgbuff, MAX_SIZE-1, 0);
if(readsize == -1) {
perror("recv local");
exit(1);
}
//handleLocalMessage(newfd, buf, readsize);
close(newfd);
}
else if(FD_ISSET(STDIN_FILENO, &readSet)) {
readsize = read(STDIN_FILENO, cmdbuff, MAX_SIZE);
if (strncmp(cmdbuff,"quit",4)==0)
running = false;
else if (strncmp (cmdbuff,"change",5) == 0){
sendReq(MSG_SET_UPDATE);
// This message will keep showing if I feed a command through a pipe
cout << " get a local message" << endl;
}
}
}
I use the pipe input the commands in this way:
echo "change" | myprogram
Thanks a lot!


