-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
110 lines (79 loc) · 2.27 KB
/
Server.cpp
File metadata and controls
110 lines (79 loc) · 2.27 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
#include <iostream>
#include <WS2tcpip.h> //Contains definitions introduced in winsock 2 and functions to receive IPs
//Also access network sockets
#include <string>
#include "ServerUtils.h"
#pragma comment (lib, "ws2_32.lib") //Linking libraries
using namespace std;
vector<studentPortal> allStudents;
int login_stat = 0;
int login_index;
void main()
{
//initialize Winsock
WSADATA wsData; //Contains information about the windows sockets implementation
WORD ver = MAKEWORD(2, 2); //The MAKEWORD(2,2) parameter of WSAStartup makes a request for
//version 2.2 of Winsock on the system, and sets the passed version as the highest version of Windows Sockets support that the caller can use
int wsok = WSAStartup(ver, &wsData); //allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation
if (wsok != 0)
{
cerr << "Can't Initialize winsock! Quitting" << endl;
return;
}
// Create a socket
SOCKET clientSocket = serverCreate();
//Features handling loop//
char buf[4096];
while (true)
{
ZeroMemory(buf, 4096);
// Wait for client to send data
int bytesReceived = recv(clientSocket, buf, 4096, 0);
if (bytesReceived == SOCKET_ERROR)
{
cerr << "Error in recv(), Trying to connect again" << endl;
clientSocket = serverCreate();
continue;
}
if (bytesReceived == 0)
{
cout << "Client disconnected " << endl;
clientSocket = serverCreate();
continue;
}
if (!strncmp(buf, "faculty", 6))
{
sendFaculty(clientSocket);
}
else if (!strncmp(buf, "searchfaculty", 13))
{
searchFaculty(clientSocket);
}
else if (!strncmp(buf, "help", 4)) //Changed here for sending emergency services
{
sendHelp_vect(clientSocket);
}
else if (!strncmp(buf, "Portal", 6))
{
sendPortal_vect(clientSocket);
}
else if (!strncmp(buf, "login", 5))
{
login_server(clientSocket , &login_stat , &login_index , allStudents);
}
else if (!strncmp(buf, "cafe", 4))
{
buyTickets(clientSocket, &login_index, allStudents);
}
else if (!strncmp(buf, "chat", 4))
{
chat(clientSocket);
}
else send(clientSocket, 0, 0, 0);
}
// Close the socket
closesocket(clientSocket);
// Cleanup winsock
WSACleanup();
system("pause");
}