-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
341 lines (330 loc) · 11 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
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
void mainMenu(int);
void displayMenu(int);
void jointDisplayMenu(int);
void admin_functions(int);
//In C language, we get a warning if we call a function before declaring it. So to avoid warnings, we added the corresponding function prototypes above.
void accountSignup(int sockfd, int flag)
{
int account_id;
char username[20],password[20], buffer[51];
printf("\n*************************************** WELCOME TO NORMAL ACCOUNT SIGN-UP PAGE ****************************************\n");
printf("Enter username: ");
scanf(" %s",username);
printf("Enter password: ");
scanf(" %s",password);
write(sockfd, username,sizeof(username));
write(sockfd, password, sizeof(password));
read(sockfd, buffer, sizeof(buffer));
printf("%s", buffer);
if(flag==1) mainMenu(sockfd);
else admin_functions(sockfd);
}
void jointAccountSignup(int sockfd, int flag)
{
char username1[20], password1[20], username2[20], password2[20], buffer[51];
printf("\n**************************************** WELCOME TO JOINT ACCOUNT SIGN-UP PAGE ****************************************\n");
printf("Enter first username: ");
scanf(" %s", username1);
printf("Enter password: ");
scanf(" %s", password1);
printf("Enter second username: ");
scanf(" %s", username2);
printf("Enter password: ");
scanf(" %s", password2);
write(sockfd, username1, sizeof(username1));
write(sockfd, password1, sizeof(password1));
write(sockfd, username2, sizeof(username2));
write(sockfd, password2, sizeof(password2));
read(sockfd, buffer, sizeof(buffer));
printf("%s", buffer);
if(flag==1) mainMenu(sockfd);
else admin_functions(sockfd);
}
void accountLogin(int sockfd)
{
char buffer[33];
char username[20],password[20];
int flag;
printf("\n**************************************** WELCOME TO LOGIN PAGE ****************************************\n");
printf("Enter your username : ");
scanf(" %s",username);
printf("Enter your password : ");
scanf(" %s",password);
write(sockfd, username,sizeof(username));
write(sockfd, password, sizeof(password));
read(sockfd, &flag, sizeof(flag));
read(sockfd, buffer, sizeof(buffer));
printf("%s",buffer);
if(flag==1) displayMenu(sockfd); //NOTE that, If flag is 1, then it means customer was able to login successfully.
else if(flag==0) mainMenu(sockfd); //else if flag is 0, then it means customer has entered incorrect credentials.
}
void jointAccountLogin(int sockfd)
{
char buffer[30];
char username1[20],password[20],username2[20];
int flag;
printf("\n**************************************** WELCOME TO JOINT ACCOUNT LOGIN PAGE ****************************************\n");
printf("Enter first username : ");
scanf(" %s",username1);
printf("Enter second username : ");
scanf(" %s",username2);
printf("Enter password for first user : ");
scanf(" %s",password);
write(sockfd, username1,sizeof(username1));
write(sockfd, username2,sizeof(username2));
write(sockfd, password, sizeof(password));
read(sockfd, &flag, sizeof(flag));
read(sockfd, buffer, sizeof(buffer));
printf("%s",buffer);
if(flag==1) jointDisplayMenu(sockfd);
else mainMenu(sockfd);
}
void viewAccountDetails(int sockfd)
{
char username[20];
int acc_id, balance;
read(sockfd,username, sizeof(username));
read(sockfd,&acc_id, sizeof(acc_id));
read(sockfd,&balance, sizeof(balance));
printf("\nYour Account Details :\nUsername: %s\tAccount ID: %d\tBalance: Rs.%d\n",username,acc_id,balance);
displayMenu(sockfd);
}
void viewJointAccountDetails(int sockfd)
{
char username1[20], username2[20];
int acc_id, balance;
read(sockfd,username1, sizeof(username1));
read(sockfd,username2, sizeof(username2));
read(sockfd,&acc_id, sizeof(acc_id));
read(sockfd,&balance, sizeof(balance));
printf("\nYour Account Details :\nUsername1: %s\tUsername2: %s\tAccount ID: %d\tBalance: Rs.%d\n",username1,username2,acc_id,balance);
jointDisplayMenu(sockfd);
}
void deposit(int sockfd)
{
int amount;
printf("\nEnter amount to deposit: ");
scanf("%d",&amount);
printf("\nRs.%d deposited successfully !!!\n",amount);
write(sockfd, &amount, sizeof(amount));
read(sockfd, &amount, sizeof(amount));
printf("\nUpdated Balance : %d\n",amount);
displayMenu(sockfd);
}
void jointDeposit(int sockfd)
{
int amount;
printf("\nEnter amount to deposit: ");
scanf("%d",&amount);
printf("Rs.%d deposited successfully !!!\n",amount);
write(sockfd, &amount, sizeof(amount));
read(sockfd, &amount, sizeof(amount));
printf("Updated Balance : %d",amount);
jointDisplayMenu(sockfd);
}
void withdraw(int sockfd)
{
int amount,flag;
printf("\nEnter amount to withdraw: ");
scanf("%d",&amount);
write(sockfd, &amount, sizeof(amount));
read(sockfd, &flag, sizeof(flag));
if(flag==0)
{
printf("\nSorry !!! Insufficient Balance.\n");
read(sockfd, &amount, sizeof(amount));
printf("Current Balance: %d",amount);
}
else
{
printf("Rs.%d withdrawn Successfully !!!\n",amount);
read(sockfd, &amount, sizeof(amount));
printf("Updated Balance: %d\n",amount);
}
displayMenu(sockfd);
}
void jointWithdraw(int sockfd)
{
int amount,flag;
printf("\nEnter amount to withdraw: ");
scanf("%d",&amount);
write(sockfd, &amount, sizeof(amount));
read(sockfd, &flag, sizeof(flag));
if(flag==0)
{
printf("\nSorry !!! Insufficient Balance.\n");
read(sockfd, &amount, sizeof(amount));
printf("Current Balance : %d",amount);
}
else
{
printf("Rs.%d withdrawn Successfully !!!\n",amount);
read(sockfd, &amount, sizeof(amount));
printf("Updated Balance : %d\n",amount);
}
jointDisplayMenu(sockfd);
}
void passwordChange(int sockfd)
{
char password[20];
printf("\nEnter the new password: ");
scanf(" %s",password);
write(sockfd,password,sizeof(password));
printf("\nPassword Updated Successfully.\n");
mainMenu(sockfd);
}
void jointPasswordChange(int sockfd)
{
char password[20];
printf("\nEnter the new password: ");
scanf(" %s",password);
write(sockfd,password,sizeof(password));
printf("\nPassword Updated Successfully.\n");
mainMenu(sockfd);
}
void deleteAccount(int sockfd)
{
char username[20],buffer[35];
printf("\nEnter the username to be deleted: ");
scanf(" %s", username);
write(sockfd, username, sizeof(username));
read(sockfd, buffer, sizeof(buffer));
printf("%s",buffer);
admin_functions(sockfd);
}
void deleteJointAccount(int sockfd)
{
char username1[20], username2[20], buffer[35];
printf("Enter Username 1 to delete: ");
scanf(" %s",username1);
printf("Enter Username 2 to delete: ");
scanf(" %s",username2);
write(sockfd, username1, sizeof(username1));
write(sockfd, username2, sizeof(username2));
read(sockfd, buffer, sizeof(buffer));
printf("%s",buffer);
admin_functions(sockfd);
}
void admin_functions(int sockfd)
{
int choice;
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You have access to perform the following operations:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
printf("\n1.Create an Account\n2.Create a Joint Account\n3.Delete an Account\n4.Delete a Joint Account\n5.Search an Account\n6.Search a Joint Account\n7.Logout\n\nEnter your choice: ");
scanf("%d", &choice);
char buffer[46], username[20], password[20], username1[20], username2[20];
write(sockfd, &choice, sizeof(choice));
switch(choice)
{
case 1: accountSignup(sockfd,1); break; //1 means the user is an Admin
case 2: jointAccountSignup(sockfd,1); break; //1 means the user is an Admin
case 3: deleteAccount(sockfd); break;
case 4: deleteJointAccount(sockfd); break;
case 5: printf("\nEnter username to search: ");
scanf(" %s", username);
write(sockfd, username, sizeof(username));
read(sockfd, &buffer, sizeof(buffer));
printf(" %s", buffer);
admin_functions(sockfd);
break;
case 6: printf("\nEnter username 1 to search: ");
scanf(" %s", username1);
printf("\nEnter username 2 to search: ");
scanf(" %s", username2);
write(sockfd, username1, sizeof(username1));
write(sockfd, username2, sizeof(username2));
read(sockfd, &buffer, sizeof(buffer));
printf("%s", buffer);
admin_functions(sockfd);
break;
case 7: mainMenu(sockfd);
}
}
void adminLogin(int sockfd)
{
char username[20], password[20], buffer[20];
int flag;
printf("\n**************************************** WELCOME TO ADMIN LOGIN PAGE ****************************************\n");
printf("Enter admin username: ");
scanf(" %s",username);
printf("Enter admin password: ");
scanf(" %s",password);
write(sockfd, username, sizeof(username));
write(sockfd, password, sizeof(password));
read(sockfd, &flag, sizeof(flag));
read(sockfd, buffer, sizeof(buffer));
printf("%s",buffer);
if(flag == 0) admin_functions(sockfd);
else if(flag==1) mainMenu(sockfd);
}
void displayMenu(int sockfd)
{
int choice;
printf("----------------------------------------------------------------------------------------------------------------");
printf("\n1.VIEW MY ACCOUNT DETAILS\n2.DEPOSIT MONEY\n3.WITHDRAW MONEY\n4.CHANGE PASSWORD\n5.EXIT\n\nEnter your choice: ");
scanf("%d",&choice);
write(sockfd, &choice, sizeof(choice));
switch(choice)
{
case 1: viewAccountDetails(sockfd); break;
case 2: deposit(sockfd); break;
case 3: withdraw(sockfd); break;
case 4: passwordChange(sockfd); break;
case 5: mainMenu(sockfd); break;
default: printf("Please enter proper choice:\n"); break;
}
}
void jointDisplayMenu(int sockfd)
{
int choice;
printf("----------------------------------------------------------------------------------------------------------------");
printf("\n1.VIEW MY ACCOUNT DETAILS\n2.DEPOSIT MONEY\n3.WITHDRAW MONEY\n4.CHANGE PASSWORD\n5.EXIT\n\nEnter your choice: ");
scanf("%d",&choice);
write(sockfd, &choice, sizeof(choice));
switch(choice)
{
case 1: viewJointAccountDetails(sockfd); break;
case 2: jointDeposit(sockfd); break;
case 3: jointWithdraw(sockfd); break;
case 4: jointPasswordChange(sockfd); break;
case 5: mainMenu(sockfd); break;
}
}
void mainMenu(int sockfd)
{
int choice;
printf("**************************************** WELCOME TO ONLINE BANKING HOME PAGE ****************************************\n");
printf("\n1.CREATE A NEW ACCOUNT\n2.LOGIN TO YOUR ACCOUNT\n3.CREATE A NEW JOINT ACCOUNT\n4.LOGIN TO YOUR JOINT ACCOUNT\n5.ADMIN LOGIN\n6.EXIT\n\nEnter your choice: ");
scanf("%d",&choice);
write(sockfd, &choice, sizeof(choice));
switch(choice)
{
case 1: accountSignup(sockfd,1); break; //1 means the user is not an Admin
case 2: accountLogin(sockfd); break;
case 3: jointAccountSignup(sockfd,1); break; //1 means the user is not an Admin
case 4: jointAccountLogin(sockfd); break;
case 5: adminLogin(sockfd); break;
case 6: exit(0); break;
}
}
int main(int argc,char *argv[])
{
struct sockaddr_in server;
int sockfd; //socket descriptor
char buffer[80];
sockfd = socket(AF_INET, SOCK_STREAM,0);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(3456);
connect(sockfd, (struct sockaddr *)(&server), sizeof(server));
mainMenu(sockfd);
close(sockfd);
return 0;
}