603a1c0e5200c916d011ecbf63d2b580e9e86c82
[p2p-kernel-protocol.git] / sendfile / server / server.c
1 #include <netinet/in.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <netdb.h>
10 #include <stdio.h>
11
12 #include "../utils/utils.h"
13
14 int main(int argc, char **argv)
15 {
16         int sock, file_fd;
17         ssize_t ret, len, offset, bytes_to_write;
18         struct sockaddr_in psin;
19         struct hostent *host;
20         char buffer[CHUNK_SIZE];
21
22         if (argc != 4) {
23                 fprintf(stderr, "Usage: %s <peer_ip> <peer_port> <file_name>\n", argv[0]);
24                 return -1;
25         }
26
27         printf("Opening AF_INET socket\n");
28
29         /* first open the file descriptor */
30         file_fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC, 0644);
31         if (file_fd < 0) {
32                 printf("Cannot open file for write\n");
33                 return -1;
34         }
35         printf("Opened file for write %s[%d]\n", argv[3], file_fd);
36
37         sock = socket(AF_INET, SOCK_DGRAM, 0);
38         printf("Socket returned: %d\n", sock);
39
40         if (sock < 0) {
41                 printf("Cannot create AF_INET socket: %s(%d)\n",
42                                 strerror(errno), errno);
43                 return -1;
44         }
45         printf("Successfully created AF_INET socket: %d\n", sock);
46
47         host = gethostbyname(argv[1]);
48         psin.sin_family = AF_INET;
49         psin.sin_port = htons(atoi(argv[2]));
50         psin.sin_addr = *((struct in_addr *)host->h_addr);
51
52         if (bind(sock, (struct sockaddr *)&psin,
53                                 sizeof(struct sockaddr_in)) == -1) {
54                 printf("Cannot bind to server: %s(%d)\n", strerror(errno), errno);
55                 return -1;
56         }
57
58         printf("Bound to: %s:%s\n", argv[1], argv[2]);
59         
60         for (;;) {
61                 ret = read(sock, buffer, CHUNK_SIZE);
62                 if (ret < 0) {
63                         printf("Error while receiving file %s: %s(%d)\n", argv[0],
64                                         strerror(errno), errno);
65                         return ret;
66                 } else if (ret == 0) {
67                         break;
68                 }
69                 bytes_to_write = ret;
70                 offset = 0;
71                 do {
72                         len = write(file_fd, buffer+offset, bytes_to_write);
73                         if (len < 0) {
74                                 printf("Error while receiving file %s: %s(%d)\n", argv[0],
75                                                 strerror(errno), errno);
76                                 return -1;
77                         }
78                         bytes_to_write -= len;
79                         offset += len;
80                 } while (bytes_to_write);
81         }
82
83         printf("Successfully read file %s\n", argv[3]);
84
85         return 0;
86 }