str_cli_select02.c 896 B

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