readv writev 简介 一次读写多个缓冲区

55次阅读

共计 458 个字符,预计需要花费 2 分钟才能阅读完成。

一个小例子说明函数使用:
结构说明:
struct iovec {
void * iov_base // 缓冲区地址
size_t iov_len // 缓冲区输入 / 输出长度
}

#include “util.h”
#include <sys/uio.h>

int main(int argc , char **argv)
{
struct iovec v[2];

char buf1[] = “nihao”;
char buf2[] = “fuck me”;
v[0].iov_base = buf1;
v[0].iov_len = 3; // 输入 / 输出 3 个字节
v[1].iov_base = buf2;
v[1].iov_len = 4; // 输入 / 输出 4 个字节
int n = writev(1,v,2);
printf(“\n write bytes:%d\n” , n);

puts(“reading from stdin”);
n = readv(0,v,2);
printf(“read bytes:%d\n”,n);
printf(“buf1:%s\n” ,buf1);
printf(“buf2:%s\n”,buf2);

return 0;
}

正文完
 0