strcliselect02.c 882 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "unp.h"
  2. void
  3. str_cli(FILE *fp, int sockfd)
  4. {
  5. int maxfdp1, stdineof;
  6. fd_set rset;
  7. char buf[MAXLINE];
  8. int n;
  9. stdineof = 0;
  10. FD_ZERO(&rset);
  11. for ( ; ; ) {
  12. if (stdineof == 0)
  13. FD_SET(fileno(fp), &rset);
  14. FD_SET(sockfd, &rset);
  15. maxfdp1 = max(fileno(fp), sockfd) + 1;
  16. Select(maxfdp1, &rset, NULL, NULL, NULL);
  17. if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
  18. if ( (n = Read(sockfd, buf, MAXLINE)) == 0) {
  19. if (stdineof == 1)
  20. return; /* normal termination */
  21. else
  22. err_quit("str_cli: server terminated prematurely");
  23. }
  24. Write(fileno(stdout), buf, n);
  25. }
  26. if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
  27. if ( (n = Read(fileno(fp), buf, MAXLINE)) == 0) {
  28. stdineof = 1;
  29. Shutdown(sockfd, SHUT_WR); /* send FIN */
  30. FD_CLR(fileno(fp), &rset);
  31. continue;
  32. }
  33. Writen(sockfd, buf, n);
  34. }
  35. }
  36. }