-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.hpp
More file actions
64 lines (60 loc) · 1.84 KB
/
WebServer.hpp
File metadata and controls
64 lines (60 loc) · 1.84 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
//
// Web Server
// 1/5/2026
//
// Web Server C++ Library
// WebServer.hpp
//
// Mohamed Abutaleb
//
#pragma once
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include "sockets/ListeningSocket.hpp"
namespace WS
{
class WebServer
{
private:
ListeningSocket listener;
int port;
public:
explicit WebServer(int p)
: listener(AF_INET, SOCK_STREAM, 0, p, INADDR_ANY, 10), port(p) {}
void start()
{
listener.start_listening();
std::cout << "[TCP] Listening on port " << port << "\n";
while (true)
{
sockaddr_in client_addr{};
int client_fd = listener.accept_connection(client_addr);
std::cout << "[TCP] Client connected: "
<< inet_ntoa(client_addr.sin_addr)
<< ":" << ntohs(client_addr.sin_port) << "\n";
while (true)
{
char buffer[1024];
std::memset(buffer, 0, sizeof(buffer));
int bytes_read = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
if (bytes_read == 0)
{
std::cout << "[TCP] Client Disconnected\n";
break;
}
if (bytes_read > 0)
{
std::cout << "[TCP] Received (" << bytes_read << " bytes):\n";
std::cout << buffer << "\n";
}
// Raw TCP reply (can be plain text)
const char *reply = "TCP server received your message.\n";
send(client_fd, reply, std::strlen(reply), 0);
}
close(client_fd);
}
}
};
}