#include <sys/sendfile.h>
#include <sys/socket.h>
#include <netinet/in.h>
+#include <sys/time.h>
#include <fcntl.h>
#include <string.h>
+#include <unistd.h>
#include <errno.h>
#include <netdb.h>
+#include "../utils/utils.h"
+
#define MAX_NUMBER_OF_PEERS 256
-#define CHUNK_SIZE 256
int main(int argc, char **argv) {
int sock, err;
int fd[MAX_NUMBER_OF_PEERS];
int peers[MAX_NUMBER_OF_PEERS];
int i = 0, total, sent_size;
+ struct timeval start;
+ struct timeval stop;
if (argc < 4 || argc % 2) {
printf("Usage: %s <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
printf("Opening file error: %s(%d)\n", strerror(errno), errno);
i--;
}
+ argv += 2;
}
total = i;
+ gettimeofday(&start, NULL);
do {
sent_size = 0;
for (i = 0; i < total; i++) {
- if ((err = sendfile(fd[i], peers[i], NULL, CHUNK_SIZE)) < 0) {
+ if ((err = sendfile(peers[i], fd[i], NULL, CHUNK_SIZE)) < 0) {
printf("Sending file error: %s(%d)\n", strerror(errno), errno);
- } else {
- sent_size += err;
+ continue;
}
+ sent_size += err;
+ usleep(20);
}
} while(sent_size);
+ gettimeofday(&stop, NULL);
+
+ printf("Successfully written %s in %ld seconds\n", file_name,
+ stop.tv_sec - start.tv_sec);
return 0;
}