-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
219 lines (168 loc) · 4.82 KB
/
server.cpp
File metadata and controls
219 lines (168 loc) · 4.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
#include "server.h"
#include <fstream>
#include <iostream>
//This are the headers for winsock
#include <sstream>
#include <string>
#include <winsock2.h>
#include <winuser.h>
#include <ws2tcpip.h>
//#include <shellapi.h>
server::server()
{
//This would hide the cursor for looking better (Optional)
hideCursor();
//This would show the port number to copy
port_view();
// The winsock is initialised here
init_winsock();
//This is a wrapper for the getaddr function
init_getaddrinfo();
//This would create the socket
init_socket();
//This function is called to bind the socket
bind_socket();
//This would start listening the socket
listen_socket();
//This would open the port on the browser
port_open();
//This would accept the connection
accept_connection();
//This send the data which is a HTML file with the HTTP request
send_data();
//This would show the cursor (Optional)
showCursor();
}
//This would clean up everything upon deletion of the server object
server::~server()
{
freeaddrinfo(result);
closesocket(listen_sock);
closesocket(client_sock);
WSACleanup();
}
int server::init_winsock()
{
int init_res;
//WSAStartup function is called to initiate use of WS2_32.dll
//It also returns a integer value and check for errors
init_res = WSAStartup(MAKEWORD(2,2), &wsaData);
if(init_res !=0)
{
std::cout<<"WSAStartup failed: "<< init_res<<std::endl;
return 1;
}
std::cout<<"WSAStartup Passed"<<std::endl;
return 1;
}
//This is the wrapper for getaddrinfo function
int server::init_getaddrinfo()
{
ZeroMemory(&hints,sizeof(hints));
hints.ai_family = AF_INET; //This is for IPv4 address family
hints.ai_protocol = IPPROTO_TCP; //This is for TCP protocol
hints.ai_flags = AI_PASSIVE; //
//Resolve the local address and port to be used by the server
// The getaddrinfo function provides protocol independent translation from an ANSI host name to an address
int init_res = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
///This is for fault tolerence
if(init_res !=0)
{
std::cout<<"getaddrinfo failed: "<< init_res<<std::endl;
WSACleanup();
return 1;
}
std::cout<< "getaddrinfo successful"<<std::endl;
return 0;
}
int server::init_socket()
{
//The socket function is called to create a socket that is bound to a specific transport service provider
listen_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
//This is for fault tolerence
if (listen_sock == INVALID_SOCKET)
{
std::cout<<"Error at socket(): " << WSAGetLastError()<<std::endl;
freeaddrinfo(result);
WSACleanup();
return 1;
}
std::cout<<"Socket Initialised Successfully"<<std::endl;
return 0;
}
//This is a wrapper for the bind function
int server::bind_socket()
{
//Setting up the TCP listening socket
int init_res = bind(listen_sock, result->ai_addr,(int)result->ai_addrlen);
//This is for fault tolerance
if(init_res == SOCKET_ERROR)
{
std::cout<<"Bind failed with error: "<< WSAGetLastError()<<std::endl;
freeaddrinfo(result);
closesocket(listen_sock);
WSACleanup();
return 1;
}
std::cout<< "Binding Successful"<<std::endl;
//Frees up the result pointer
freeaddrinfo(result);
return 0;
}
int server::listen_socket()
{
//This listen function will listen to the socket created
if( listen( listen_sock, SOMAXCONN) == SOCKET_ERROR)
{
std::cout<< "Listen failed with error: "<<WSAGetLastError()<<std::endl;
closesocket(listen_sock);
WSACleanup();
return 1;
}
std::cout<<"Listening..."<<std::endl;
//loading_animation() would replace the static output
//This is buggy as of now
//loading_animation();
return 0;
}
//This would accpet the connection
int server::accept_connection()
{
//Accept a client sock
client_sock = accept(listen_sock, NULL, NULL);
//This would create a fault tolerance for accepting the connection
if(client_sock == INVALID_SOCKET)
{
std::cout<< "Accept Failed: "<< WSAGetLastError()<<std::endl;
closesocket(listen_sock);
WSACleanup();
return 1;
}
std::cout<< "Connection Accepted!" <<std::endl;
return 0;
}
int server::send_data()
{
//This block would read content from the html file
std::ifstream file("index.html");
std::stringstream buffer;
buffer<<file.rdbuf();
std::string content = buffer.str();
//Construct HTTP response with the HTML page
std::string http_response = "HTTP/1.1 200 OK\r\n";
http_response += "Content-Type: text/html\r\n";
http_response += "Content-Length: " + std::to_string(content.size())+ "\r\n\r\n";
http_response += content;
//This sends the data to the client
int send_result = send(client_sock, http_response.c_str(), http_response.size(),0);
//Creates Fault tolerence for Sending Data
if(send_result == SOCKET_ERROR)
{
std::cout<<"Send Failed: "<< WSAGetLastError()<<std::endl;
closesocket(client_sock);
WSACleanup();
return 1;
}
std::cout<< "Sending Successful!"<<std::endl;
return 0;
}