readn和writen函数

  1 int readn(int connfd, void *pbuf, int nums)
  2 {
  3     int nleft = 0;
  4     int nread = 0;
  5     char *pread_buf = NULL;
  6     struct timeval select_timeout;
  7     fd_set rset;
  8     int ret = 0;
  9 
 10     memset(&select_timeout, 0, sizeof(select_timeout));
 11 
 12     if ((connfd <= 0) || (NULL == pbuf) || (nums <= 0))
 13     {
 14         printf("readn: param err.\n");
 15         return -1;
 16     }
 17 
 18     pread_buf = (char *)pbuf;
 19     nleft = nums;
 20 
 21     while (nleft > 0)
 22     {
 23         select_timeout.tv_sec = 10;
 24         select_timeout.tv_usec = 0;
 25 
 26         FD_ZERO(&rset);
 27         FD_SET((unsigned int)connfd, &rset);
 28         ret = select(connfd + 1, &rset, NULL, NULL, &select_timeout);        
 29         if (ret < 0)
 30         {
 31             printf("readn: select failed, %s\n", strerror(errno));
 32             return -1;
 33         }
 34         else if (ret == 0)
 35         {
 36             break;
 37         }
 38         else
 39         {
 40             if (FD_ISSET(connfd, &rset))
 41             {
 42                 nread = recv(connfd, pread_buf, nleft, 0);
 43                 if (nread < 0)
 44                 {
 45                     perror("recv");
 46                     if (EINTR == errno)
 47                     {
 48                         nread = 0;
 49                     }
 50                     else
 51                     {
 52                         printf("recv, %s\n", strerror(errno));
 53                         return -1;
 54                     }
 55                 }
 56                 else if (0 == nread)
 57                 {
 58                     break;
 59                 }
 60                 else
 61                 {
 62                     nleft -= nread;
 63                     pread_buf += nread;
 64                 }
 65             }
 66             else
 67                 return -1;
 68         }
 69     }
 70 
 71     return(nums - nleft);
 72 }
 73 
 74 int writen(int connfd, void *pbuf, int nums)
 75 {
 76     int nleft = 0;
 77     int nwritten = 0;
 78     char *pwrite_buf = NULL;
 79     struct timeval select_timeout;
 80     fd_set rset;
 81 
 82     memset(&select_timeout, 0, sizeof(select_timeout));
 83 
 84     if ((connfd <= 0) || (NULL == pbuf) || (nums < 0))
 85     {
 86         printf("writen: param err.\n");
 87         return -1;
 88     }
 89 
 90     pwrite_buf = (char *)pbuf;
 91     nleft = nums;
 92 
 93     while(nleft > 0)
 94     {
 95 
 96         select_timeout.tv_sec = 10;
 97         select_timeout.tv_usec = 0;
 98 
 99         FD_ZERO(&rset);
100         FD_SET((unsigned int)connfd, &rset);
101         if (select(connfd + 1, NULL, &rset, NULL, &select_timeout) <= 0)
102         {    
103             printf("writen: select failed, %s\n", strerror(errno));
104             return -1;
105         }
106 
107         if (-1 == (nwritten = send(connfd, pwrite_buf, nleft, MSG_NOSIGNAL)))
108         {
109             if (EINTR == errno)
110             {
111                 printf("EINTR\n");
112                 nwritten = 0;
113             }
114             else 
115             {
116                 printf("Send() error, 0x%x\n", errno);
117                 return -1;
118             }
119         }
120         nleft -= nwritten;
121         pwrite_buf += nwritten;
122     }
123 
124     return(nums);
125 }

 

你可能感兴趣的:(readn和writen函数)