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

版权声明: https://blog.csdn.net/dashoumeixi/article/details/85137904

一个小例子说明函数使用:

结构说明:

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;
}

猜你喜欢

转载自blog.csdn.net/dashoumeixi/article/details/85137904