Initial commit

This commit is contained in:
2025-09-08 09:41:16 +03:00
commit 418d33a6de
5 changed files with 90 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build/

22
CMakeLists.txt Normal file
View File

@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.15...4.0)
project(SimpleHttpServer VERSION 1.0
DESCRIPTION "A simple HTTP server written in C++"
LANGUAGES CXX)
add_library(
httpserverlib
STATIC
src/server.hpp
src/server.cpp
)
target_compile_features(httpserverlib PUBLIC cxx_std_23)
add_executable(
httpserver
app/main.cpp
)
target_link_libraries(httpserver PUBLIC httpserverlib)

8
app/main.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include <print>
#include "../src/server.hpp"
int main() {
SimpleHttpServer server;
server.start_listening(8080);
std::println("Bye!");
}

34
src/server.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "server.hpp"
SimpleHttpServer::SimpleHttpServer() {
}
SimpleHttpServer::~SimpleHttpServer() {
}
void SimpleHttpServer::start_listening(int port) {
std::println("Starting server on port {0}...", port);
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(port);
serverAddress.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
listen(serverSocket, 5);
std::println("Listening...");
int clientSocket = accept(serverSocket, nullptr, nullptr);
char buffer[1024] = {0};
recv(clientSocket, buffer, sizeof(buffer), 0);
std::println("Received: {0}", buffer);
std::println("Closing...");
close(serverSocket);
}

25
src/server.hpp Normal file
View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#include <print>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
class SimpleHttpServer {
public:
SimpleHttpServer();
SimpleHttpServer(SimpleHttpServer &&) = default;
SimpleHttpServer(const SimpleHttpServer &) = default;
SimpleHttpServer &operator=(SimpleHttpServer &&) = default;
SimpleHttpServer &operator=(const SimpleHttpServer &) = default;
~SimpleHttpServer();
void start_listening(int port);
private:
};