#define _POSIX_SOURCE #include #include #include #include #include #include #include #include #include #include #include void sig_chld(int signo) { pid_t pid; int stat; // the following code will make sure that we remove // ALL terminated child processes while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) printf("child %d terminated.\n", (int)pid); return ; } /* this routine read n bytes * using the function from the textbook, instead of our own */ ssize_t readn(int fd, void *vptr, size_t n) { size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft = n; while (nleft > 0) { if ((nread = read(fd, ptr, nleft)) < 0) { if (errno == EINTR) { nread = 0; // and call read() again } else { return (-1); } } else if (nread == 0) { break; // end-of-file } nleft -= nread; ptr += nread; } return (n - nleft); } /* this routine read n bytes */ // int readn (int sock, char *msgp, unsigned msgsize) // { // int rlen; // int temp; // // do { // rlen = read(sock, msgp, msgsize); // } while (rlen == -1 && errno == EINTR); // // if (rlen == 0) { // /* connection closed */ // return 0; // } // // // while (rlen 0) write(rec_sock, buf, num); } exit(0); } else { close(rec_sock); } } }