5 #include <sys/sendfile.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 #include "../utils/utils.h"
15 #define MAX_NUMBER_OF_PEERS 256
17 int main(int argc, char **argv) {
19 struct sockaddr_in psin;
22 int fd[MAX_NUMBER_OF_PEERS];
23 int peers[MAX_NUMBER_OF_PEERS];
24 int i = 0, total, sent_size;
26 if (argc < 4 || argc % 2) {
27 printf("Usage: %s <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
28 " <peer_port2> ...\n", argv[0]);
32 printf("Create sendfile socket\n");
34 sock = socket(AF_INET, SOCK_DGRAM, 0);
35 printf("Socket returned: %d\n", sock);
38 printf("Cannot create sendfile socket: %s(%d)\n",
39 strerror(errno), errno);
42 printf("Succesfully created sendfile socket\n");
49 printf("Create sendfile socket\n");
51 sock = socket(AF_INET, SOCK_DGRAM, 0);
52 printf("Socket returned: %d\n", sock);
55 printf("Cannot create sendfile socket: %s(%d)\n",
56 strerror(errno), errno);
59 printf("Succesfully created sendfile socket\n");
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);
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);
73 printf("Connected to: %s:%s\n", argv[0], argv[1]);
75 fd[i] = open(file_name, O_RDONLY);
77 printf("Opening file error: %s(%d)\n", strerror(errno), errno);
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);