-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
641 lines (495 loc) · 27.1 KB
/
client.c
File metadata and controls
641 lines (495 loc) · 27.1 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <poll.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include "msg_struct.h"
#define TAILLE 1024
int create_client_socket(char *addr_ip, short port) {
printf("Creating Client socket...\n");
int client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_aton(addr_ip, &(addr.sin_addr));
printf("Client connecting to Server...\n\n");
int connect_result = connect(client_socket, (struct sockaddr *)&addr, sizeof(addr));
if (connect_result == -1) { perror("ERROR while Connecting"); exit(1); }
return client_socket;
}
void send_msg(int sockfd) {
char buf[TAILLE] = "";
char *buf_support = NULL;
struct message msgstruct;
char *element_decoupe = NULL;
char *le_pseudoo = NULL;
char *le_salon = NULL;
char *str_support;
// select variables
fd_set readfds;
int fd_stdin = fileno(stdin);
int select_result;
struct timeval tv;
//sending file variables
char *file_name = NULL;
int passed = 0;
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
printf("\n[Server] : %s\n", msgstruct.infos);
}
while (1) {
// Cleaning memory
memset(&msgstruct, 0, sizeof(struct message));
memset(buf, 0, TAILLE);
element_decoupe = malloc(256*sizeof(char));
// putting stdin fd in readfds after cleaning it
FD_ZERO(&readfds);
FD_SET(fd_stdin, &readfds);
// time to wait in scanf
tv.tv_sec = 9;
tv.tv_usec = 0;
// Envoyer au serveur une chaine ecrite au clavier
if (le_salon != NULL){
le_salon[strcspn(le_salon,"\n")] = 0;
printf("\n>>>>[%s]: ", le_salon);
}
else{
printf("\n>>>>: ");
}
//printf("\n>>>>: ");
fflush(stdout);
select_result = select(fd_stdin + 1, &readfds, NULL, NULL, &tv);
if (select_result == -1) { // if there is an error with select
printf("Error while using select\n");
exit(1);
}
else if (select_result == 0) { // if nothing was entered or Enter wasn't pressed afr 8s
// reading what was sent to us
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), MSG_DONTWAIT) > 0){
if( (msgstruct.pld_len == -1) && (strcmp( msgstruct.infos, "") != 0) ){
if( (strcmp( msg_type_str[msgstruct.type], "BROADCAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "UNICAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "MULTICAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "FILE_REQUEST") == 0) ){
if(strcmp( msg_type_str[msgstruct.type], "FILE_REQUEST") == 0){
passed++;
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] want to send you the file: '%s'. Do you accept? [Y/N]\n", msgstruct.nick_sender, msgstruct.infos);
}
else if( (strcmp( msg_type_str[msgstruct.type], "FILE_ACCEPT") == 0) || (strcmp( msg_type_str[msgstruct.type], "FILE_REJECT") == 0) ){
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] %s \n", msgstruct.nick_sender, msgstruct.infos);
}
else{
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
printf("\n[%s] : %s\n", msgstruct.nick_sender, msgstruct.infos);
}
}
else{
printf("\n[Server] : %s\n", msgstruct.infos);
}
}
}
}
else { // if something was entered and Enter was pressed
if(fgets(buf, 256, stdin) == NULL){
printf("# Attention # : What you entered is not valid !\n");
}
else{ // if fgets worked successfully
buf_support = malloc(128*sizeof(char));
strcpy(buf_support, buf);
// if the client sent /quit or /who
if(strcmp( buf, "/quit\n") == 0){
if(le_salon != NULL){
le_salon = NULL;
memset(&msgstruct, 0, sizeof(struct message));
msgstruct.type = MULTICAST_QUIT;
msgstruct.pld_len = -1;
send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0);
}
else{
printf("Ending the connection...\n");
break;
}
}
else if(strcmp( buf, "/who\n" ) == 0){
msgstruct.type = NICKNAME_LIST;
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
printf("[Server] : %s\n", msgstruct.infos);
}
}
else if(strcmp( buf, "/channel_list\n" ) == 0){
msgstruct.type = MULTICAST_LIST;
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
printf("[Server] : %s\n", msgstruct.infos);
}
}
else if( (strcmp( buf, "Y\n" ) == 0) && (passed > 0) ){
msgstruct.type = FILE_ACCEPT;
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
//printf("[Server] : %s\n", msgstruct.infos);
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] %s \n", msgstruct.nick_sender, msgstruct.infos);
}
}
else if( (strcmp( buf, "N\n" ) == 0) && (passed > 0) ){
msgstruct.type = FILE_REJECT;
//strncpy(msgstruct.infos, " cancelled file transfer.", sizeof(msgstruct.infos) - 1);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
//printf("[Server] : %s\n", msgstruct.infos);
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] %s \n", msgstruct.nick_sender, msgstruct.infos);
}
}
else { // if the client wrote /msgtype+arg or something else
element_decoupe = strtok(buf," ");
// Filling msgstruct
if(strncmp( element_decoupe, "/nick", sizeof("/nick") - 1) == 0){
msgstruct.type = NICKNAME_NEW;
// cut /nick and only take the pseudo
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
element_decoupe = strtok(NULL," "); // NULL if nothing after pseudo
if(element_decoupe != NULL){// if pseudo contains space
printf("# Attention # : Your pseudo is not valid !\n");
}
else{ // if valid pseudo
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct\n");
break;
}
le_pseudoo = malloc(128*sizeof(char));
strncpy(le_pseudoo, msgstruct.infos, sizeof(le_pseudoo) - 1);
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
printf("[Server] : %s\n", msgstruct.infos);
}
}
}
else{
printf("# Attention # : Please specify a pseudo!\n");
}
}
else if(strncmp( element_decoupe, "/whois", sizeof("/whois") - 1) == 0){
msgstruct.type = NICKNAME_INFOS;
// cut /whois and only take the pseudo
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
element_decoupe = strtok(NULL," "); // NULL if nothing after pseudo
if(element_decoupe != NULL){// if pseudo contains space
printf("# Attention # : The pseudo is not valid !\n");
}
else{ // if valid pseudo
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for whois\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
printf("[Server] : %s\n", msgstruct.infos);
}
}
}
else{
printf("# Attention # : Please specify a pseudo!\n");
}
}
else if(strncmp( element_decoupe, "/msgall", sizeof("/msgall") - 1) == 0){
msgstruct.type = BROADCAST_SEND;
// cut /msgall and only take the msg
element_decoupe = strtok(NULL,"");
if(element_decoupe != NULL){
// sending first msg with pld_len
msgstruct.pld_len = sizeof(element_decoupe);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for msg1\n");
break;
}
// sending second msg with the mssg
msgstruct.pld_len = -1;
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for msgall\n");
break;
}
}
else{
printf("# Attention # : Please enter a msg!\n");
}
}
else if(strncmp( element_decoupe, "/msg", sizeof("/msg") - 1) == 0){
msgstruct.type = UNICAST_SEND;
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
// cut /msg and only take the pseudo
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
// cut /msg and pseudo and only take the msg
element_decoupe = strtok(NULL,"");
if(element_decoupe != NULL){
// sending first msg with pld_len
msgstruct.pld_len = sizeof(element_decoupe);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for msg1\n");
break;
}
// sending second msg with the mssg
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
msgstruct.pld_len = -1;
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for msg1\n");
break;
}
}
else{
printf("# Attention # : Please enter a msg!\n");
}
}
else{
printf("# Attention # : Please enter a pseudo!\n");
}
}
else if(strncmp( element_decoupe, "/create", sizeof("/create") - 1) == 0){
msgstruct.type = MULTICAST_CREATE;
// cut /create and only take the channel_name
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
str_support = malloc(128*sizeof(char));
strncpy(str_support, msgstruct.infos, sizeof(le_salon) - 1);
element_decoupe = strtok(NULL," "); // NULL if nothing after channel_name
if(element_decoupe != NULL){// if channel_name contains space
printf("# Attention # : Your channel_name is not valid !\n");
}
else{ // if valid channel_name
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for multicast create\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
if(msgstruct.pld_len == -1){
le_salon = malloc(128*sizeof(char));
strncpy(le_salon, str_support, sizeof(le_salon) - 1);
}
printf("[Server] : %s\n", msgstruct.infos);
}
}
}
}
else if(strncmp( element_decoupe, "/join", sizeof("/join") - 1) == 0){
msgstruct.type = MULTICAST_JOIN;
// cut /join and only take the channel_name
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
str_support = malloc(128*sizeof(char));
strncpy(str_support, msgstruct.infos, sizeof(le_salon) - 1);
element_decoupe = strtok(NULL," "); // NULL if nothing after channel_name
if(element_decoupe != NULL){// if channel_name contains space
printf("# Attention # : channel_name is not valid !\n");
}
else{ // if valid channel_name
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for multicast create\n");
break;
}
//waiting server's reponse
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) > 0){
if(msgstruct.pld_len == -1){
le_salon = malloc(128*sizeof(char));
strncpy(le_salon, msgstruct.infos, sizeof(le_salon) - 1);
}
printf("[Server] : %s\n", msgstruct.infos);
}
}
}
}
else if(strncmp( element_decoupe, "/send", sizeof("/send") - 1) == 0){
msgstruct.type = FILE_REQUEST;
if(le_pseudoo != NULL){
strncpy(msgstruct.nick_sender, le_pseudoo, sizeof(msgstruct.nick_sender) - 1);
}
// cut /send and only take the pseudo
element_decoupe = strtok(NULL," ");
if(element_decoupe != NULL){
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
// cut /send and pseudo and only take the filename
element_decoupe = strtok(NULL,"");
if(element_decoupe != NULL){
// sending first msg with pld_len
msgstruct.pld_len = sizeof(element_decoupe);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for send1\n");
break;
}
// sending second msg with the mssg
file_name = malloc(128*sizeof(char));
strcpy(file_name, element_decoupe);
strncpy(msgstruct.infos, element_decoupe, sizeof(msgstruct.infos) - 1);
msgstruct.pld_len = -1;
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for send2\n");
break;
}
}
else{
printf("# Attention # : Please enter a filename!\n");
}
}
else{
printf("# Attention # : Please enter a pseudo!\n");
}
}
else { // utilisation d'ECHO SEND
memset(&msgstruct, 0, sizeof(struct message));
if(le_salon != NULL){
// sending first msg with pld_len
msgstruct.type = MULTICAST_SEND;
msgstruct.pld_len = sizeof(buf);
strcpy(msgstruct.infos, le_salon);
send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0);
// sending second msg with the mssg
memset(&msgstruct, 0, sizeof(struct message));
msgstruct.type = MULTICAST_SEND;
msgstruct.pld_len = -1;
strcpy(msgstruct.infos, buf_support);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for echo\n");
break;
}
}
else{
msgstruct.type = ECHO_SEND;
// sending first msg with pld_len
msgstruct.pld_len = sizeof(buf_support);
send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0);
// sending second msg with the mssg
msgstruct.pld_len = -1;
strcpy(msgstruct.infos, buf_support);
if (send(sockfd, (void*)&msgstruct, sizeof(msgstruct), 0) <= 0) {
printf("Problem while sending msgstruct for echo\n");
break;
}
}
}
}
}
}
// reading what was sent to us
memset(&msgstruct, 0, sizeof(struct message));
if(recv(sockfd, (void*)&msgstruct, sizeof(msgstruct), MSG_DONTWAIT) > 0){
if( (msgstruct.pld_len == -1) && (strcmp( msgstruct.infos, "") != 0) ){
if( (strcmp( msg_type_str[msgstruct.type], "BROADCAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "UNICAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "MULTICAST_SEND") == 0) || (strcmp( msg_type_str[msgstruct.type], "FILE_REQUEST") == 0) ){
if(strcmp( msg_type_str[msgstruct.type], "FILE_REQUEST") == 0){
passed++;
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] want to send you the file: '%s'. Do you accept? [Y/N]\n", msgstruct.nick_sender, msgstruct.infos);
}
else{
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
printf("\n[%s] : %s\n", msgstruct.nick_sender, msgstruct.infos);
}
}
else if( (strcmp( msg_type_str[msgstruct.type], "FILE_ACCEPT") == 0) || (strcmp( msg_type_str[msgstruct.type], "FILE_REJECT") == 0) ){
msgstruct.nick_sender[strcspn(msgstruct.nick_sender,"\n")] = 0;
msgstruct.infos[strcspn(msgstruct.infos,"\n")] = 0;
printf("\n[%s] %s \n", msgstruct.nick_sender, msgstruct.infos);
}
else{
printf("\n[Server] : %s\n", msgstruct.infos);
}
}
}
//printf("\n pld_len: %i\n nick_sender: %s\n type: %s\n infos: %s\n\n", msgstruct.pld_len, msgstruct.nick_sender, msg_type_str[msgstruct.type], msgstruct.infos);
}
if(element_decoupe != NULL){
free(element_decoupe);
}
if(le_pseudoo != NULL){
free(le_pseudoo);
}
if(le_salon != NULL){
free(le_salon);
}
if(str_support != NULL){
free(str_support);
}
if(buf_support != NULL){
free(buf_support);
}
if(file_name != NULL){
free(file_name);
}
}
int main(int argc, char *argv[]) {
if ( argc != 3 ){
printf("Usage : pgm ip_addr port_number\n");
return 0;
}
char *addr_ip = argv[1];
char *str_port = argv[2];
int port = atoi(str_port);
//char *addr_ip = "127.0.0.1";
//short port = 9090;
int client_socket = create_client_socket(addr_ip, port);
send_msg(client_socket);
close(client_socket);
return 0;
}