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