sendfile: added timer for client
[p2p-kernel-protocol.git] / sendfile / client / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/sendfile.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <sys/time.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <netdb.h>
14
15 #include "../utils/utils.h"
16
17 #define MAX_NUMBER_OF_PEERS             256
18
19 int main(int argc, char **argv) {
20         int sock, err;
21         struct sockaddr_in psin;
22         struct hostent *host;
23         char *file_name;
24         int fd[MAX_NUMBER_OF_PEERS];
25         int peers[MAX_NUMBER_OF_PEERS];
26         int i = 0, total, sent_size;
27         struct timeval start;
28         struct timeval stop;
29
30         if (argc < 4 || argc % 2) {
31                 printf("Usage: %s <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
32                                 " <peer_port2> ...\n", argv[0]);
33                 return -1;
34         }
35
36         printf("Create sendfile socket\n");
37
38         sock = socket(AF_INET, SOCK_DGRAM, 0);
39         printf("Socket returned: %d\n", sock);
40
41         if (sock < 0) {
42                 printf("Cannot create sendfile socket: %s(%d)\n", 
43                                 strerror(errno), errno);
44                 return -1;
45         }
46         printf("Succesfully created sendfile socket\n");
47
48         argv++;
49         file_name = argv[0];
50         argv++;
51
52         while(*argv) {
53                 printf("Create sendfile socket\n");
54
55                 sock = socket(AF_INET, SOCK_DGRAM, 0);
56                   printf("Socket returned: %d\n", sock);
57
58                 if (sock < 0) {
59                         printf("Cannot create sendfile socket: %s(%d)\n", 
60                                         strerror(errno), errno);
61                         return -1;
62                 }
63                 printf("Succesfully created sendfile socket\n");
64
65                 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
66                 host = gethostbyname(argv[0]);
67                 psin.sin_family = AF_INET;
68                 psin.sin_port = htons(atoi(argv[1]));
69                 psin.sin_addr = *((struct in_addr*)host->h_addr);
70
71                 if (connect(sock, (struct sockaddr *)&psin, 
72                                         sizeof(struct sockaddr_in)) == -1) {
73                         printf("Cannot connect to server: %s(%d)\n", 
74                                         strerror(errno), errno);
75                         continue;
76                 }
77                 printf("Connected to: %s:%s\n", argv[0], argv[1]);
78                 peers[i] = sock;
79                 fd[i] = open(file_name, O_RDONLY);
80                 if (fd[i++] < 0) {
81                         printf("Opening file error: %s(%d)\n", strerror(errno), errno);
82                         i--;
83                 }
84                 argv += 2;
85         }
86
87         total = i;
88
89         gettimeofday(&start, NULL);
90         do {
91                 sent_size = 0;
92
93                 for (i = 0; i < total; i++) {
94                         if ((err = sendfile(peers[i], fd[i], NULL, CHUNK_SIZE)) < 0) {
95                                 printf("Sending file error: %s(%d)\n", strerror(errno), errno);
96                                 continue;
97                         }
98                         sent_size += err;
99                         usleep(20);
100                 }
101         } while(sent_size);
102         gettimeofday(&stop, NULL);
103
104         printf("Successfully written %s in %ld seconds\n", file_name, 
105                         stop.tv_sec - start.tv_sec);
106
107         return 0;
108 }