-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
340 lines (281 loc) · 10.6 KB
/
Copy pathclient.cpp
File metadata and controls
340 lines (281 loc) · 10.6 KB
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
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#define BUFFER_SIZE 1024 //At a single time 1 kb of data will be sent
using namespace std;
int main(int argc, char const *argv[])
{
char c_message[BUFFER_SIZE];
string temp;
string file_name;
//This is Client socket File descriptor
int Client_socket_fd = 0;
int Client_recv;
unsigned char B_Mem[BUFFER_SIZE] = {0};
FILE *fp;
int R_bytes=0;
long int to_be_received;
long int network_tbr;
long int bytesToRead;
long int bytesRead=0;
long int readThisTime;
int dl;
char overwrite;
int validity_check=0;
long int res;
long int converter_res;
unsigned char data[BUFFER_SIZE]={0};
int n_bytes;
char system_command[16];
long int bytesSent=0;
long int bytesToSend;
long int SentThisTime=0;
struct sockaddr_in serv_addr;
int i;
int port_num = atoi(argv[2]);
cout<<"Port number client is trying to reach is :"<<port_num<<endl;
if(port_num<1024 || port_num>65535){
cout<<"Invalid port number ,please use port number between 1024 and 65535, exiting ...."<<endl;
return -1;
}
//I am using ipv4 TCP with internet protocol.
Client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if ( Client_socket_fd == -1)
{
cout<<"Socket could not be created"<<endl;
return -2;
}
serv_addr.sin_family = AF_INET; //We need to specify which addresses to listen and talk
serv_addr.sin_port = htons(port_num); // Conversion of endian system
serv_addr.sin_addr.s_addr= INADDR_ANY;
if (connect(Client_socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
cout<<"Cannot connect to server"<<endl;
return -3;
}
bzero(c_message , sizeof(c_message));
bzero(B_Mem , sizeof(B_Mem));
while(1){
bzero(B_Mem , sizeof(B_Mem));
bzero(c_message , sizeof(c_message));
R_bytes=0;
cin.clear();
cin.sync();
//cin.ignore();
cout<<"Please enter the command to the server:";
cin.getline(c_message,sizeof(c_message));
cout<<endl;
if(strcmp(c_message, "LIST" )==0)
{
send(Client_socket_fd , c_message , strlen(c_message) , 0 );
cout<<"Files list received :"<<endl;
while( 1 )
{
read(Client_socket_fd , &network_tbr , sizeof(network_tbr) );
to_be_received = ntohl(network_tbr);
if(to_be_received > 0)
{
R_bytes = read(Client_socket_fd, B_Mem , to_be_received );
cout<<B_Mem;
bzero(B_Mem , sizeof(B_Mem));
}
if(to_be_received <= 0){
if(to_be_received==0){
cout<<endl<<"End of file list"<<endl;
break;
}
if(to_be_received<0){
cout<<"Error in reading the file list"<<endl;
break;
}
}
}
cout<<endl;
continue;
}
if(c_message[0]=='R' && c_message[1]=='E' && c_message[2]=='T' && c_message[3]=='R' && c_message[4]==' ')
{
file_name=&c_message[5];
validity_check=0;
dl = access(file_name.c_str() , F_OK);
if(dl==0)
{
cout<<"The file"<<file_name<<" already exists in this directory , do you wish to overwrite it ? Enter Y/N:";
cin>>overwrite;
cout<<"Entered input is :"<<overwrite<<endl;
if(overwrite=='Y'){
cout<<"File will be downloaded from the server and overwritten"<<endl;
validity_check=1;
}
else if(overwrite=='N'){
cout<<"File in the directory will not be overwritten"<<endl;
validity_check=0;
continue;
}
else{
cout<<"Invalid input , file will not be overwritten"<<endl;
validity_check=0;
continue;
}
}
if(dl==-1){
cout<<"The file"<<file_name<<" does not exists in this directory , and will be downloaded from the server:"<<endl;
validity_check=1;
}
if(validity_check==0){
continue;
}
send(Client_socket_fd , c_message , strlen(c_message) , 0 );
//Check whether file exists at server side
read(Client_socket_fd , B_Mem , 3*sizeof(char));
cout<<B_Mem<<endl;
if(B_Mem[0]=='A' && B_Mem[1]=='C' && B_Mem[2]=='K'){
cout<<"Server has acknowleged the retrival , the file shall now be received."<<endl;
}
else{
cout<<"Server has denied the retrival , the file will not be received."<<endl;
continue;
}
bzero(B_Mem , sizeof(B_Mem));
network_tbr = 0;
read(Client_socket_fd , &network_tbr , sizeof(network_tbr) );
to_be_received = ntohl(network_tbr);
cout<<"Number of bytes to be received is :"<<to_be_received<<endl;
bytesToRead = to_be_received;
bytesRead=0;
readThisTime=0;
fp=fopen( &c_message[5] , "wb");
if(fp==NULL){
cout<<"Error in creating file.";
return 1;
}
while( bytesToRead > bytesRead )
{
if( bytesToRead >= BUFFER_SIZE ){
R_bytes = read(Client_socket_fd , B_Mem , BUFFER_SIZE);
if(R_bytes<0){
cout<<"Read error"<<endl;
continue;
}
bytesRead= bytesRead + R_bytes;
cout<<"Bytes received :"<<R_bytes<<endl;
cout<<"Bytes read till now :"<<bytesRead<<endl;
fwrite(B_Mem , 1 , R_bytes , fp);
bzero(B_Mem , sizeof(B_Mem));
}
if ( bytesRead < BUFFER_SIZE ){
R_bytes = read(Client_socket_fd , B_Mem , bytesToRead - bytesRead);
if(R_bytes<0){
cout<<"Read error"<<endl;
continue;
}
bytesRead= bytesRead + R_bytes;
cout<<"Bytes received :"<<R_bytes<<endl;
cout<<"Bytes read till now :"<<bytesRead<<endl;
fwrite(B_Mem , 1 , R_bytes , fp);
bzero(B_Mem , sizeof(B_Mem));
}
}
cout<<"File downloading complete"<<endl;
fclose(fp);
bzero(B_Mem , sizeof(B_Mem));
bzero(c_message , sizeof(c_message));
R_bytes=0;
cin.sync();
cin.clear();
continue;
}
if(c_message[0]=='Q' && c_message[1]=='U' && c_message[2]=='I' && c_message[3]=='T' ){
send(Client_socket_fd , c_message , strlen(c_message) , 0 );
cout<<"This client will now quit"<<endl;
close(Client_socket_fd);
return 0;
}
if(c_message[0]=='D' && c_message[1]=='E' && c_message[2]=='L' && c_message[3]=='E' && c_message[4]==' ' ){
send(Client_socket_fd , c_message , strlen(c_message) , 0 );
cout<<"The Client wishes the server to delete the file:"<<&c_message[5]<<endl;
read(Client_socket_fd , B_Mem , BUFFER_SIZE);
cout<<"Server reply:"<<B_Mem<<endl;
continue;
}
if(c_message[0]=='S' && c_message[1]=='T' && c_message[2]=='O' && c_message[3]=='R' && c_message[4]==' ' ){
//First check if the file to be stored even exists
file_name=&c_message[5];
validity_check=0;
dl = access(file_name.c_str() , F_OK);
if(dl==0)
{
cout<<"The file :"<<file_name<<" exists in this directory"<<endl;;
validity_check=1;
}
if(dl==-1){
cout<<"The file :"<<file_name<<" does not exists in this directory please make sure the file to be sent in present in the directory."<<endl;
validity_check=0;
}
if(validity_check==0){
continue;
}
//File to be sent does exist then send the command to the server.
send(Client_socket_fd , c_message , strlen(c_message) , 0 );
//Check the message from the server whether file will be allowed to be stored
read(Client_socket_fd , B_Mem , BUFFER_SIZE);
cout<<B_Mem<<endl;
if(B_Mem[0]=='A' && B_Mem[1]=='C' && B_Mem[2]=='K'){
cout<<"Server has acknowleged the storage , the file shall now be sent."<<endl;
}
else{
cout<<"Server has denied the storage , the file will not be sent."<<endl;
continue;
}
fp=fopen( &c_message[5] , "rb");
if(fp==NULL){
cout<<"Error in opening file."<<endl;
continue;
}
res=0;
fseek(fp , 0l , SEEK_END);
res = ftell(fp);
fseek(fp , 0l , SEEK_SET);
cout<<"BUFFER_SIZE of file is bytes:"<<res<<endl;
converter_res= htonl(res);
send(Client_socket_fd , &converter_res , sizeof(converter_res) ,0);
while( 1 )
{
bzero(data,sizeof(data));
n_bytes = fread(data,1,BUFFER_SIZE , fp);
cout<<"Number of bytes read:"<<n_bytes<<endl;
if(n_bytes > 0){
//cout<<"Sending"<<endl;
send(Client_socket_fd , data , (size_t)n_bytes ,0);
//cout<<data;
}
if (n_bytes<=0){
if(feof(fp)){
//cout<<"BUFFER_SIZE of data:"<<n_bytes<<endl;
//send(i , data , (size_t)n_bytes ,0);
cout<<"End of File"<<endl;
//continue;
}
if(ferror(fp)){
cout<<"Error Reading"<<endl;
}
break;
}
}
cout<<"File sent successfully"<<endl;
continue;
}
{
cout<<"Entered command is not valid"<<endl;
}
}
close(Client_socket_fd);
return 0;
}