5 #include <sys/sendfile.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
15 #include "../utils/utils.h"
17 #define MAX_NUMBER_OF_PEERS 256
19 int main(int argc, char **argv) {
21 struct sockaddr_in psin;
24 int fd[MAX_NUMBER_OF_PEERS];
25 int peers[MAX_NUMBER_OF_PEERS];
26 int i = 0, total, sent_size;
30 if (argc < 4 || argc % 2) {
31 printf("Usage: %s <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
32 " <peer_port2> ...\n", argv[0]);
36 printf("Create sendfile socket\n");
38 sock = socket(AF_INET, SOCK_DGRAM, 0);
39 printf("Socket returned: %d\n", sock);
42 printf("Cannot create sendfile socket: %s(%d)\n",
43 strerror(errno), errno);
46 printf("Succesfully created sendfile socket\n");
53 printf("Create sendfile socket\n");
55 sock = socket(AF_INET, SOCK_DGRAM, 0);
56 printf("Socket returned: %d\n", sock);
59 printf("Cannot create sendfile socket: %s(%d)\n",
60 strerror(errno), errno);
63 printf("Succesfully created sendfile socket\n");
65 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
66 host = gethostbyname(argv[0]);
67 psin.sin_family = AF_INET;
68 psin.sin_port = htons(atoi(argv[1]));
69 psin.sin_addr = *((struct in_addr*)host->h_addr);
71 if (connect(sock, (struct sockaddr *)&psin,
72 sizeof(struct sockaddr_in)) == -1) {
73 printf("Cannot connect to server: %s(%d)\n",
74 strerror(errno), errno);
77 printf("Connected to: %s:%s\n", argv[0], argv[1]);
79 fd[i] = open(file_name, O_RDONLY);
81 printf("Opening file error: %s(%d)\n", strerror(errno), errno);
89 gettimeofday(&start, NULL);
93 for (i = 0; i < total; i++) {
94 if ((err = sendfile(peers[i], fd[i], NULL, CHUNK_SIZE)) < 0) {
95 printf("Sending file error: %s(%d)\n", strerror(errno), errno);
102 gettimeofday(&stop, NULL);
104 printf("Successfully written %s in %ld seconds\n", file_name,
105 stop.tv_sec - start.tv_sec);