-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPServer.cpp
82 lines (61 loc) · 2.13 KB
/
HTTPServer.cpp
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
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <string>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
void Error(const char* msg) {
cerr << msg << endl;
exit(1);
}
void HandleClient(SOCKET ClientSocket) {
char Buffer[2048];
int BytesReceived;
BytesReceived = recv(ClientSocket, Buffer, sizeof(Buffer), 0);
if (BytesReceived < 0) {
cerr << "Error receiving request." << endl;
return;
}
Buffer[BytesReceived] = '\0';
cout << "Request : \n" << Buffer << endl;
string Response = "HTTP/1.1 200 OK\r\n";
Response += "Content-Type : text/html; charset=UTF-8\r\n";
Response += "Connection : close\r\n\r\n";
Response += "<html><body><h1>This is a Simple HTTP Response</h1></body></html>";
send(ClientSocket, Response.c_str(), Response.length(), 0);
closesocket(ClientSocket);
}
int main() {
WSADATA wsaData;
SOCKET ServerSocket, ClientSocket;
sockaddr_in ServerAddress, ClientAddress;
int ClientAddressSize = sizeof(ClientAddress);
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
Error("WSAStartup Failed!");
}
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ServerSocket == INVALID_SOCKET) {
Error("Socket Creation Failed!");
}
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_addr.s_addr = INADDR_ANY;
ServerAddress.sin_port = htons(8080);
if (bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) == SOCKET_ERROR) {
Error("Binding Failed!");
}
if (listen(ServerSocket, 5) == SOCKET_ERROR) {
Error("Listener Failed!");
}
cout << "Server listening on port 8080..." << endl;
while (true) {
ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddress, &ClientAddressSize);
if (ClientSocket == INVALID_SOCKET) {
Error("Accept Failed!");
}
cout << "Client Connected" << endl;
HandleClient(ClientSocket);
}
closesocket(ServerSocket);
WSACleanup();
return 0;
}