#include #include #include #include #include /* Common client and server code (for fifo and tcp samples). */ /* ====================================================== The client function reads from stdin (using readline), and unless the line is "quit", sends it to the file descriptor. Also quits if stdin is closed... */ void client(int fd) { char *line; while (line = readline("What should I send? ")) { if (strncmp(line,"quit",4)==0) break; if (write(fd,line,strlen(line))<0) { perror("Error writing to fifo"); exit(1); } /* readline strips the newline, we should send one... */ if (write(fd,"\n",1)<0) { perror("Error writing to fifo"); exit(1); } } close(fd); } /* ====================================================== The server function reads from a file descriptor, and sends everything it reads to stdout. This function returns only once the client has closed it's end of the connection (fifo or tcp connection). */ void server(int fd) { int cnt; char buf[100]; printf("Connection established, now waiting for input\n"); /* while read returns something, send to stdout */ /* read will return 0 when somebody has closed the other end */ while ((cnt = read(fd,buf,100))>0) { write(1,buf,cnt); } close(fd); }