5 #include <sys/sendfile.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
13 #define MAX_NUMBER_OF_PEERS 256
14 #define CHUNK_SIZE 256
16 int main(int argc, char **argv) {
18 struct sockaddr_in psin;
21 int fd[MAX_NUMBER_OF_PEERS];
22 int peers[MAX_NUMBER_OF_PEERS];
23 int i = 0, total, sent_size;
25 if (argc < 4 || argc % 2) {
26 printf("Usage: %s <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
27 " <peer_port2> ...\n", argv[0]);
31 printf("Create sendfile socket\n");
33 sock = socket(AF_INET, SOCK_DGRAM, 0);
34 printf("Socket returned: %d\n", sock);
37 printf("Cannot create sendfile socket: %s(%d)\n",
38 strerror(errno), errno);
41 printf("Succesfully created sendfile socket\n");
48 printf("Create sendfile socket\n");
50 sock = socket(AF_INET, SOCK_DGRAM, 0);
51 printf("Socket returned: %d\n", sock);
54 printf("Cannot create sendfile socket: %s(%d)\n",
55 strerror(errno), errno);
58 printf("Succesfully created sendfile socket\n");
60 printf("Connecting to: %s:%s\n", argv[0], argv[1]);
61 host = gethostbyname(argv[0]);
62 psin.sin_family = AF_INET;
63 psin.sin_port = htons(atoi(argv[1]));
64 psin.sin_addr = *((struct in_addr*)host->h_addr);
66 if (connect(sock, (struct sockaddr *)&psin,
67 sizeof(struct sockaddr_in)) == -1) {
68 printf("Cannot connect to server: %s(%d)\n",
69 strerror(errno), errno);
72 printf("Connected to: %s:%s\n", argv[0], argv[1]);
74 fd[i] = open(file_name, O_RDONLY);
76 printf("Opening file error: %s(%d)\n", strerror(errno), errno);
86 for (i = 0; i < total; i++) {
87 if ((err = sendfile(fd[i], peers[i], NULL, CHUNK_SIZE)) < 0) {
88 printf("Sending file error: %s(%d)\n", strerror(errno), errno);