//Server Program with named pipe #include #include #include #include #include #include #include char *fifoname = "channel"; int main(int argc, char **argv) { char buf[512]; char exitstr[5] = "exit"; int fd, check, len; fd_set fdset, temp; if( mkfifo(fifoname,0777) == -1){ perror("mkfifo()"); exit(3); } if( (fd = open(fifoname, O_RDONLY)) < 0 ){ perror("open()"); exit(1); } FD_ZERO(&fdset); FD_SET(fd, &fdset); while(1){ temp = fdset; check = select(fd+1, &temp, NULL, NULL, NULL); if( check == -1 ){ perror("select()"); exit(2); } else { if( FD_ISSET(fd, &temp) ){ len = read(fd, buf, sizeof(buf)); buf[len] = 0; if(len > 0) printf("Server: %s\n", buf); if(*buf == *exitstr){ close(fd); execlp("rm", "rm", "-f", fifoname,(char*)0); } } //End of if } //End of else } //End of while }