-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
248 lines (218 loc) · 5.5 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
/*
* 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>
#define BUF 2048
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;
}
printf("Username: ");
fgets(buffer, BUF-1, stdin);
//printf("send username\n");
send(sockFd, buffer, strlen(buffer), 0);
char *pwd;
pwd=getpass("Password: ");
strcpy(buffer, pwd);
//printf("send password\n");
send(sockFd, buffer, strlen(buffer), 0);
//printf("recv loginmessage\n");
size = recv(sockFd, buffer, BUF-1, 0);
if(size > 0) {
buffer[size]= '\0';
printf("%s\n", buffer);
}
int status = -1;
if(strcmp(buffer, "Username or password invalid\n") == 0) {
status = 0;
} else if(strcmp(buffer, "User is blocked!\n") == 0) {
status = 0;
}
while(status != 0) {
do {
printf("Send command: ");
fgets(buffer, BUF-1, stdin);
if(strncmp(buffer, "list", 4) == 0) {
status = 1;
} else if(strncmp(buffer, "get", 3) == 0) {
status = 2;
// 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 command again
if(filename == NULL) {
status = -1;
continue;
}
strcpy(fn, filename);
} else if(strncmp(buffer, "quit", 4) == 0) {
// quit client and close connection
status = 0;
} 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 str to long
packages = strtol(bufferPackages, NULL, 10);
if (packages == -1) {
if (status == 2) {
printf("File not found!\n");
}
continue;
}
// list
if (status == 1) {
// send confirm message of packages
// separates packages from filelist
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;
}
}
}
// get
else if (status == 2) {
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");
}
}
}
fclose(file);
}
}
}
close(sockFd);
return EXIT_SUCCESS;
}