-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectClientPg.cpp
More file actions
397 lines (347 loc) · 11.7 KB
/
projectClientPg.cpp
File metadata and controls
397 lines (347 loc) · 11.7 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
#include <iostream>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <cstdlib>
using namespace std;
int main (int argc, char *argv[])
{
system("clear");
int sizeBuffS = 500;
char opp[20];
char login = 'a';
int x = 0;
int sock;
struct sockaddr_in server;
struct hostent *hp;
char buff[1024];
sock = socket (AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket failed");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if(hp == 0)
{
perror("gethostbyname failed");
close(sock);
exit(1);
}
char hantar[2][sizeBuffS];
char dapat[10][sizeBuffS];
char logname[30]={""};
//printf("Client site : ");
//scanf("%s",hantar);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
if(connect(sock, (struct sockaddr *) &server, sizeof (server))< 0)
{
perror("connect failed");
close(sock);
exit(1);
}
if(x==0)
{
strcpy(hantar[0],"sent");
strcpy(hantar[1],"Client connetion OK..");
write(sock,hantar,sizeof(hantar));
x=1;
}
else
if(write(sock, hantar, sizeof(hantar)) < 0)
{
perror("sent failed");
close(sock);
exit(1);
}
if(read(sock,dapat,sizeof(dapat))>0)
{
//printf("Server site : %s\n",dapat);
//printf("Client site : ");
}
close(sock);
/////////////////////////////////////////////////////////////////////////////
int run = 1;
while(run == 1 ){
while(login == 'a'){
char idlog[50];
char passlog[50];
char logsql[sizeBuffS];
system("clear");
cout<<endl<<endl<<endl<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<"| LOGIN |"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<endl;
cout<<" Enter ID : ";
cin>>ws;
cin.getline(idlog,50);
cout<<" Enter Password : ";
cin.getline(passlog,50);
snprintf(logsql,sizeBuffS,"%s%s%s%s%s","SELECT * from uitm.student where stu_id = ",idlog
," and stu_password ='",passlog,"';");
//cout<<logsql<<endl;
//////////////////////////////////////
sock = socket (AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
connect(sock, (struct sockaddr *) &server, sizeof (server));
snprintf(hantar[0],sizeBuffS,"%s","login");
snprintf(hantar[1],sizeBuffS,"%s",logsql);
strcpy(dapat[0],"55");
write(sock, hantar, sizeof(hantar));
read(sock,dapat,sizeof(dapat));
strcpy(logname,dapat[0]);
//printf("Server site : %s\n",dapat);
close(sock);
//////////////////////////////////////
cout<<" ________________________________________________________"<<endl;
if (strcmp(logname,"")!=0){
login = 'b';
cout<< " Welcome "<<logname;
}
if(login == 'a'){
cout<<" Invalid ID or Password !";
}
}
cout<<endl;
cout<<" ________________________________________________"<<endl;
cout<<"| Option 1 : List Student |"<<endl;
cout<<"| Option 2 : Search Student |"<<endl;
cout<<"| Option 3 : Student Registration |"<<endl;
cout<<"| Option 4 : Delete Student |"<<endl;
cout<<"| Option 5 : LogOut |"<<endl;
cout<<"|________________________________________________|"<<endl;
cout<<" Enter option : ";
cin>>ws;
cin.getline(opp,20);
strcpy(dapat[0],"");
if(strcmp( opp,"1")== 0){
////////////////////////////////////////////////////////
system("clear");
cout<<endl<<" Welcome "<<logname<<endl<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<"| List Student |"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"| ID | Name |Couse Code| Tel no | Age |"<<endl;
cout<<"|____|____________________|__________|_____________|_____|"<<endl;
char dapat2[30][6][30];
//////////////////////////////////////
sock = socket (AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
connect(sock, (struct sockaddr *) &server, sizeof (server));
//snprintf(hantar,sizeBuffS,"%s","SELECT * from uitm.student;");
snprintf(hantar[0],sizeBuffS,"%s","1");
snprintf(hantar[1],sizeBuffS,"%s","SELECT * from uitm.student;");
write(sock, hantar, sizeof(hantar));
read(sock,dapat2,sizeof(dapat2));
close(sock);
//////////////////////////////////////
for (int x = 0;x<30;x++) {
if(strcmp(dapat2[x][0],"nnn")!=0){
cout << "|"<<setw(3) << dapat2[x][0];
cout << " |"<<setw(19) << dapat2[x][1];
cout << " |"<<setw(9) << dapat2[x][2];
cout << " |"<<setw(12) << dapat2[x][3];
cout << " |"<<setw(4) << dapat2[x][4]<<" |";
cout<<dapat2[x][5];
cout <<endl;
}
}
cout<<"|____|____________________|__________|_____________|_____|"<<endl;
cout <<endl;
cout << "_________________________________________________________"<<endl;
}
else if(strcmp( opp,"2")== 0){
////////////////////////////////////////////////////
system("clear");
char input[50];
char in[sizeBuffS];
cout<<endl<<" Welcome "<<logname<<endl<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<"| Search Student |"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<endl;
cout<<" Search by student ID = ";
cin>> ws;
cin.getline(input,50);
strcpy(in,"SELECT * from uitm.student where stu_id = ");
strcpy(in , strcat(in,input));
//////////////////////////////////////
sock = socket (AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
connect(sock, (struct sockaddr *) &server, sizeof (server));
snprintf(hantar[0],sizeBuffS,"%s","2");
snprintf(hantar[1],sizeBuffS,"%s",in);
write(sock, hantar, sizeof(hantar));
read(sock,dapat,sizeof(dapat));
//printf("Server site : %s\n",dapat);
close(sock);
//////////////////////////////////////
cout<<" ________________________________________________________"<<endl;
if(strcmp(dapat[0],"404")==0){
cout<<"Error. ID is Integer only!"<<endl;
}
else if(strcmp(dapat[0],"")==0){
cout<<"ID not found in database"<<endl;
}
else
{
cout << " ID = " <<dapat[0]<<endl;
cout << " Name = "<<dapat[1] <<endl;
cout << " Couse Code = "<<dapat[2] << endl;
cout << " Tel no = "<<dapat[3] << endl;
cout << " Age = "<<dapat[4] << endl;
}
strcpy(dapat[0],"");
cout<<" ________________________________________________________"<<endl;
}
else if(strcmp( opp,"3")== 0){
//////////////////////////////////////////////////////////////
char id[50];
char name[50];
char course[50];
char tel[50];
char password[50];
char age[50];
system("clear");
cout<<endl<<" Welcome "<<logname<<endl<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<"| Student Registration |"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<endl;
cout<<" Enter Student ID : ";
cin>> ws;
cin.getline(id,50);
cout<<" Enter Student Name : ";
cin.getline(name,50);
cout<<" Enter Student Course : ";
cin.getline(course,50);
cout<<" Enter Student Tel no : ";
cin.getline(tel,50);
cout<<" Enter Student Password : ";
cin.getline(password,50);
cout<<" Enter Student Age : ";
cin.getline(age,50);
char addsql[sizeBuffS];
snprintf(addsql,sizeBuffS,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
"INSERT INTO uitm.student (stu_id,stu_name,stu_course,stu_tel,stu_password,stu_age) ",
"VALUES (",id,",'",name,"','",course,"','",tel,"','",password,"','",age,"');");
//cout<<" "<<addsql<<endl;
//////////////////////////////////////
sock = socket (AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
connect(sock, (struct sockaddr *) &server, sizeof (server));
snprintf(hantar[0],sizeBuffS,"%s","3");
snprintf(hantar[1],sizeBuffS,"%s",addsql);
write(sock, hantar, sizeof(hantar));
read(sock,dapat,sizeof(dapat));
if(strcmp(dapat[0],"404")==0){
cout <<" ______________________________________ "<<endl;
cout<<" Error 1. ID is already in database \n or"<<endl;
cout<<" Error 2. ID and age is Integer only"<<endl;
}
else
{
cout <<" ______________________________________ "<<endl;
cout <<" | |"<<endl;
cout <<" | Registration successful |"<<endl;
cout <<" |______________________________________|"<<endl;
cout <<endl;
}
//printf("Server site : %s\n",dapat);
close(sock);
//////////////////////////////////////
cout<<"________________________________________________________"<<endl;
cout<<endl;
}
else if(strcmp( opp,"4")== 0){
///////////////////////////////////////////////////////////////////
system("clear");
cout<<endl<<" Welcome "<<logname<<endl<<endl;
cout<<" ________________________________________________________"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<"| Delete Student |"<<endl;
cout<<"|________________________________________________________|"<<endl;
cout<<endl;
char idd[50];
cout<<" Enter Student ID : ";
cin>> ws;
cin.getline(idd,50);
char delsql[sizeBuffS];
snprintf(delsql,sizeBuffS,"%s%s",
"DELETE from uitm.student where stu_id = ",idd);
//cout<<" "<<delsql<<endl;
//////////////////////////////////////
sock = socket (AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
connect(sock, (struct sockaddr *) &server, sizeof (server));
snprintf(hantar[0],sizeBuffS,"%s","4");
snprintf(hantar[1],sizeBuffS,"%s",delsql);
write(sock, hantar, sizeof(hantar));
read(sock,dapat,sizeof(dapat));
close(sock);
//////////////////////////////////////
if(strcmp(dapat[0],"404")==0){
cout <<" ______________________________________ "<<endl;
cout<<" Error. ID is Integer only! "<<endl;
}
else
{
cout <<" ______________________________________ "<<endl;
cout <<" | |"<<endl;
cout <<" | Delete successful |"<<endl;
cout <<" |______________________________________|"<<endl;
cout <<endl;
}
cout<<" ________________________________________________________"<<endl;
cout<<endl;
}
else if(strcmp( opp,"5")== 0){
login='a';
}
else {
system("clear");
cout <<endl;
cout <<endl;
cout <<" ______________________________________ "<<endl;
cout <<" | |"<<endl;
cout <<" | Option is not valid!. |"<<endl;
cout <<" |______________________________________|"<<endl;
cout <<endl;
cout <<endl;
}
//snprintf(hantar,sizeBuffS,"%c",opp);
}
return 0;
}
//strcpy(hantar,opp);
//////////////////////////////////////////////////////////////////////////////
//scanf("%s",hantar);