• 使用select正确处理EOF的str_cli函数


    void str_cli(FILE *fp, int sockfd) {
        int maxfdp1, stdineof;
        fd_set rset;
        char buf[MAXLINE];
        int n;
    
        stdineof = 0; 
        FD_ZERO(&rset);
        for ( ; ; ) {
          if (stdineof == 0) {
              FD_SET(fileno(fp), &rset);
          }
            FD_SET(sockfd, &rset);
          maxfdp1 = max(fileno(fp), sockfd) + 1;
          select(maxfdp1, &rset, NULL, NULL, NULL);
    
          if (FD_ISSET(sockfd, &rset)) {      /* socket is readable */
              if ( (n = read(sockfd, buf, MAXLINE)) == 0) {
                  if (stdineof == 1) {
                    return;
                } else {
                      exit(1);
                }
              }
              write(fileno(stdout), buf, n);
          }
    
          if (FD_ISSET(fileno(fp), &rset)) { /* inout is readable */
              if ( (n = read(fileno(fp), buf, MAXLINE)) == 0) {
                  stdineof = 1;
                shutdown(sockfd, SHUT_WR); /* send FIN */
                FD_CLR(fileno(fp), &rset);
                continue;
              }
              writen(sockfd, buf, n);
          }
        }
    }
     
  • 相关阅读:
    Android新手引导库推荐
    windbg 常调用指令
    通过Hook NtOpenProcess 函数实现反调试
    PE文件
    消息机制
    软件调试
    异常(2) --- 编译器对于SEH异常的拓展
    异常(1)
    等待对象
    进程与线程
  • 原文地址:https://www.cnblogs.com/soldierback/p/10700791.html
Copyright © 2020-2023  润新知