strclithread.c 688 B

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