strcliselect01.c 691 B

123456789101112131415161718192021222324252627282930
  1. #include "unp.h"
  2. void
  3. str_cli(FILE *fp, int sockfd)
  4. {
  5. int maxfdp1;
  6. fd_set rset;
  7. char sendline[MAXLINE], recvline[MAXLINE];
  8. FD_ZERO(&rset);
  9. for ( ; ; ) {
  10. FD_SET(fileno(fp), &rset);
  11. FD_SET(sockfd, &rset);
  12. maxfdp1 = max(fileno(fp), sockfd) + 1;
  13. Select(maxfdp1, &rset, NULL, NULL, NULL);
  14. if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
  15. if (Readline(sockfd, recvline, MAXLINE) == 0)
  16. err_quit("str_cli: server terminated prematurely");
  17. Fputs(recvline, stdout);
  18. }
  19. if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
  20. if (Fgets(sendline, MAXLINE, fp) == NULL)
  21. return; /* all done */
  22. Writen(sockfd, sendline, strlen(sendline));
  23. }
  24. }
  25. }