-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalclient2.cpp
482 lines (446 loc) · 12.8 KB
/
finalclient2.cpp
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <bits/stdc++.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>
#include <string.h>
#include <iostream>
#include<pthread.h>
//#define port 9000
#define BUFF_SIZE 1024
#define trd 10
//#define ip "127.0.0.1"
//#define port1 9001
#define nm 100
using namespace std;
int port, port1;
int fl = 0;
string ip = "127.0.0.1";
vector<string> tracker_ip;
vector<int> tracker_port;
void *fileRead(void *sock){
cout<<"inside the thread"<<endl;
int client_socket = *((int *)sock);
string filename = "rfile"+to_string(++fl);
FILE *fp = fopen(filename.c_str(), "wb");
char Buffer[BUFF_SIZE];
int file_size,n;
recv(client_socket, &file_size, sizeof(file_size), 0);
cout<<"file size received "<<file_size<<endl;
while ((n=recv(client_socket,Buffer,BUFF_SIZE,0))>0 && file_size>0){
fwrite(Buffer, sizeof(char), n, fp);
file_size = file_size - n;
}
cout<<"file is closed"<<endl;
fclose(fp);
close(client_socket);
pthread_exit(NULL);
}
void *fileSend(void *sock){
char Buffer[BUFF_SIZE];
int file_size,n,size;
cout<<"inside sending thread"<<endl;
int client_socket = *((int *)sock);
recv(client_socket, Buffer, BUFF_SIZE, 0);
FILE *fp = fopen(Buffer,"rb");
fseek (fp,0,SEEK_END);
size = ftell(fp);
rewind (fp);
send (client_socket,&size,sizeof(size), 0);
cout<<Buffer<<endl;
memset(Buffer,'\0', BUFF_SIZE);
while ((n= fread(Buffer,sizeof(char),BUFF_SIZE,fp)) > 0 && size > 0 ){
send (client_socket,Buffer, n, 0);
size = size - n ;
}
cout<<"closing the file in sending theread"<<endl;
fclose(fp);
close(client_socket);
pthread_exit(NULL);
}
void *peerServer(void *sock){
int svr_socket;
pthread_t thread[trd];
char response[BUFF_SIZE];
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip.c_str());
int addrlen = sizeof(sockaddr);
//binding the address
bind(svr_socket, (struct sockaddr*) &server, sizeof(server));
//listening to respond
listen(svr_socket,trd);
int temp,i=0,sts;
string filename;
while(1){
//accepting request from clients
cout<<"server is listening"<<endl;
int client_socket = accept(svr_socket, (struct sockaddr *)&server, (socklen_t*)&addrlen);
sts = pthread_create(&thread[i], NULL, fileSend, &client_socket);
i++;
}
//closing the socket
close(svr_socket);
pthread_exit(NULL);
}
void *peerClient(void *args){
char *filepath = ((char *)args);
cout<<filepath<<endl;
int svr_socket;
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning values to the structure like type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(port1);
server.sin_addr.s_addr = inet_addr(ip.c_str());
//connecting to the server
int status = connect(svr_socket, (struct sockaddr*) &server, sizeof(server));
if(status<0){
cout<<"Error in connection establishment "<<endl;
}
char response[BUFF_SIZE];
cout<<"starting communication from client thread"<<endl;
FILE *fp1 = fopen("received","wb");
send (svr_socket,filepath,sizeof(filepath), 0);
char Buffer[BUFF_SIZE] ;
int n;
int file_size;
recv(svr_socket, &file_size, sizeof(file_size), 0);
while ((n=recv(svr_socket,Buffer,BUFF_SIZE,0))>0 && file_size>0){
fwrite(Buffer, sizeof(char), n, fp1);
file_size = file_size - n;
}
cout<<"closing the file"<<endl;
fclose(fp1);
pthread_exit(NULL);
}
//connect to Tracker
/*void connectTracker(){
int svr_socket;
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning values to the structure like type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(tracker_port[0]);
server.sin_addr.s_addr = inet_addr(ip.c_str());
//connecting to the server
int status = connect(svr_socket, (struct sockaddr*) &server, sizeof(server));
if(status<0){
cout<<"Error in connection establishment "<<endl;
}
}*/
//login into the sharing system
bool login(string user, string pass, int svr_socket, int tracker_desc){
int option = 2;
char username[nm];
char passwd[nm];
char buffer[BUFF_SIZE];
int status;
strcpy(buffer,user.c_str());
//cout<<"first"<<endl;
send(svr_socket,&option,sizeof(option),0);
//cout<<"second"<<endl;
send(svr_socket,buffer,BUFF_SIZE,0);
//cout<<"third"<<endl;
strcpy(buffer,pass.c_str());
send(svr_socket,buffer,BUFF_SIZE,0);
//cout<<"fourth"<<endl;
recv(svr_socket,&status,sizeof(status),0);
//cout<<"last"<<endl;
if(status)
return true;
else return false;
}
//creating user
bool createUser(string user, string pass, int svr_socket, int tracker_desc){
int option = 1;
char username[nm];
char passwd[nm];
char buffer[BUFF_SIZE];
int status;
strcpy(buffer,user.c_str());
//cout<<"first"<<endl;
send(svr_socket,&option,sizeof(option),0);
//cout<<"second"<<endl;
send(svr_socket,buffer,BUFF_SIZE,0);
//cout<<"third"<<endl;
strcpy(buffer,pass.c_str());
send(svr_socket,buffer,BUFF_SIZE,0);
//cout<<"fourth"<<endl;
recv(svr_socket,&status,sizeof(status),0);
//cout<<"last"<<endl;
if(status)
return true;
else return false;
}
//logout from the tracker
bool logout(string user, int svr_socket, int tracker_desc){
int option = 5;
char username[nm];
char passwd[nm];
char buffer[BUFF_SIZE];
int status;
strcpy(buffer,user.c_str());
send(svr_socket,&option,sizeof(option),0);
send(svr_socket,buffer,BUFF_SIZE,0);
recv(svr_socket,&status,sizeof(status),0);
if(status)
return true;
else return false;
}
//create group
bool createGroup(string user, int group_id, int svr_socket, int tracker_desc){
int option = 6;
char buffer[BUFF_SIZE];
int status;
strcpy(buffer,user.c_str());
send(svr_socket,&option,sizeof(option),0);
send(svr_socket,buffer,BUFF_SIZE,0);
cout<<group_id<<endl;
send(svr_socket,&group_id,sizeof(group_id),0);
recv(svr_socket,&status,sizeof(status),0);
if(status)
return true;
else return false;
}
//list all groups
void listAllGroups(int svr_socket, int tracker_desc){
int option = 7;
char buffer[BUFF_SIZE];
int status=1;
int group_id;
int gsize =0;
send(svr_socket,&option,sizeof(option),0);
recv(svr_socket,&gsize,sizeof(gsize),0);
while(gsize){
recv(svr_socket,&group_id,sizeof(group_id),0);
cout<<group_id<<" ";
recv(svr_socket,buffer,BUFF_SIZE,0);
cout<<buffer<<endl;
gsize--;
memset (buffer,'\0',BUFF_SIZE);
}
}
//join group
void joinGroup(string uname, int gid, int svr_socket, int tracker_desc){
int option = 8;
char buffer[BUFF_SIZE];
bool status;
//strcpy(buffer,uname.c_str());
send(svr_socket,&option,sizeof(option),0);
//send(svr_socket,buffer,BUFF_SIZE,0);
send(svr_socket,&gid,sizeof(gid),0);
recv(svr_socket,&status,sizeof(status),0);
if(status){
cout<<"requested for joining"<<endl;
}
else{
cout<<"join request failed"<<endl;
}
}
//listing all the pending request for a group
void listRequests(int gid, int svr_socket, int tracker_desc){
int option = 9;
char buffer[BUFF_SIZE];
bool status;
int preq;
//strcpy(buffer,uname.c_str());
send(svr_socket,&option,sizeof(option),0);
//send(svr_socket,buffer,BUFF_SIZE,0);
send(svr_socket,&gid,sizeof(gid),0);
recv(svr_socket,&preq,sizeof(preq),0);
if(!preq){
cout<<"N0 pending requests"<<endl;
return;
}
while(preq){
recv(svr_socket,buffer,BUFF_SIZE,0);
cout<<"requested "<<buffer<<endl;
preq--;
}
}
//Accept request
void acceptRequests(int gid, string un, string username, int svr_socket, int tracker_desc){
int option = 10;
char buffer[BUFF_SIZE];
bool status=true;
strcpy(buffer,un.c_str());
send(svr_socket,&option,sizeof(option),0);
send(svr_socket,&gid,sizeof(gid),0);
send(svr_socket,buffer,BUFF_SIZE,0);
strcpy(buffer,username.c_str());
send(svr_socket,buffer,BUFF_SIZE,0);
recv(svr_socket,&status,sizeof(status),0);
if(status){
cout<<"request accepted"<<endl;
}
else{
cout<<"not able to process the request"<<endl;
}
}
//Leave Group
void leaveGroup(int gid, string un, int svr_socket, int tracker_desc){
int option=11;
char buffer[BUFF_SIZE];
bool status=true;
strcpy(buffer,un.c_str());
send(svr_socket,&option,sizeof(option),0);
send(svr_socket,&gid,sizeof(gid),0);
send(svr_socket,buffer,BUFF_SIZE,0);
recv(svr_socket,&status,sizeof(status),0);
if(status){
cout<<"left group successfully"<<endl;
}
else{
cout<<"not able to leave group"<<endl;
}
}
//int socket(int domain, int type, int protocol);
int main(int argc, char* argv[]){
//command line arguments
ip = argv[1];
port = stoi(argv[2]);
//port1 = stoi(argv[3]);
string conf_file = argv[3];
//declarations
string st;
pthread_t td1,td2;
char filepath[nm];
string tfname,username,passwd;
int tport;
int option;
int userFlag = 0;
int loginFlag = 0;
bool status;
int log_option;
//vector<string> tracker_ip;
//vector<int> tracker_port;
ifstream file(conf_file);
for(int i=0;i<2;i++){
file >>tfname;
tracker_ip.push_back(tfname);
file >> tport;
tracker_port.push_back(tport);
}
int statust = pthread_create(&td1, NULL, peerServer,NULL);
//connecting to the tracker
int svr_socket;
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning values to the structure like type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(tracker_port[0]);
server.sin_addr.s_addr = inet_addr(ip.c_str());
int tracker_status = connect(svr_socket, (struct sockaddr*) &server, sizeof(server));
while(1){
cout<<"1.Create User "<<endl;
cout<<"2.Login "<<endl;
cin>>option;
if(option == 1){
cout<<"Enter username"<<endl;
cin>>username;
cout<<"Enter password"<<endl;
cin>>passwd;
status = createUser(username, passwd, svr_socket, tracker_status);
if(status == true){
cout<<"user create successful"<<endl;
userFlag = 1;
}
else{
cout<<"user create failed"<<endl;
continue;
}
}
else if(option == 2){
cout<<"Enter username"<<endl;
cin>>username;
cout<<"Enter password"<<endl;
cin>>passwd;
status = login(username, passwd, svr_socket, tracker_status);
if(status == true){
cout<<"login successful"<<endl;
loginFlag = 1;
}
else{
cout<<"login failed"<<endl;
continue;
}
}
if(loginFlag == 1){
while(1){
cout<<"3.Upload"<<endl;
cout<<"4.Download"<<endl;
cout<<"5.Logout"<<endl;
cout<<"6.Create Group"<<endl;
cout<<"7.List Groups"<<endl;
cout<<"8.Join Group"<<endl;
cout<<"9.List request"<<endl;
cout<<"10.Accept Request"<<endl;
cout<<"11.Leave Group"<<endl;
/*cout<<""<<endl;
cout<<""<<endl;*/
/*cin>>st;
if(st.compare("download") == 0){
cout<<"Enter filepath "<<endl;
cin>>filepath;
int cstatus = pthread_create(&td1, NULL, peerClient,&filepath);
}*/
cin>>log_option;
if(log_option == 5){
status = logout(username, svr_socket, tracker_status);
if(status == 1){
cout<<"logged out successfully"<<endl;
break;
}
}
else if(log_option == 6){
int group_id;
cin>>group_id;
status = createGroup(username, group_id, svr_socket, tracker_status);
if(status){
cout<<"group created"<<endl;
}
else{
cout<<"error in group creation"<<endl;
}
}
else if(log_option == 7){
listAllGroups(svr_socket, tracker_status);
}
else if(log_option == 8){
int group_id;
cin>>group_id;
joinGroup(username, group_id, svr_socket, tracker_status);
}
else if(log_option == 9){
int group_id;
cin>>group_id;
listRequests(group_id, svr_socket, tracker_status);
}
else if(log_option == 10){
int group_id;
string un;
cin>>group_id>>un;
acceptRequests(group_id, un, username, svr_socket, tracker_status);
}
else if(log_option == 11){
int group_id;
cin>>group_id;
leaveGroup(group_id, username, svr_socket, tracker_status);
}
}
}
}
return 0;
}