implement sendfile client
authorTudorCazangiu <tudor.cazangiu@gmail.com>
Sat, 2 Jun 2012 10:26:04 +0000 (13:26 +0300)
committerTudorCazangiu <tudor.cazangiu@gmail.com>
Sat, 2 Jun 2012 10:27:00 +0000 (13:27 +0300)
sendfile/client/Makefile [new file with mode: 0644]
sendfile/client/client.c

diff --git a/sendfile/client/Makefile b/sendfile/client/Makefile
new file mode 100644 (file)
index 0000000..8d5c7ad
--- /dev/null
@@ -0,0 +1,7 @@
+FILE=client
+
+$(FILE):$(FILE).o
+
+.PHONY: clean
+clean:
+       -rm -rf $(FILE) $(FILE).o
index fc5fa4c..1e5b59e 100644 (file)
@@ -1,5 +1,96 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sendfile.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <netdb.h>
+
+#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 <file_name> <peer_ip1> <peer_port1> <peer_ip2>"
+                               " <peer_port2> ...\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;
 }