test:server:create userspace peer for receiving file
[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 #define TEST_FILE "/root/Dropbox/school/p2pkp/test/client/test_file"
13
14 int main(int argc, char **argv)
15 {
16         int sock, err;
17         struct sockaddr_in psin;
18         struct hostent *host;
19
20         if (argc % 2 == 0 || argc < 2) {
21                 fprintf(stderr, "Usage: %s <peer_ip1> <peer_port1> "
22                                 "<pper_ip2> <peer_port2> ...\n", argv[0]);
23                 return -1;
24         }
25
26         printf("Opening AF_P2PKP socket\n");
27
28         sock = socket(AF_P2PKP, SOCK_DGRAM, 0);
29         printf("Socket returned: %d\n", sock);
30
31         if (sock < 0) {
32                 printf("Cannot create AF_P2PKP socket: %s(%d)\n",
33                                 strerror(errno), errno);
34                 return -1;
35         }
36         printf("Successfully created AF_P2PKP socket: %d\n", sock);
37
38         argv++;
39         while (*argv) {
40                 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
41
42                 host = gethostbyname(argv[0]);
43                 psin.sin_family = AF_INET;
44                 psin.sin_port = htons(atoi(argv[1]));
45                 psin.sin_addr = *((struct in_addr *)host->h_addr);
46
47                 if (connect(sock, (struct sockaddr *)&psin,
48                                         sizeof(struct sockaddr_in)) == -1) 
49                 {
50                         printf("Cannot connect to server: %s(%d)\n", strerror(errno), errno);
51                         return -1;
52                 }
53                 printf("Connected to: %s:%s\n", argv[0], argv[1]);
54                 argv+=2;
55         }
56
57         printf("Starting to write on the socket\n");
58
59         err = write(sock, TEST_FILE, sizeof(TEST_FILE) - 1);
60         if (err < 0) {
61                 printf("Error while sending file %s: %s(%d)\n", TEST_FILE,
62                                 strerror(errno), errno);
63                 return err;
64         }
65
66         printf("Successfully written file\n");
67
68         return 0;
69 }