sendfile: added server implementation
[p2p-kernel-protocol.git] / sendfile / server / server.c
index 8bc0ff0..603a1c0 100644 (file)
@@ -2,21 +2,22 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <netdb.h>
 #include <stdio.h>
 
-#define BUFFER_SIZE            256
+#include "../utils/utils.h"
 
 int main(int argc, char **argv)
 {
-       int sock, err, file_fd;
+       int sock, file_fd;
        ssize_t ret, len, offset, bytes_to_write;
        struct sockaddr_in psin;
        struct hostent *host;
-       char buffer[BUFFER_SIZE];
+       char buffer[CHUNK_SIZE];
 
        if (argc != 4) {
                fprintf(stderr, "Usage: %s <peer_ip> <peer_port> <file_name>\n", argv[0]);
@@ -57,11 +58,11 @@ int main(int argc, char **argv)
        printf("Bound to: %s:%s\n", argv[1], argv[2]);
        
        for (;;) {
-               ret = read(sock, buffer, BUFFER_SIZE);
+               ret = read(sock, buffer, CHUNK_SIZE);
                if (ret < 0) {
                        printf("Error while receiving file %s: %s(%d)\n", argv[0],
                                        strerror(errno), errno);
-                       return err;
+                       return ret;
                } else if (ret == 0) {
                        break;
                }
@@ -76,12 +77,10 @@ int main(int argc, char **argv)
                        }
                        bytes_to_write -= len;
                        offset += len;
-               } while (bytes_to_write > 0);
-               if (ret < BUFFER_SIZE)
-                       break;
+               } while (bytes_to_write);
        }
 
-       printf("Successfully read file %s\n", argv[0]);
+       printf("Successfully read file %s\n", argv[3]);
 
        return 0;
 }