module: fixed receiving size
[p2p-kernel-protocol.git] / test / client / client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <netdb.h>
9
10 #include "p2pkp.h"
11
12 int main(int argc, char **argv)
13 {
14         int sock, err;
15         struct sockaddr_in psin;
16         struct hostent *host;
17         char *filename;
18         int filename_len;
19         struct timeval start;
20         struct timeval stop;
21
22         if (argc % 2 || argc < 3) {
23                 fprintf(stderr, "Usage: %s <file> <peer_ip1> <peer_port1> "
24                                 "[<pper_ip2> <peer_port2> ...]\n", argv[0]);
25                 return -1;
26         }
27
28         printf("Opening AF_P2PKP socket\n");
29
30         sock = socket(AF_P2PKP, SOCK_DGRAM, 0);
31         printf("Socket returned: %d\n", sock);
32
33         if (sock < 0) {
34                 printf("Cannot create AF_P2PKP socket: %s(%d)\n",
35                                 strerror(errno), errno);
36                 return -1;
37         }
38         printf("Successfully created AF_P2PKP socket: %d\n", sock);
39         filename = argv[1];
40         filename_len = strlen(filename);
41
42         argv+=2;
43         while (*argv) {
44                 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
45
46                 host = gethostbyname(argv[0]);
47                 psin.sin_family = AF_INET;
48                 psin.sin_port = htons(atoi(argv[1]));
49                 psin.sin_addr = *((struct in_addr *)host->h_addr);
50
51                 if (connect(sock, (struct sockaddr *)&psin,
52                                         sizeof(struct sockaddr_in)) == -1) 
53                 {
54                         printf("Cannot connect to server: %s(%d)\n", strerror(errno), errno);
55                         return -1;
56                 }
57                 printf("Connected to: %s:%s\n", argv[0], argv[1]);
58                 argv+=2;
59         }
60
61         printf("Starting to write on the socket\n");
62
63         gettimeofday(&start, NULL);
64         err = write(sock, filename, filename_len - 1);
65         if (err < 0) {
66                 printf("Error while sending file %s: %s(%d)\n", filename,
67                                 strerror(errno), errno);
68                 return err;
69         }
70         gettimeofday(&stop, NULL);
71
72         printf("Successfully written %s in %ld seconds\n", filename, 
73                         stop.tv_sec - start.tv_sec);
74
75         return 0;
76 }