Skip to content

Create a HTTP Server

Configure a C++20 application using Installation. Start the server before running the matching client.

HTTP Echo Server

Use the arknet::arknet interface target in a C++20 application following the build guide. Build the server and client blocks as separate executables. Start the server first, then run the client in another terminal. Press Enter in the server terminal to stop it. Plain HTTP does not require TLS.

#include <iostream>
#include <utility>

#include <arknet/http/http_server.hpp>

int main()
{
    arknet::http_server server;
    server.bind_recv([](auto& session, arknet::http::request<arknet::http::string_body>& request)
    {
        arknet::http::response<arknet::http::string_body> response(
            arknet::http::status::ok, request.version());
        response.keep_alive(request.keep_alive());
        response.set(arknet::http::field::content_type, "text/plain");
        response.body() = request.body();
        response.prepare_payload();
        session->async_send(std::move(response));
    });
    if (!server.start("127.0.0.1", 8080))
    {
        std::cerr << arknet::get_last_error().message() << '\n';
        return 1;
    }
    std::cout << "HTTP listening on http://127.0.0.1:8080/echo\n";
    std::cin.get();
    server.stop();
}

Configuration and advanced usage · Performance

Navigation

Type to search…

↑↓ navigate↵ selectEsc close