-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
143 lines (129 loc) · 2.91 KB
/
client.c
File metadata and controls
143 lines (129 loc) · 2.91 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
#include<stdio.h>
#include<sys/socket.h>
#include<unistd.h>
#include<string.h>
#include <netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#define PORT 28223
#define PACK 512
void sendmesg(char* str,int sock){
int numpack = (strlen(str)-1)/PACK +1;
write(sock,&numpack,sizeof(int));
char* msg = (char *)malloc(numpack*PACK);
strcpy(msg,str);
for(int i=0;i<numpack;++i){
int n=write(sock,msg,PACK);
msg += PACK;
}
}
char* readmesg(int sock){
int n;
read(sock,&n,sizeof(int));
char* str = (char *)malloc(n*PACK);
char * tmp = str;
for(int i=0;i<n;++i){
read(sock,str,PACK);
str += PACK;
}
return tmp;
}
void usr(int sock){
printf("For mini-statement type MINI\n For balance-enquiry type BAL\n To close connection type EXIT\n");
while(1){
char inp[512]={0};
scanf("%s",inp);
sendmesg(inp,sock);
if(strcmp(inp,"EXIT")==0) break;
char* str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
}
close(sock);
}
void plc(int sock){
char* str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
char buff[64]={0};
while(1){
printf("Enter customer name for mini statement\n");
scanf("%s",buff);
sendmesg(buff,sock);
if(strcmp(buff,"EXIT")==0){
close(sock);
break;
}
char* rep = readmesg(sock);
while(*rep!='\0') printf("%c",*rep++);
}
return;
}
void adm(int sock){
while(1){
char buff[128] = {0};
char* str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
scanf("%s",buff);
sendmesg(buff,sock);
if(strcmp(buff,"EXIT")==0) break;
str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
memset(buff,0,sizeof(buff));
scanf("%s",buff);
sendmesg(buff,sock);
str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
memset(buff,0,sizeof(buff));
scanf("%s",buff);
sendmesg(buff,sock);
str = readmesg(sock);
while(*str!='\0') printf("%c",*str++);
}
close(sock);
return;
}
int main(){
int sock;
char buf[512] = {0};
if((sock = socket(AF_INET , SOCK_STREAM , 0)) == -1){
printf("Unable to create socket\n");
return 0;
}
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET,"127.0.0.1",&serv_addr.sin_addr)<=0){
printf("Invalid Address\n");
return 0;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) ==-1)
{
printf("Connection failed \n");
return 0;
}
char* retun;
retun = readmesg(sock);
while(*retun!='\0') printf("%c",*retun++);
memset(buf,0,sizeof(buf));
scanf("%s",buf);
sendmesg(buf,sock);
retun = readmesg(sock);
while(*retun!='\0') printf("%c",*retun++);
memset(buf,0,sizeof(buf));
scanf("%s",buf);
sendmesg(buf,sock);
retun = readmesg(sock);
if(strcmp(retun,"INVALID")==0){
printf("INVALID CREDENTIAL\n");
close(sock);
}
if(strcmp(retun,"USER")==0){
usr(sock);
}
if(strcmp(retun,"POLICE")==0){
plc(sock);
}
if(strcmp(retun,"ADMIN")==0){
adm(sock);
}
return 0;
}