#define _GNU_SOURCE#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>
#include <fcntl.h>#include <sys/stat.h>void check(int res, const char *fucname){if(res < 0)
{perror(fucname);exit(1);}}int main()
{int res;
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in local;
local.sin_family = AF_INET;local.sin_addr.s_addr = htonl(INADDR_ANY);local.sin_port = htons(5000);res = bind(listenfd, (struct sockaddr *)&local, sizeof(local));check(res, "bind");
res = listen(listenfd, 3);check(res, "listen");
int connfd = accept(listenfd, NULL, NULL);
int pipefd[2] = {0x00};
pipe(pipefd);struct iovec buff;
buff.iov_base = malloc(4096);buff.iov_len = 4096;while(1)
{splice(connfd, NULL, pipefd[1], NULL, 4096, SPLICE_F_MOVE | SPLICE_F_MORE);res = vmsplice(pipefd[0], &buff, 1, SPLICE_F_GIFT);check(res, "splice");
char *pstr = (char *)buff.iov_base;fputs(pstr, stdout);memset(buff.iov_base, 0, 4096);}free(buff.iov_base);close(listenfd);return 0;
}
linux vmplice 示例
最新推荐文章于 2022-12-30 15:05:33 发布