-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
604 lines (512 loc) · 15 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
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdbool.h>
#include "client.h"
void client_show_menu(int sd)
{
int select;
if(account_type==1 || account_type==2)
{
write(1,"1) Check account balance. \n",sizeof("1) Check account balance. \n"));
write(1,"2) View all Transactions. \n",sizeof("2) View all Transactions. \n"));
write(1,"3) Deposit money into account. \n",sizeof("3) Deposit money into account. \n"));
write(1,"4) Withdraw money into account. \n",sizeof("4) Withdraw money into account. \n"));
write(1,"5) Change Password. \n",sizeof("5) Change Password. \n"));
write(1,"6) Exit. \n",sizeof("6) Exit. \n"));
write(1,"Choose the option : ",sizeof("Choose the option : "));
scanf("%d",&select);
if(select==1)
{
client_check_balance(sd);
}
else if(select==2)
{
client_view_details(sd);
}
else if(select==3)
{
client_deposit_money(sd);
}
else if(select==4)
{
client_withdraw_money(sd);
}
else if(select==5)
{
client_change_password(sd);
}
else if(select==6)
{
write(sd,&select,sizeof(int));
write(1,"Thank you !\n",sizeof("Thank you !\n"));
write(1,"Created BY: Soham Kolhe. \n",sizeof("Created BY: Soham Kolhe. \n"));
exit(0);
}
else
{
write(1,"Invalid option!!\n\n",sizeof("Invalid option!!\n\n"));
client_show_menu(sd);
}
}
else if(account_type==3)
{
write(1,"1) Search for an account. \n",sizeof("1) Search for an account. \n"));
write(1,"2) Add an account. \n",sizeof("2) Add an account. \n"));
write(1,"3) Delete an account. \n",sizeof("3) Delete an account. \n"));
write(1,"4) Modify an account. \n",sizeof("4) Modify an account. \n"));
write(1,"5) Exit. \n",sizeof("5) Exit. \n"));
write(1,"Choose an option: ",sizeof("Choose an option: "));
scanf("%d",&select);
if(select == 1)
{
client_search_account(sd);
}
else if(select == 2)
{
client_add_account(sd);
}
else if(select == 3)
{
client_delete_account(sd);
}
else if(select == 4)
{
client_update_account(sd);
}
else if (select == 5)
{
write(sd,&select,sizeof(int));
write(1,"Thank you !\n",sizeof("Thank you !\n"));
write(1,"Created BY: Soham Kolhe. \n",sizeof("Created BY: Soham Kolhe. \n"));
exit(0);
}
else
{
write(1,"Invalid option!!\n\n",sizeof("Invalid option!!\n\n"));
client_show_menu(sd);
}
}
}
void client_choose_option(int sd)
{
while(1)
{
write(1,"Choose Your Account Type \n",sizeof("Choose Your Account Type \n"));
write(1,"1)Normal User\n",sizeof("1)Normal User\n"));
write(1,"2)Joint Account User \n",sizeof("2)Joint Account User \n"));
write(1,"3)Admin Login \n",sizeof("3)Admin Login \n"));
write(1,"Choose the account type: ",sizeof("Choose the account type: "));
scanf("%d",&account_type);
if(account_type == 1)
{
client_normal_user_login(sd);
break;
}
else if(account_type == 2)
{
client_joint_user_login(sd);
break;
}
else if(account_type == 3)
{
client_admin_user_login(sd);
break;
}
else
{
write(1,"Invalid Choice!! \n\n",sizeof("Invalid Choice!! \n\n"));
}
}
return;
}
void client_normal_user_login(int sd)
{
bool result;
normal_user currUser;
write(1,"Enter your User ID: ",sizeof("Enter your User ID: "));
scanf("%d",&currUser.userID);
currUserID=currUser.userID;
write(1,"Enter your password: ",sizeof("Enter your password: "));
scanf("%s",currUser.password);
write(sd,&account_type,sizeof(int));
write(sd,&currUser,sizeof(normal_user));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Invalid Credentials. !!\n\n",sizeof("Invalid Credentials. !!\n\n"));
client_choose_option(sd);
}
else
{
write(1,"Logged in Sucessfully. !!\n\n",sizeof("Logged in Sucessfully. !!\n\n"));
}
return;
}
void client_joint_user_login(int sd)
{
bool result;
joint_user currUser;
write(1,"Enter your User ID: ",sizeof("Enter your User ID: "));
scanf("%d",&currUser.userID);
currUserID=currUser.userID;
write(1,"Enter your password: ",sizeof("Enter your password: "));
scanf("%s",currUser.password);
write(sd,&account_type,sizeof(int));
write(sd,&currUser,sizeof(joint_user));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Invalid Credentials. !!\n\n",sizeof("Invalid Credentials. !!\n\n"));
client_choose_option(sd);
}
else{
write(1,"Logged in Sucessfully. !!\n\n",sizeof("Logged in Sucessfully. !!\n\n"));
}
return;
}
void client_admin_user_login(int sd)
{
bool result;
admin_user currUser;
write(1,"Enter your User ID: ",sizeof("Enter your User ID: "));
scanf("%d",&currUser.userID);
currUserID=currUser.userID;
write(1,"Enter your password: ",sizeof("Enter your password: "));
scanf("%s",currUser.password);
write(sd,&account_type,sizeof(int));
write(sd,&currUser,sizeof(admin_user));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Invalid Credentials. !!\n\n",sizeof("Invalid Credentials. !!\n\n"));
client_choose_option(sd);
}
else
{
write(1,"Logged in Sucessfully. !!\n\n",sizeof("Logged in Sucessfully. !!\n\n"));
}
return;
}
void client_deposit_money(int sd)
{
float amount;
int select=1;
bool result;
write(1,"Enter the amount you want to Deposit: Rs.",sizeof("Enter the amount you want to Deposit: Rs."));
scanf("%f",&amount);
while(amount<=0)
{
write(1,"Please enter a valid amount!!\n",sizeof("Please enter a valid amount!!\n"));
write(1,"Enter the amount you want to Deposit: Rs.",sizeof("Enter the amount you want to Deposit: Rs."));
scanf("%f",&amount);
}
write(sd,&select,sizeof(int));
write(sd,&amount,sizeof(float));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error in depositing your money to your account. !!\n\n",sizeof("Error in depositing your money to your account. !!\n\n"));
}
else
{
write(1,"Your amount has been deposited succesfully !!\n\n",sizeof("Your amount has been deposited succesfully !!\n\n"));
}
client_show_menu(sd);
return;
}
void client_withdraw_money(int sd)
{
float amount;
int select=2;
bool result;
write(1,"Enter the amount you want to Withdraw: Rs.",sizeof("Enter the amount you want to withdraw: Rs."));
scanf("%f",&amount);
while(amount<=0)
{
write(1,"Please enter a valid amount!!\n",sizeof("Please enter a valid amount!!\n"));
write(1,"Enter the amount you want to Withdraw: Rs.",sizeof("Enter the amount you want to withdraw: Rs."));
scanf("%f",&amount);
}
write(sd,&select,sizeof(int));
write(sd,&amount,sizeof(float));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error in withdrawing your money from your account. !!\n\n",sizeof("Error in withdrawing your money from your account. !!\n\n"));
}
else
{
write(1,"Amount Succesfully withdrawn from your account !!\n\n",sizeof("Amount Succesfully withdrawn from your account !!\n\n"));
}
client_show_menu(sd);
return;
}
void client_check_balance(int sd)
{
float amount;
int select=3;
int len;
write(sd,&select,sizeof(int));
len=read(sd,&amount,sizeof(float));
write(1,"Available balance in your account is: Rs.",sizeof("Available balance in your account is: Rs."));
printf("%0.3f\n\n",amount);
client_show_menu(sd);
return;
}
void client_change_password(int sd)
{
int select=4;
char nPassword[10];
bool result;
write(1,"Enter the new password: ",sizeof("Enter the new password: "));
scanf("%s",nPassword);
write(sd,&select,sizeof(int));
write(sd,nPassword,sizeof(nPassword));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error in changing your account password!!\n\n",sizeof("Error in changing your account password!!\n\n"));
}
else
{
write(1,"Succesfully changed your login password!!\n\n",sizeof("Succesfully changed your login password!!\n\n"));
}
client_show_menu(sd);
return;
}
void client_view_details(int sd)
{
int select=5;
write(sd,&select,sizeof(int));
if(account_type==1)
{
transaction transaction_details;
while(read(sd,&transaction_details,sizeof(transaction))!=0)
{
if(transaction_details.value>0)
{
printf("%s %f\n",transaction_details.transaction,transaction_details.value);
}
else
{
break;
}
}
}
else if(account_type==2)
{
transaction transaction_details;
while(read(sd,&transaction_details,sizeof(transaction))!=0)
{
if(transaction_details.value>0)
{
printf("%s %f\n",transaction_details.transaction,transaction_details.value);
}
else
{
break;
}
}
}
client_show_menu(sd);
return;
}
void client_add_account(int sd)
{
int select=1;
int type;
bool result;
write(sd,&select,sizeof(int));
write(1,"Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: ",sizeof("Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: "));
scanf("%d",&type);
write(sd,&type,sizeof(int));
if(type==1)
{
normal_user addUser1;
write(1,"Name of the account holder : ",sizeof("Name of the account holder : "));
scanf(" %[^\n]",addUser1.name);
write(1,"Account password: ",sizeof("Account password: "));
scanf("%s",addUser1.password);
write(1,"Account Initial Deposit: Rs.",sizeof("Account Initial Deposit: Rs."));
scanf("%f",&addUser1.balance);
write(sd,&addUser1,sizeof(normal_user));
}
else if(type==2)
{
joint_user addUser2;
write(1,"Name of the primary account holder: ",sizeof("Name of the primary account holder: "));
scanf(" %[^\n]",addUser2.name1);
write(1,"Name of the secondary account holder: ",sizeof("Name of the secondary account holder: "));
scanf(" %[^\n]",addUser2.name2);
write(1,"Account password: ",sizeof("Account password: "));
scanf("%s",addUser2.password);
write(1,"Account Initial Deposit: Rs.",sizeof("Account Initial Deposit: Rs."));
scanf("%f",&addUser2.balance);
write(sd,&addUser2,sizeof(joint_user));
}
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error in adding the account !!\n\n",sizeof("Error in adding the account !!\n\n"));
}
else
{
write(1,"Succesfully added the account !!\n\n",sizeof("Succesfully added the account !!\n\n"));
}
client_show_menu(sd);
return;
}
void client_delete_account(int sd)
{
int select=2;
int type,userID;
bool result;
write(sd,&select,sizeof(int));
write(1,"Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: ",sizeof("Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: "));
scanf("%d",&type);
write(sd,&type,sizeof(int));
write(1,"User ID : ",sizeof("User ID : "));
scanf("%d",&userID);
write(sd,&userID,sizeof(int));
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error int deleting the account.\nPlease check the User ID.\n\n",sizeof("Error int deleting the account.\nPlease check the User ID.\n\n"));
}
else
{
write(1,"Account Deleted Sucessfully !! \n\n",sizeof("Account Deleted Sucessfully !! \n\n"));
}
client_show_menu(sd);
return;
}
void client_update_account(int sd)
{
int select=3;
int type;
bool result;
write(sd,&select,sizeof(int));
write(1,"Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: ",sizeof("Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: "));
scanf("%d",&type);
write(sd,&type,sizeof(int));
if(type==1)
{
normal_user modUser1;
write(1,"Enter account ID: ",sizeof("Enter account ID: "));
scanf("%d",&modUser1.userID);
write(1,"Enter Account Number: ",sizeof("Enter Account Number: "));
scanf("%d",&modUser1.account_number);
write(1,"Enter New Account Holder Name: ",sizeof("Enter New Account Holder Name: "));
scanf(" %[^\n]",modUser1.name);
write(1,"Enter New Account Password: ",sizeof("Enter New Account Password: "));
scanf("%s",modUser1.password);
write(1,"Enter New Account Balance : ",sizeof("Enter New Account Balance : "));
scanf("%f",&modUser1.balance);
write(sd,&modUser1,sizeof(normal_user));
}
else if(type==2)
{
joint_user modUser2;
write(1,"Enter account ID: ",sizeof("Enter account ID: "));
scanf("%d",&modUser2.userID);
write(1,"Enter Account Number: ",sizeof("Enter Account Number: "));
scanf("%d",&modUser2.account_number);
write(1,"Enter New Primary Account Holder Name: ",sizeof("Enter New Primary Account Holder Name: "));
scanf(" %[^\n]",modUser2.name1);
write(1,"Enter New Secondary Account Holder Name: ",sizeof("Enter New Secondary Account Holder Name: "));
scanf(" %[^\n]",modUser2.name2);
write(1,"Enter New Account Password: ",sizeof("Enter New Account Password: "));
scanf("%s",modUser2.password);
write(1,"Enter New Account Balance : ",sizeof("Enter New Account Balance : "));
scanf("%f",&modUser2.balance);
write(sd,&modUser2,sizeof(joint_user));
}
read(sd,&result,sizeof(result));
if(!result)
{
write(1,"Error in modifying the entered account.\nPlease check the User ID and Account No.\n\n",sizeof("Error in modifying the entered account.\nPlease check the User ID and Account No.\n\n"));
}
else
{
write(1,"Account modified Sucessfully !!\n\n",sizeof("Account modified Sucessfully !!\n\n"));
}
client_show_menu(sd);
return;
}
void client_search_account(int sd)
{
int select=4;
int type,len;
bool result;
write(sd,&select,sizeof(int));
write(1,"Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: ",sizeof("Enter the account type \n1) Normal use account \n2) Joint user account \nChoice: "));
scanf("%d",&type);
write(sd,&type,sizeof(int));
if(type==1)
{
normal_user searchUser1;
int userID;
write(1,"Enter account ID: ",sizeof("Enter account ID: "));
scanf("%d",&userID);
write(sd,&userID,sizeof(int));
len=read(sd,&searchUser1,sizeof(normal_user));
if(len==0)
{
write(1,"Please check the Account ID!!\n\n",sizeof("Please check the Account ID!!\n\n"));
}
else
{
printf("Account ID : %d\n",searchUser1.userID);
printf("Account Number : %d\n",searchUser1.account_number);
printf("Account holder name : %s\n",searchUser1.name);
printf("Available Balance: Rs.%0.3f\n",searchUser1.balance);
printf("Account status : %s\n\n",searchUser1.status);
}
}
else if(type==2)
{
joint_user searchUser2;
int userID1;
write(1,"Enter account ID: ",sizeof("Enter account ID: "));
scanf("%d",&userID1);
write(sd,&userID1,sizeof(int));
len=read(sd,&searchUser2,sizeof(joint_user));
if(len==0)
{
write(1,"Please check the Account ID!!\n\n",sizeof("Please check the Account ID!!\n\n"));
}
else
{
printf("Account ID: %d\n",searchUser2.userID);
printf("Account Number: %d\n",searchUser2.account_number);
printf("Primary Account Holder's Name: %s\n",searchUser2.name1);
printf("Secondary Account Holder's Name: %s\n",searchUser2.name2);
printf("Available Balance: Rs.%0.3f\n",searchUser2.balance);
printf("Account Status: %s\n\n",searchUser2.status);
}
}
client_show_menu(sd);
return;
}
int main()
{
struct sockaddr_in server;
int sd,msgLength;
char buff[50];
char result;
sd=socket(AF_INET,SOCK_STREAM,0);
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
server.sin_port=htons(5555);
connect(sd,(struct sockaddr *)&server,sizeof(server));
client_choose_option(sd);
client_show_menu(sd);
//Closing the connection
close(sd);
return 0;
}