-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
400 lines (358 loc) · 9.14 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* FTP Client
* Authors: Janosevic Goran, Kumbeiz Alexander
* System: Linux Debian x86
* Date: 2011-01-11
* Usage: client IP-ADDRESS PORT
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#define BUF 2048
// State of commands
#define QUIT 0
#define LIST 1
#define GET 2
#define SEND 3
#define BLOCKLIST 4
int main(int argc, char **argv) {
int sockFd;
char buffer[BUF];
struct sockaddr_in address;
int size;
long port;
char *filename = NULL;
char fn[1024];
if( argc < 3 ) {
fprintf(stderr, "Usage: %s IP-Adresse Port-Nummer\n", argv[0]);
exit(EXIT_FAILURE);
}
if((sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket error");
return EXIT_FAILURE;
}
port = strtol(argv[2], NULL, 10);
memset(&address,0,sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_aton(argv[1], &address.sin_addr);
if(connect( sockFd,(struct sockaddr *) &address, sizeof(address)) == 0) {
printf("Connection with server(%s) established\n", inet_ntoa(address.sin_addr));
//printf("recv Welcome Message\n");
size=recv(sockFd,buffer,BUF-1, 0);
if(size > 0) {
buffer[size]= '\0';
printf("%s",buffer);
}
}
else {
perror("Connect error - no server available");
return EXIT_FAILURE;
}
// get username
printf("Username: ");
fgets(buffer, BUF-1, stdin);
if(strlen(buffer) <= 1) {
strcpy(buffer, "null");
// send pseudo username and password
send(sockFd, buffer, strlen(buffer), 0);
send(sockFd, buffer, strlen(buffer), 0);
close(sockFd);
fprintf(stderr, "User invalid\n");
return EXIT_FAILURE;
}
// send username
send(sockFd, buffer, strlen(buffer), 0);
// get password
char *pwd;
pwd=getpass("Password: ");
strcpy(buffer, pwd);
if(strlen(buffer) < 1) {
strcpy(buffer, "null");
// send pseudo password
send(sockFd, buffer, strlen(buffer), 0);
close(sockFd);
fprintf(stderr, "User or password invalid\n");
return EXIT_FAILURE;
}
// send password
send(sockFd, buffer, strlen(buffer), 0);
// recv login message
size = recv(sockFd, buffer, BUF-1, 0);
if(size > 0) {
buffer[size]= '\0';
printf("%s\n", buffer);
}
// status code of command
int status = -1;
if(strcmp(buffer, "Username or password invalid\n") == 0) {
status = QUIT;
} else if(strcmp(buffer, "User is blocked!\n") == 0) {
status = QUIT;
}
while(status != QUIT) {
do {
// get command
printf("Send command: ");
fgets(buffer, BUF-1, stdin);
// get command status
if(strncmp(buffer, "list", 4) == 0) {
status = LIST;
} else if(strncmp(buffer, "get", 3) == 0) {
status = GET;
// parse filename
char temp[BUF] = "";
if(strlen(buffer)-3 > 0)
// remove 'get'
strncpy(temp, (buffer+3), strlen(buffer)-3);
else
continue;
filename = strtok(temp, " \n\r");
// if nothing was after 'get' aks again for command
if(filename == NULL) {
status = -1;
continue;
}
strcpy(fn, filename);
} else if(strncmp(buffer, "send", 4) == 0) {
status = SEND;
// parse filename
char temp[BUF] = "";
if(strlen(buffer)-4 > 0)
// remove 'send'
strncpy(temp, (buffer+4), strlen(buffer)-4);
else
continue;
filename = strtok(temp, " \n\r");
// if nothing was after 'send' aks again for command
if(filename == NULL) {
status = -1;
continue;
}
strcpy(fn, filename);
} else if(strncmp(buffer, "blocklist", 9) == 0) {
status = BLOCKLIST;
} else if(strncmp(buffer, "quit", 4) == 0) {
// quit client and close connection
status = QUIT;
} else {
status = -1;
}
} while(status == -1);
// send command
send(sockFd, buffer, strlen(buffer), 0);
// recv packagesize
char bufferPackages[BUF];
size=recv(sockFd,bufferPackages,BUF-1, 0);
if(size > 0) {
bufferPackages[size] = '\0';
}
long packages;
// convert string to long
packages = strtol(bufferPackages, NULL, 10);
if (packages == -1) {
if (status == GET) {
printf("File not found!\n");
}
continue;
}
// list
if (status == LIST) {
// send confirmation
send(sockFd,buffer, 1, 0);
size = 0;
int i;
// recv until whole list was received
for (i=0; i < packages; i+=size) {
// recv filenames
size=recv(sockFd, buffer, BUF-1, 0);
if((size) > 0) {
// print file list
buffer[size]= '\0';
printf("%s",buffer);
fflush(stdout);
} else if (size == 0) {
printf("connect to socket failed\n");
break;
} else if (size == -1) {
printf("error in socket\n");
continue;
}
}
printf("\n");
}
// get
else if (status == GET) {
int isConfirmed = -1;
printf("Size of selected File is %ld bytes\nDo you want to download the file? (y/n)",packages );
// confirm download
do {
fgets(buffer, BUF-1, stdin);
if (strncmp(buffer, "y", 1) == 0) {
// send confirm yes
strcpy(buffer, "y");
send(sockFd, buffer, strlen(buffer), 0);
isConfirmed = 1;
} else if (strncmp(buffer, "n", 1) == 0) {
// send confirm no
strcpy(buffer, "n");
send(sockFd, buffer, strlen(buffer), 0);
isConfirmed = 0;
} else {
// try again
isConfirmed = -1;
printf("Invalid input. (y/n) ");
}
} while(isConfirmed == -1);
// if client sends y
if(isConfirmed) {
// get File
int leftBytes;
FILE *file = NULL;
// save file in current directory where client was started
char dirfile[1024] = "./";
strcat(dirfile, fn);
// progress status
int progress = 0;
int percent = 0;
int diff = 0;
size = 0;
// open file in write-binary mode
file = fopen(dirfile, "wb");
// until file is full transmitted
for(leftBytes = packages; leftBytes > 0; leftBytes -= size) {
char tempBuffer[BUF] = "";
// recv part of file an bytes
size = recv(sockFd, tempBuffer,BUF-1, 0);
if (size > 0) {
// write bytes in file
fwrite(tempBuffer, 1, size, file);
} else if (size == 0) {
printf("connect to socket failed\n");
break;
} else if (size == -1) {
printf("error in socket\n");
break;
}
// Progress bar
percent = (double)(packages-leftBytes+size)/(double)packages*20;
diff = percent-progress;
if(diff >= 0) {
int i;
for(i = 0; i < diff; i++) {
printf("#");
fflush(stdout);
progress++;
}
if(progress == 20) {
printf("!\n");
}
}
}
printf("\n");
fclose(file);
}
// SEND
} else if (status == SEND) {
int leftBytes;
char sendBuffer[BUF];
char receiveBuffer[BUF];
FILE *file = NULL;
struct stat attribut;
unsigned long sizeOfFile = 0;
// save file in current directory where client was started
char dirfile[1024] = "./";
strcat(dirfile, fn);
// get file attributes
if(stat(dirfile, &attribut) == -1)
{
// send -1 as error
sprintf(sendBuffer, "%ld", (long)-1);
send(sockFd, sendBuffer, strlen(sendBuffer), 0);
printf("File not found!\n");
}
else {
// get size of file
sizeOfFile = attribut.st_size;
sprintf(sendBuffer, "%ld", sizeOfFile);
// send size of file
send(sockFd, sendBuffer, strlen(sendBuffer), 0);
// recv confirmation to send file
size = recv(sockFd, receiveBuffer, 1, 0);
// open file in read-binary mode
file = fopen(dirfile, "rb");
if (file != NULL) {
// size of part of file to send
int newBUF = BUF-1;
int size;
// progress status
int progress = 0;
int percent = 0;
int diff = 0;
// send file parts until whole file is sent
for(leftBytes = sizeOfFile; leftBytes > 0; leftBytes -= (BUF-1)) {
char tempBuffer[BUF];
// set new buffer size for the last part of the file
if(leftBytes < (BUF-1)) {
newBUF = leftBytes % (BUF-1);
}
// read bytes of file
size = fread(tempBuffer, newBUF, 1, file);
// send bytes of file
send(sockFd, tempBuffer, newBUF, 0);
// Progress bar
percent = (double)(sizeOfFile-leftBytes+newBUF)/(double)sizeOfFile*20;
diff = percent-progress;
if(diff >= 0) {
int i;
for(i = 0; i < diff; i++) {
printf("#");
fflush(stdout);
progress++;
}
if(progress == 20) {
printf("!\n");
}
}
}
printf("\n");
fclose(file);
} else {
printf("can't open file\n");
}
}
} else if(status == BLOCKLIST) {
// send confirmation
send(sockFd,buffer, 1, 0);
size = 0;
int i;
// recv until whole list was received
for (i=0; i < packages; i+=size) {
// recv blocklist
size=recv(sockFd, buffer, BUF-1, 0);
if((size) > 0) {
// print blocklist
buffer[size]= '\0';
printf("%s",buffer);
fflush(stdout);
} else if (size == 0) {
printf("connect to socket failed\n");
break;
} else if (size == -1) {
printf("error in socket\n");
continue;
}
}
printf("\n");
}
}
close(sockFd);
return EXIT_SUCCESS;
}