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_SET(mcastsockfd, &readSet); // multicast socket
n = max(n, mcastsockfd);

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");
readsize = recv(newfd, msgbuff, MAX_SIZE-1, 0);

if(readsize == -1) {
perror("recv local");
//handleLocalMessage(newfd, buf, readsize);
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){

// 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!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!