From db66484c4300f5f0e857eff01d15fd3593002a79 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 5 Jun 2020 14:09:18 +0200 Subject: Initial commit --- ctest.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 ctest.c (limited to 'ctest.c') diff --git a/ctest.c b/ctest.c new file mode 100644 index 0000000..2e871cb --- /dev/null +++ b/ctest.c @@ -0,0 +1,74 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PIPE_RD 0 +#define PIPE_WR 1 + +int +main(int argc, char **argv) { + int sfd; + struct sockaddr_in addr; + int zfd; + int pfd[2]; + size_t total = 0; + + if (argc != 4) { + fprintf(stderr, "Usage: %s <64k count>\n", argv[0]); + exit(EXIT_FAILURE); + } + + sfd = socket(AF_INET, SOCK_STREAM, 0); + if (sfd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(atoi(argv[2])); + addr.sin_addr.s_addr = inet_addr(argv[1]); + + if (connect(sfd, &addr, sizeof(addr)) < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + zfd = open("/dev/zero", O_RDONLY | O_CLOEXEC); + if (zfd < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + + if (pipe2(pfd, O_CLOEXEC) < 0) { + perror("pipe2"); + exit(EXIT_FAILURE); + } + + for (int i = 0; i < atoi(argv[3]); i++) { + ssize_t r, w; + + r = splice(zfd, NULL, pfd[PIPE_WR], NULL, 64 * 1024, SPLICE_F_MOVE); + //fprintf(stderr, "Read %zi bytes from /dev/zero\n", r); + w = splice(pfd[PIPE_RD], NULL, sfd, NULL, r, SPLICE_F_MOVE); + //fprintf(stderr, "Wrote %zi bytes to socket\n", w); + if (r != w) { + fprintf(stderr, "Read/write mismatch\n"); + exit(EXIT_FAILURE); + } + total += w; + } + + printf("Client: sent %zu bytes\n", total); + exit(EXIT_SUCCESS); +} + -- cgit v1.2.3