4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 int main(int argc, char **argv)
15 struct sockaddr_in psin;
22 if (argc % 2 || argc < 3) {
23 fprintf(stderr, "Usage: %s <file> <peer_ip1> <peer_port1> "
24 "[<pper_ip2> <peer_port2> ...]\n", argv[0]);
28 printf("Opening AF_P2PKP socket\n");
30 sock = socket(AF_P2PKP, SOCK_DGRAM, 0);
31 printf("Socket returned: %d\n", sock);
34 printf("Cannot create AF_P2PKP socket: %s(%d)\n",
35 strerror(errno), errno);
38 printf("Successfully created AF_P2PKP socket: %d\n", sock);
40 filename_len = strlen(filename);
44 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
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);
51 if (connect(sock, (struct sockaddr *)&psin,
52 sizeof(struct sockaddr_in)) == -1)
54 printf("Cannot connect to server: %s(%d)\n", strerror(errno), errno);
57 printf("Connected to: %s:%s\n", argv[0], argv[1]);
61 printf("Starting to write on the socket\n");
63 gettimeofday(&start, NULL);
64 err = write(sock, filename, filename_len - 1);
66 printf("Error while sending file %s: %s(%d)\n", filename,
67 strerror(errno), errno);
70 gettimeofday(&stop, NULL);
72 printf("Successfully written %s in %ld seconds\n", filename,
73 stop.tv_sec - start.tv_sec);