From b5fd47dcb6af4b842455c2f6fb2b0fc58704f08c Mon Sep 17 00:00:00 2001 From: TudorCazangiu Date: Sat, 2 Jun 2012 13:26:04 +0300 Subject: [PATCH] implement sendfile client --- sendfile/client/Makefile | 7 +++ sendfile/client/client.c | 93 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 sendfile/client/Makefile diff --git a/sendfile/client/Makefile b/sendfile/client/Makefile new file mode 100644 index 0000000..8d5c7ad --- /dev/null +++ b/sendfile/client/Makefile @@ -0,0 +1,7 @@ +FILE=client + +$(FILE):$(FILE).o + +.PHONY: clean +clean: + -rm -rf $(FILE) $(FILE).o diff --git a/sendfile/client/client.c b/sendfile/client/client.c index fc5fa4c..1e5b59e 100644 --- a/sendfile/client/client.c +++ b/sendfile/client/client.c @@ -1,5 +1,96 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_NUMBER_OF_PEERS 256 +#define CHUNK_SIZE 256 int main(int argc, char **argv) { - + int sock, err; + struct sockaddr_in psin; + struct hostent *host; + char *file_name; + int fd[MAX_NUMBER_OF_PEERS]; + int peers[MAX_NUMBER_OF_PEERS]; + int i = 0, total, sent_size; + + if (argc < 4 || argc % 2) { + printf("Usage: %s " + " ...\n", argv[0]); + return -1; + } + + printf("Create sendfile socket\n"); + + sock = socket(AF_INET, SOCK_DGRAM, 0); + printf("Socket returned: %d\n", sock); + + if (sock < 0) { + printf("Cannot create sendfile socket: %s(%d)\n", + strerror(errno), errno); + return -1; + } + printf("Succesfully created sendfile socket\n"); + + argv++; + file_name = argv[0]; + argv++; + + while(*argv) { + printf("Create sendfile socket\n"); + + sock = socket(AF_INET, SOCK_DGRAM, 0); + printf("Socket returned: %d\n", sock); + + if (sock < 0) { + printf("Cannot create sendfile socket: %s(%d)\n", + strerror(errno), errno); + return -1; + } + printf("Succesfully created sendfile socket\n"); + + printf("Connecting to: %s:%s\n", argv[0], argv[1]); + host = gethostbyname(argv[0]); + psin.sin_family = AF_INET; + psin.sin_port = htons(atoi(argv[1])); + psin.sin_addr = *((struct in_addr*)host->h_addr); + + if (connect(sock, (struct sockaddr *)&psin, + sizeof(struct sockaddr_in)) == -1) { + printf("Cannot connect to server: %s(%d)\n", + strerror(errno), errno); + continue; + } + printf("Connected to: %s:%s\n", argv[0], argv[1]); + peers[i] = sock; + fd[i] = open(file_name, O_RDONLY); + if (fd[i++] < 0) { + printf("Opening file error: %s(%d)\n", strerror(errno), errno); + i--; + } + } + + total = i; + + do { + sent_size = 0; + + for (i = 0; i < total; i++) { + if ((err = sendfile(fd[i], peers[i], NULL, CHUNK_SIZE)) < 0) { + printf("Sending file error: %s(%d)\n", strerror(errno), errno); + } else { + sent_size += err; + } + } + } while(sent_size); + + return 0; } -- 2.20.1