-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
412 lines (379 loc) · 9.82 KB
/
client.c
File metadata and controls
412 lines (379 loc) · 9.82 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include<string.h>
#include<stdbool.h>
struct password_change
{
char oldpassword[1024];
char newpassword[1024];
};
struct acc_no
{
long long int account_number;
};
struct customer{
char username[1024];
char password[1024];
long long int account_number;
double balance;
char type;
}user;
struct customer modification(struct customer data,int sd)
{
struct modify{
char oldusername[1024];
char newusername[1024];
char password[1024];
char type;
int update;
};
struct modify new;
int c=1;
while(c==1)
{
printf("\nChoose What Wants To Update\n");
printf("1.Update UserName\n");
printf("2.Password\n");
printf("3.Usertype\n");
int choice;
scanf("%d",&choice);
printf("\nPlease Enter following details\n");
printf("\nEnter original username(UNIQUE):");
scanf("%s",new.oldusername);
switch(choice)
{
case 1:
printf("\nEnter new username(UNIQUE):");
scanf("%s",new.newusername);
new.update=1;
c=0;
break;
case 2:
printf("\nEnter new password:");
scanf("%s",new.password);
new.update=2;
c=0;
break;
case 3:
printf("\nEnter usertype(n:normal a:admin j joint):");
scanf(" %c",&new.type);
new.update=3;
c=0;
break;
default:
printf("\nInvalid choice");
break;
}
}
write(sd,&new,sizeof(new));
struct modify res;
read(sd,&res,sizeof(res));
if(strcmp(res.oldusername,"ok")==0)
{
printf("\n\nYour Information is modified successfully.\n\n");
}
else
if(strcmp(res.oldusername,"exist")==0)
{
printf("\n\nUsername exist. please use diffrenet username\n\n");
}
else
if(strcmp(res.oldusername,"Not found")==0)
{
printf("\n\nUsername not found :(\n\n");
}
}
int main()
{
struct sockaddr_in serv;
int sd = socket (AF_INET, SOCK_STREAM, 0);
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = INADDR_ANY;
//insert new port no here
serv.sin_port = htons(7940);
int connect_result=connect (sd, (void *)(&serv), sizeof(serv));
printf("Connection result:%d\n",connect_result);
/*write(sd,"Hello server\n",14);
read(sd,buf,100);
printf("%s",buf);*/
printf("\n\n\n***************** WELCOME TO ONLINE BANK ********************\n");
char ch='Y';
while(ch=='Y'){
// do you client stuff here
//write(sd,"Hello server\n",14);
char name[1024],pass[1024];
printf("\nEnter useranme:");
scanf("%s",user.username);
printf("Enter Password:");
scanf("%s",user.password);
write(sd,&user,sizeof(user));
int size;
char buf[100];
read(sd,buf,100);
//printf("%s\n",buf);
//admin....
if(strcmp(buf,"admin")==0)//compare with msg send by server
{
printf("\n\n\t\tADMIN LOGIN");
bool inside=1;
while(inside==1)
{
printf("\n--------------------------------------------------");
printf("\nChoose Option:\n");
printf("1.Add New Account\n2.Modify Account\n3.Search an Account\n4.Delete Account\n5.Quit\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
//add account
case 1:
write(sd,"add",4);
while(1)
{
struct customer new,newj;
printf("\nPlease Enter following details\n");
printf("\nEnter username(UNIQUE):");
scanf("%s",new.username);
printf("\nEnter password:");
scanf("%s",new.password);
printf("\nEnter Customer type (a:admin n:normal j:joint):");
scanf(" %c",&new.type);
if(new.type=='j')//not completed
{
printf("\nEnter second username(UNIQUE) for joint account:");
scanf("%s",newj.username);
printf("\nEnter password:");
scanf("%s",newj.password);
newj.type='j';
write(sd,&new,sizeof(new));
write(sd,&newj,sizeof(newj));
}
else
{
write(sd,&new,sizeof(new));
}
char res[100];
read(sd,&res,100);
if(strcmp(res,"ok")==0)
{
struct acc_no a;
int fd2=open("acc_no",O_CREAT|O_RDWR,0744);
read(fd2,&a,sizeof(a));
printf("\n\nCongratulations...\t:)\nAccount created successfully ... Your account number is %lld\n\n",a.account_number);
break;
}
else
if(strcmp(res,"exist")==0)
{
printf("\n\nUsername exist. please use diffrenet username\n");
continue;
}
}
break;
//modification
case 2:
write(sd,"mod",4);
struct customer mod;
mod=modification(mod,sd);
break;
//search
//problem is when you search for name which is not in database
case 3:
write(sd,"srh",4);
struct customer ser;
printf("Please Enter following details\n");
printf("\nEnter username to be searched:");
scanf("%s",ser.username);
write(sd,&ser,sizeof(ser));
int ssize;
while((ssize=read(sd,&ser,sizeof(ser)))==0);
printf("%s",ser.username);
if(strcmp(ser.username,"Not found")==0)//username ==not found
{
printf("\n\nUsername does not Exist :(\n\n");
}
else
{
printf("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
printf("\nUser information:\n");
printf("\tUsername:%s\n",ser.username);
//printf("%s\n",ser.password);
printf("\tAccount no:%lld\n",ser.account_number);
printf("\tBalance:%f\n",ser.balance);
printf("\tUser type(j:joint,n:normal,a:admin) %c",ser.type);
printf("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n");
}
break;
//delete
case 4:
write(sd,"del",4);
struct customer del;
printf("\nPlease Enter following details\n");
printf("\nEnter username to be deleted:");
scanf("%s",del.username);
write(sd,&del,sizeof(del));
int dsize;
while((dsize=read(sd,&del,sizeof(del)))==0);
if(strcmp(del.username,"Not found")==0)//username ==not found
{
printf("\n\nUsername does not Exist :(\n\n");
}
else
{
printf("\n\nAccount deleted successfully :)\n\n");
}
break;
//exit
case 5:
write(sd,"out",4);
inside=0;
break;
default:
printf("\n\nInvalid choice\n\n");
}
}
}
else if(strcmp(buf,"normal")==0||strcmp(buf,"joint")==0)
{
if(strcmp(buf,"joint")==0)
{
printf("\n\n\t\tJOINT USER");
}
else
{
printf("\n\n\t\tNORMAL USER");
}
int inside=1;
while(inside==1)
{
printf("\n--------------------------------------------------");
printf("\nEnter Choice\n");
printf("1.Deposit\n");
printf("2.Withdraw\n");
printf("3.Balance Enquiry\n");
printf("4.Password Change\n");
printf("5.View Passbook\n");
printf("6.Exit\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1:
write(sd,"dep",4);
struct customer depo,res;
//struct responce res;
printf("Enter Deposit Amount:\n");
scanf("%lf",&depo.balance);
printf("%lf",depo.balance);
write(sd,&depo,sizeof(depo));
read(sd,&res,sizeof(res));
if(strcmp(res.username,"unsuccess")==0)
{
printf("\n\nTransaction Unsuccessful :(\n\n");
}
else if(strcmp(res.username,"success")==0)
{
printf("\n\nTransaction successfull :)\nYour current balance is %f\n\n",res.balance);
}
break;
case 2:
write(sd,"wit",4);
struct customer wit,res1;
//struct responce res;
printf("Enter Withdraw Amount:\n");
scanf("%lf",&wit.balance);
write(sd,&wit,sizeof(wit));
read(sd,&res1,sizeof(res1));
if(strcmp(res1.username,"notenough")==0)
{
printf("\n\nNot enough money in your account :(\n\n");
}
else if(strcmp(res1.username,"success")==0)
{
printf("\n\nTransaction successfull :)\nYour current balance is %f\n\n",res1.balance);
}
break;
case 3:
write(sd,"bal",4);
struct customer res2;
read(sd,&res2,sizeof(res2));
if(strcmp(res2.username,"unsuccess")==0)
{
printf("\n\nRequest Unsuccessful :(\n\n");
}
else if(strcmp(res2.username,"success")==0)
{
printf("\n\nYour current balance is %f\n\n",res2.balance);
}
break;
case 4:
write(sd,"pas",4);
struct password_change ps;
struct customer res3;
printf("\nPlease enter old password:");
scanf("%s",ps.oldpassword);
printf("\nPlease enter new password:");
scanf("%s",ps.newpassword);
write(sd,&ps,sizeof(ps));
read(sd,&res3,sizeof(res3));
if(strcmp(res3.username,"notmatch")==0)
{
printf("\n\nYour old password does not match :(\n\n");
}
else if(strcmp(res3.username,"success")==0)
{
printf("\n\nYour password is changed successfully :)\n\n");
}
break;
case 5:
write(sd,"vie",4);
char buf[10000];
int len=read(sd,buf,10000);
if(strcmp(buf,"empty")==0)
{
printf("\nYour passbook is empty :(\n\n");
}
else
{
printf("\n\t\tPassbook");
printf("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
printf("\n%s",buf);
printf("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n");
}
break;
case 6:
write(sd,"out",4);
inside=0;
break;
default:
printf("\n\nInvalid choice\n\n");
break;
}
}
}
else if(strcmp(buf,"Invalid")==0)
{
printf("\n\nUsername or Password is Invalid. Please try again :)\n\n");
continue;
}
else if(strcmp(buf,"logged")==0)
{
printf("\n\nUser is already logged in. Please try later :)\n\n");
continue;
}
else
{
printf("\ndefault\n");
}
printf("\n\nYou are logged out\n\n");
printf("Do you want to continue(Y/N)?");
scanf(" %c",&ch);
write()
}
}