-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
538 lines (420 loc) · 10.7 KB
/
functions.cpp
File metadata and controls
538 lines (420 loc) · 10.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
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
#include"ServerUtils.h"
SOCKET serverCreate()
{
//Create a sock
/*
Socket just plugs a port and an IP
*/
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
cerr << "Can't create a socket! Quitting" << endl;
return (SOCKET)-1;
}
//Bind the socket to an ip address and port
/*
Port and ip to a socket
*/
// Bind the ip address and port to a socket
sockaddr_in hint;//Specifies transport and port for the AF_INET
hint.sin_family = AF_INET;
hint.sin_port = htons(54000);//Host to Network short
hint.sin_addr.S_un.S_addr = 0; //ALternative = inet_pton
bind(listening, (sockaddr*)&hint, sizeof(hint)); //Binding done
// Tell Winsock the socket is for listening
listen(listening, SOMAXCONN); //SOMAXCONN is the maximum number in "listen"
// Wait for a connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);
char host[NI_MAXHOST]; // Client's remote name
char service[NI_MAXSERV]; // Service (i.e. port) the client is connect on
ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
cout << host << " connected on port " << service << endl;
}
else
{
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); //Address to string translation
cout << host << " connected on port " <<
ntohs(client.sin_port) << endl;
}
// Close listening socket
closesocket(listening);
return clientSocket;
}
bool facultyDeptComp(const info a, const info b)
{
int c = strcmp(a.department, b.department);
if (c < 0) return TRUE;
else return FALSE;
}
vector<string> substringer(char* str)
{
vector<string> parts;
char subs[5];
if (strlen(str) < 3)
{
ZeroMemory(subs, 5);
strncpy(subs, str, 2);
string cpp_sub(subs);
parts.push_back(cpp_sub);
}
else
{
for (int i = 0; i < strlen(str) - 2; i++)
{
ZeroMemory(subs, 5);
strncpy(subs, str + i, 3);
string cpp_sub(subs);
parts.push_back(cpp_sub);
}
}
return parts;
}
int facultyNameComp(char* match, char* toMatch)
{
string name(match);
string substring(toMatch);
vector<string> parts;
transform(name.begin(), name.end(), name.begin(), ::tolower);
transform(substring.begin(), substring.end(), substring.begin(), ::tolower);
parts = substringer(toMatch);
for (unsigned int i = 0; i < parts.size(); i++)
{
//cout << parts[i] << endl;
size_t f = name.find(parts[i]);
if (f != string::npos)
{
return TRUE;
}
}
return FALSE;
}
void sendFaculty(SOCKET sock)
{
FILE* fp = fopen(facultylist, "r+");
info faculty;
vector<info> facultyShomuho;
fseek(fp, 0, 0);
while (!feof(fp))
{
ZeroMemory(&faculty, sizeof(faculty));
fread(&faculty, sizeof(faculty), 1, fp);
if (faculty.phone_no == 0) break;
facultyShomuho.push_back(faculty);
}
std::sort(facultyShomuho.begin(), facultyShomuho.end(), facultyDeptComp);
ZeroMemory(&faculty, sizeof(faculty));
facultyShomuho.push_back(faculty);
cout << "Faculties shown :" << facultyShomuho.size() << endl;
for (unsigned int i = 0; i < facultyShomuho.size(); i++)
{
int sendbytes = send(sock, (char*)(&facultyShomuho[i]), sizeof(faculty), 0);
if (sendbytes == 0)
{
cerr << "Send failed . Client disconnected\n";
return;
}
}
fclose(fp);
return;
}
void searchFaculty(SOCKET sock)
{
FILE* fp = fopen(facultylist, "r+");
info faculty;
vector<info> facultyShomuho, foundFaculties;
char stringToFindBuf[128];
bool notfoundflag = TRUE;
int matchCountThreshold;
map<int, info> foundFacs;
ZeroMemory(stringToFindBuf, 128);
int bytesrecv = recv(sock, stringToFindBuf, 128, 0);
if (bytesrecv == 0)
{
cerr << "Client disconnected";
return;
}
fseek(fp, 0, 0);
while (!feof(fp))
{
ZeroMemory(&faculty, sizeof(faculty));
fread(&faculty, sizeof(faculty), 1, fp);
if (faculty.phone_no == 0) break;
facultyShomuho.push_back(faculty);
}
for (unsigned int i = 0; i < facultyShomuho.size(); i++)
{
if (facultyNameComp(facultyShomuho[i].name, stringToFindBuf) > 0)
{
foundFaculties.push_back(facultyShomuho[i]);
}
}
ZeroMemory(&faculty, sizeof(faculty));
foundFaculties.push_back(faculty);
cout << "Faculties shown :" << foundFaculties.size() << endl;
for (unsigned int i = 0; i < foundFaculties.size(); i++)
{
int sendbytes = send(sock, (char*)(&foundFaculties[i]), sizeof(faculty), 0);
if (sendbytes == 0)
{
cerr << "Send failed . Client disconnected\n";
return;
}
}
}
void sendHelp(SOCKET sock)
{
FILE* fp = fopen(emergencylist, "r+");
EmergencyServices service;
fseek(fp, 0, 0);
while (!feof(fp))
{
ZeroMemory(&service, sizeof(service));
fread(&service, sizeof(service), 1, fp);
int send_success = send(sock, (char*)&service, sizeof(service), 0);
if (send_success == SOCKET_ERROR)
{
cerr << "Client disconnected, try to connect again\n";
return;
}
}
fclose(fp);
return;
}
void sendHelp_vect(SOCKET sock)
{
FILE* fp = fopen(emergencylist, "r+");
EmergencyServices service;
vector<EmergencyServices> serviceShomuho;
fseek(fp, 0, 0);
while (!feof(fp))
{
//Enters all the services as a vector for ease of access
ZeroMemory(&service, sizeof(service));
fread(&service, sizeof(service), 1, fp);
if (service.contact == 0) break;
serviceShomuho.push_back(service);
}
//Skipping sort to avoid complications.
//Entering an empty or zero vector as a terminating condition.
ZeroMemory(&service, sizeof(service));
serviceShomuho.push_back(service);
//Vector manipulation done//
//Server console outputs:
cout << "Emergency Services shown :" << serviceShomuho.size() << endl;
for (unsigned int i = 0; i < serviceShomuho.size(); i++)
{
int sendbytes = send(sock, (char*)(&serviceShomuho[i]), sizeof(service), 0);
if (sendbytes == 0)
{
cerr << "Send failed . Client disconnected\n";
return;
}
}
fclose(fp);
return;
}
void sendPortal(SOCKET sock)
{
FILE* fp = fopen(StudentPortal, "r+");
studentPortal student;
fseek(fp, 0, 0);
while (!feof(fp))
{
ZeroMemory(&student, sizeof(student));
fread(&student, sizeof(student), 1, fp);
int send_success = send(sock, (char*)&student, sizeof(student), 0);
if (send_success == SOCKET_ERROR)
{
cerr << "Client disconnected, try to connect again\n";
return;
}
}
fclose(fp);
return;
}
void sendPortal_vect(SOCKET sock)
{
//FILE* fp = fopen(StudentPortal, "r+");
//studentPortal student;
//vector<studentPortal>studentShomuho;
//fseek(fp, 0, 0);
//while (!feof(fp))
//{
// //Enters all the services as a vector for ease of access
// ZeroMemory(&student, sizeof(student));
// fread(&student, sizeof(student), 1, fp);
// if (student.roll == 0) break;
// studentShomuho.push_back(student);
//}
//ZeroMemory(&student, sizeof(student));
//studentShomuho.push_back(student);
////Vector manipulation done//
////Server console outputs:
//cout << "Portals shown: " << studentShomuho.size() << endl;
//for (unsigned int i = 0; i < studentShomuho.size(); i++)
//{
// int sendbytes = send(sock, (char*)(&studentShomuho[i]), sizeof(student), 0);
// if (sendbytes == 0)
// {
// cerr << "Send failed . Client disconnected\n";
// return;
// }
//}
//fclose(fp);
return;
}
//Hash Function
unsigned long long Hash(const char* str)
{
unsigned long long x = 0;
for (int i = 0; str[i] != '\0'; i++)
{
x = str[i] + (x << 6) + (x << 16) - x; //or multiplying by 65599
//SDBM hash
}
return x;
}
void login_server(SOCKET sock, int* login_stat , int * login_index, vector<studentPortal> &allStudents)
{
FILE* fp = fopen(StudentPortal, "r+");
studentPortal student;
logininfo log;
char buf = 'S';
fseek(fp, 0, 0);
allStudents.clear();
while (!feof(fp))
{
//Enters all the services as a vector for ease of access
ZeroMemory(&student, sizeof(student));
fread(&student, sizeof(student), 1, fp);
std::cout << student.name << " >> "<<feof(fp)<<std::endl;
if (student.roll == 0) break;
allStudents.push_back(student);
}
ZeroMemory(&student, sizeof(student));
allStudents.push_back(student);
int recvbytes = recv(sock, (char*)&log, sizeof(logininfo), 0);
if (recvbytes == 0)
{
cout << "Login Failed";
return;
}
for (int i = 0; i < allStudents.size(); i++)
{
if (allStudents[i].hash == log.hash && allStudents[i].roll == log.id)
{
studentPortal loggedStud;
loggedStud = allStudents[i];
int s = send(sock, &buf, sizeof(buf), 0);
if (s == SOCKET_ERROR)
{
cout << "Send Failed\n";
}
int s2 = send(sock, (char *)&loggedStud, sizeof(studentPortal), 0);
if (s2 == SOCKET_ERROR)
{
cout << "Send Failed\n";
}
*login_index = i;
*login_stat = 1;
return;
}
}
buf = 'F';
int s = send(sock, &buf, sizeof(buf), 0);
if (s == SOCKET_ERROR)
{
cout << "Send Failed\n";
}
return;
}
void buyTickets(SOCKET sock, int* login_index, vector<studentPortal> &allStudents)
{
char buff;
int bytesRecv = recv(sock, &buff, sizeof(buff), 0);
if (bytesRecv == 0)
{
cerr << "Error buying tickets\n";
return;
}
if (buff == 'L')
{
if (allStudents[*login_index].balance - 70 >= 0)
{
cout << allStudents[*login_index].balance << endl;
allStudents[*login_index].balance -= 70;
rePrint(allStudents);
char response = 'Y';
send(sock, &response, sizeof(response), 0);
cout << allStudents[*login_index].balance << endl;
}
else
{
char response = 'N';
send(sock, &response, sizeof(response), 0);
}
}
else if (buff == 'S')
{
if (allStudents[*login_index].balance - 80 >= 0)
{
cout << allStudents[*login_index].balance << endl;
allStudents[*login_index].balance -= 80;
rePrint(allStudents);
char response = 'Y';
send(sock, &response, sizeof(response), 0);
cout << allStudents[*login_index].balance << endl;
}
else
{
char response = 'N';
send(sock, &response, sizeof(response), 0);
}
}
}
void rePrint(vector <studentPortal>& allStudents)
{
remove(StudentPortal);
FILE* fp = fopen(StudentPortal, "w");
studentPortal student;
fseek(fp, 0, 0);
for (unsigned int i = 0; i < allStudents.size(); i++)
{
student = allStudents[i];
fwrite(&student, sizeof(student), 1, fp);
}
ZeroMemory(&student, sizeof(student));
fwrite(&student, sizeof(student), 1, fp);
fclose(fp);
return;
}
void chat(SOCKET sock)
{
system("cls");
char buff[2000];
while (true)
{
int success = recv(sock, (char*)&buff, sizeof(buff), 0);
if (!strncmp(buff, "bye", 3)|| success<=0)
{
cerr << "User has terminated the chat!" << endl;
cout << "Going back ...\n";
Sleep(2000);
system("cls");
return;
}
cout << "Client >> ";
cout << buff << endl;
fflush(stdin);
cout << "Server >> ";
ZeroMemory(buff, 2000);
cin.getline(buff, sizeof(buff));
send(sock, (char*)&buff, sizeof(buff), 0);
}
return;
}