11 #include <netinet/in.h>
12 #include <arpa/inet.h>
19 static struct timeval start_time;
20 static unsigned long long kb_sent;
21 static unsigned long iters;
23 static unsigned long mtime_since(struct timeval *s, struct timeval *e)
27 sec = e->tv_sec - s->tv_sec;
28 usec = e->tv_usec - s->tv_usec;
29 if (sec > 0 && usec < 0) {
35 usec /= (double) 1000;
40 static unsigned long mtime_since_now(struct timeval *s)
44 gettimeofday(&t, NULL);
45 return mtime_since(s, &t);
48 void show_rate(int sig)
50 unsigned long msecs = mtime_since_now(&start_time);
52 printf("Throughput: %LuMiB/sec (%Lu MiB in %lu msecs)\n", kb_sent / msecs, kb_sent, msecs);
53 printf("avg put: %Lu\n", kb_sent / iters);
56 int main(int argc, char *argv[])
58 struct sockaddr_in addr;
60 int fd, pfd[2], ffd, ret;
64 printf("%s: file target port\n", argv[0]);
70 memset(&addr, 0, sizeof(addr));
71 addr.sin_family = AF_INET;
72 addr.sin_port = htons(port);
74 if (inet_aton(argv[2], &addr.sin_addr) != 1) {
75 struct hostent *hent = gethostbyname(argv[2]);
78 return error("gethostbyname");
80 memcpy(&addr.sin_addr, hent->h_addr, 4);
83 printf("Connecting to %s/%d\n", argv[2], port);
85 fd = socket(AF_INET, SOCK_STREAM, 0);
87 return error("socket");
89 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
90 return error("connect");
95 ffd = open(argv[1], O_RDWR);
97 return error("open input");
99 signal(SIGINT, show_rate);
100 gettimeofday(&start_time, NULL);
103 ret = splice(ffd, NULL, pfd[1], NULL, SPLICE_SIZE, SPLICE_F_NONBLOCK);
106 printf("spliced %d\n", ret);
109 if (errno == EAGAIN) {
113 return error("splice");
117 kb_sent += ret >> 10;
121 int written = splice(pfd[0], NULL, fd, NULL, ret, flags);
123 return error("splice-out");
128 } while (kb_sent < 512 * 1024 && bla);