· 9 years ago · Oct 14, 2016, 01:38 PM
1/*fileclient.c*/
2//#define _DEFAULT_SOURCE
3
4#include <signal.h>
5#include <sys/wait.h>
6#include <stdio.h>
7#include <fcntl.h>
8#include <sys/stat.h>
9#include <sys/types.h>
10#include <unistd.h>
11#include <string.h>
12#include <stdlib.h>
13#include <netdb.h>
14#include <errno.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <sys/time.h>
19
20#define MAX_BUF 1024
21
22int main(int argc, char * argv[]) {
23
24 FILE *fp;
25 int fd;
26 struct hostent *hp;
27 struct sockaddr_in sin;
28 char *host, *secretkey, *srv_prtnum, *filename, *config_fname;
29 char request[MAX_BUF], temp[MAX_BUF];
30 int s, n = 0;
31 int len, block_size;
32 long bytes_read = 0;
33
34 struct timeval tv1, tv2;
35 long time_elapsed = 0;
36
37 if (argc == 6) {
38 host = argv[1];
39 srv_prtnum = argv[2];
40 secretkey = argv[3];
41 filename = argv[4];
42 config_fname = argv[5];
43 } else {
44 printf(
45 "usage: $fileclient hostname portnumber secretkey filename configfile.dat\n");
46 exit(1);
47 }
48
49 //reading blocksize
50 fp = fopen(config_fname, "r");
51 //fscanf stops scanning at whitespace
52 fscanf(fp, "%d", &block_size);
53 fclose(fp);
54 char recvbuf[block_size];
55
56 /* translate host name into peer's IP address */
57 hp = gethostbyname(host);
58 if (!hp) {
59 printf("Error: unknown host: %s\n", host);
60 exit(1);
61 }
62
63 /* build address data structure */
64 memset((char *) &sin, 0, sizeof(sin));
65 sin.sin_family = AF_INET;
66 memcpy(hp->h_addr_list[0], (char *) &sin.sin_addr, hp->h_length);
67 sin.sin_port = htons(atoi(srv_prtnum));
68
69 /* active open */
70 //The call to the function socket() creates an UN-named socket inside the kernel
71 //and returns an integer known as socket descriptor.
72 if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
73 perror("Error: socket");
74 exit(1);
75 }
76
77 //socklen_t soc_len = sizeof sin;
78 //The call to connect this socket s with the socket of the
79 //remote host(the populated address in struct sin)
80 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
81 perror("Error: connect");
82 close(s);
83 exit(1);
84 }
85
86 //Connection established
87 //main part of the program
88 //constructing the request
89 sprintf(request, "$%s$%s", secretkey, filename);//appends null character on its own
90
91 //sending to server
92 //snprintf(buf, sizeof(buf), "%s", cmd);
93 write(s, request, strlen(request));
94
95 //Read response and save it in ./filename
96 if (access(filename, F_OK) != -1) {
97 // file exists
98 printf("Error: File already exists\n");
99 exit(1);
100
101 } else {
102 // file doesn't exist, hence create it
103 //fd = open(filename, O_RDWR | O_CREAT | O_APPEND, O_RDWR, S_IRUSR | S_IWUSR |S_IXUSR | S_IRGRP | S_IWGRP |S_IXGRP| S_IXOTH| S_IWOTH | S_IROTH);
104 fd = open(filename, O_RDWR| O_APPEND | O_CREAT, S_IRUSR | S_IWUSR |S_IXUSR);
105 //noting starting time
106 if (gettimeofday(&tv1, NULL) == -1) {
107 perror("Error: gettimeofday\n");
108 exit(1);
109 }
110
111 while ((n = read(s, recvbuf, block_size)) > 0) {
112 bytes_read += n;
113
114 write(fd, recvbuf, block_size);
115 }
116
117 close(fd);
118
119 //noting ending time
120 if (gettimeofday(&tv2, NULL) == -1) {
121 perror("Error: gettimeofday\n");
122 exit(1);
123 }
124
125 //seconds to milliseconds
126 printf("seconds time: %ld\n",(long)(tv2.tv_sec - tv1.tv_sec));
127 time_elapsed = (tv2.tv_sec - tv1.tv_sec) * 1000.0;
128 printf("microseconds time: %ld\n", (long)(tv2.tv_usec - tv1.tv_usec));
129 //micro to milliseconds
130 time_elapsed += (tv2.tv_usec - tv1.tv_usec) / 1000.0;
131
132 printf("Completion time: %ld\n", time_elapsed);
133 printf("Bytes downloaded: %ld\n", bytes_read);
134 printf("Throughput: %f Kbps\n", (float)bytes_read/time_elapsed);
135
136 }
137
138 return (0);
139}