Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563111
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-09-23 17:15:23

字节流套接字上调用read、write输入或输出的字节数可能比请求的数量少,然而这不是出错的状态。这个现象的原因在于内核中用于套接字的缓冲区可能已达到了极限。此时需要再次调用read或write函数,以输入或输出剩余的字节。
以下三个函数就是用于此目的:

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include "readn.h"

  7. int readn(int fd, void* buffer, size_t n)
  8. {
  9.     int nread;
  10.     int nleft;
  11.     char *ptr;

  12.     ptr = buffer;
  13.     nleft = n;
  14.     while(nleft >0) {
  15.         if((nread=read(fd,ptr,nleft)) < 0){
  16.             if(errno == EINTR) {
  17.                 nread = 0;
  18.                 continue;
  19.             }else {
  20.                 return -1;
  21.             }
  22.         }else if (nread==0) {
  23.             break;
  24.         }
  25.         ptr += nread;
  26.         nleft -= nread;
  27.     }
  28.     return (n - nleft);
  29. }

  30. int writen(int fd, void* buffer, size_t n)
  31. {
  32.     int nwrite;
  33.     int nleft;
  34.     char* ptr;

  35.     ptr = buffer;
  36.     nleft = n;
  37.     while(nleft > 0) {
  38.         if((nwrite = write(fd,ptr,nleft)) < 0) {
  39.             if(errno == EINTR) {
  40.                 nwrite = 0;
  41.                 continue;
  42.             }else {
  43.                 return -1;
  44.             }
  45.         }else if(nwrite == 0){
  46.             break;
  47.         }
  48.         ptr += nwrite;
  49.         nleft -= nwrite;
  50.     }
  51.     return (n-nleft);
  52. }

  53. size_t readline(int fd, void * buffer, size_t maxlen)
  54. {
  55.     int i;
  56.     int nread;
  57.     char c;
  58.     char *ptr;

  59.     ptr=buffer;
  60.     for(i = 0;i < maxlen;i++) {
  61. again:
  62.         if((nread = read(fd,&c,1)) == 1){
  63.             *ptr++ = c;
  64.             if(c == '\n')
  65.                 break;
  66.         }else if(nread == 0) {
  67.             *ptr = 0;
  68.             return i-1;
  69.         }else {
  70.             if(errno == EINTR) {
  71.                 goto again;
  72.             }else {
  73.                 return -1;
  74.             }
  75.         }
  76.     }
  77.     *ptr = 0;
  78.     return i;
  79. }
  80. static char *ptr;
  81. static int len = 0;
  82. char read_buffer[MAXLINE];
  83. ssize_t my_read(int fd, char* buffer)
  84. {
  85.     int nread;

  86.     if(len <= 0) {
  87. again:
  88.         if((nread = read(fd,read_buffer,MAXLINE)) < 0) {
  89.             if(errno == EINTR)
  90.                 goto again;
  91.             return -1;
  92.         }else if(nread == 0) {
  93.             return 0;
  94.         }
  95.         ptr = read_buffer;
  96.         len = nread;
  97.     }
  98.     *buffer = *ptr++;
  99.     len--;
  100.     return 1;
  101. }



阅读(856) | 评论(0) | 转发(0) |
0

上一篇:printf %s inet_ntoa coredump

下一篇:fgets & fputs

给主人留下些什么吧!~~