strclifork.c 532 B

12345678910111213141516171819202122232425
  1. #include "unp.h"
  2. void
  3. str_cli(FILE *fp, int sockfd)
  4. {
  5. pid_t pid;
  6. char sendline[MAXLINE], recvline[MAXLINE];
  7. if ( (pid = Fork()) == 0) { /* child: server -> stdout */
  8. while (Readline(sockfd, recvline, MAXLINE) > 0)
  9. Fputs(recvline, stdout);
  10. kill(getppid(), SIGTERM); /* in case parent still running */
  11. exit(0);
  12. }
  13. /* parent: stdin -> server */
  14. while (Fgets(sendline, MAXLINE, fp) != NULL)
  15. Writen(sockfd, sendline, strlen(sendline));
  16. Shutdown(sockfd, SHUT_WR); /* EOF on stdin, send FIN */
  17. pause();
  18. return;
  19. }