strclithread2.c 743 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "unpthread.h"
  2. void *copyto(void *);
  3. static int sockfd;
  4. static FILE *fp;
  5. static int done;
  6. void
  7. str_cli(FILE *fp_arg, int sockfd_arg)
  8. {
  9. char recvline[MAXLINE];
  10. pthread_t tid;
  11. sockfd = sockfd_arg; /* copy arguments to externals */
  12. fp = fp_arg;
  13. Pthread_create(&tid, NULL, copyto, NULL);
  14. while (Readline(sockfd, recvline, MAXLINE) > 0)
  15. Fputs(recvline, stdout);
  16. if (done == 0)
  17. err_quit("server terminated prematurely");
  18. }
  19. void *
  20. copyto(void *arg)
  21. {
  22. char sendline[MAXLINE];
  23. while (Fgets(sendline, MAXLINE, fp) != NULL)
  24. Writen(sockfd, sendline, strlen(sendline));
  25. Shutdown(sockfd, SHUT_WR); /* EOF on stdin, send FIN */
  26. done = 1;
  27. return(NULL);
  28. /* return (i.e., thread terminates) when end-of-file on stdin */
  29. }