test: moved testing script
[p2p-kernel-protocol.git] / test / server / server.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <netinet/in.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <netdb.h>
8
9 #include "p2pkp.h"
10
11 int main(int argc, char **argv)
12 {
13         int sock, err;
14         struct sockaddr_in psin;
15         struct hostent *host;
16
17         if (argc != 4) {
18                 fprintf(stderr, "Usage: %s <peer_ip> <peer_port> <file_name>\n", argv[0]);
19                 return -1;
20         }
21
22         printf("Opening AF_P2PKP socket\n");
23
24         sock = socket(AF_P2PKP, SOCK_DGRAM, 0);
25         printf("Socket returned: %d\n", sock);
26
27         if (sock < 0) {
28                 printf("Cannot create AF_P2PKP socket: %s(%d)\n",
29                                 strerror(errno), errno);
30                 return -1;
31         }
32         printf("Successfully created AF_P2PKP socket: %d\n", sock);
33
34         argv++;
35
36         host = gethostbyname(argv[0]);
37         psin.sin_family = AF_INET;
38         psin.sin_port = htons(atoi(argv[1]));
39         psin.sin_addr = *((struct in_addr *)host->h_addr);
40
41         if (bind(sock, (struct sockaddr *)&psin,
42                                 sizeof(struct sockaddr_in)) == -1) {
43                 printf("Cannot bind to server: %s(%d)\n", strerror(errno), errno);
44                 return -1;
45         }
46
47         printf("Bound to: %s:%s\n", argv[0], argv[1]);
48         argv += 2;
49         
50         err = read(sock, argv[0], sizeof(argv[0]));
51         if (err < 0) {
52                 printf("Error while receiving file %s: %s(%d)\n", argv[0],
53                                 strerror(errno), errno);
54                 return err;
55         }
56
57         printf("Successfully read file %s\n", argv[0]);
58
59         return 0;
60 }