使用 Poco C++ 库构建轻量级 HTTP 服务器

在现代 C++ 后端开发中,如果你需要构建一个轻量、高性能、可嵌入式的 HTTP 服务,而不愿引入重量级框架如 Boost.Beast 或 cpp-httplib,那么 Poco C++ Libraries 提供了一个优雅的解决方案。Poco 是一套广泛使用的 C++ 框架,涵盖网络、线程、文件系统、JSON/XML 等模块。

本篇文章将手把手教你如何使用 Poco 构建一个基础的 HTTP Server,并扩展为一个具备实际用途的请求处理器。


✨ 为什么选择 Poco?

  • 易于集成,无外部依赖(仅需 CMake + 编译 Poco)

  • 支持多线程、高并发

  • 内置 HTTPServer、WebSocket、REST 支持

  • 类似 Java Servlet 模型:请求/响应 + 工厂 + Handler

    cpp 复制代码
    my_http_server/
    ├── CMakeLists.txt
    ├── MyHttpApp.h
    ├── MyHttpApp.cpp
    ├── RequestHandlerFactory.h
    ├── RequestHandlerFactory.cpp
    └── main.cpp

    项目结构

    cpp 复制代码
    // RequestHandlerFactory.h
    #pragma once
    #include <Poco/Net/HTTPRequestHandlerFactory.h>
    #include <Poco/Net/HTTPRequestHandler.h>
    #include <Poco/Net/HTTPServerRequest.h>
    #include <Poco/Net/HTTPServerResponse.h>
    
    class RequestHandler : public Poco::Net::HTTPRequestHandler {
    public:
        void handleRequest(Poco::Net::HTTPServerRequest& request,
                           Poco::Net::HTTPServerResponse& response) override;
    };
    
    class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory {
    public:
        Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest&) override;
    };
    cpp 复制代码
    // RequestHandlerFactory.cpp
    #include "RequestHandlerFactory.h"
    #include <iostream>
    
    void RequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request,
                                       Poco::Net::HTTPServerResponse& response) {
        response.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
        response.setContentType("text/plain");
    
        std::ostream& ostr = response.send();
        ostr << "Hello from Poco ServerApplication HTTP Server!\n";
    }
    
    Poco::Net::HTTPRequestHandler* RequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest&) {
        return new RequestHandler;
    }
    cpp 复制代码
    #pragma once
    #include <Poco/Util/ServerApplication.h>
    #include <Poco/Net/HTTPServer.h>
    #include <Poco/Net/HTTPServerParams.h>
    #include <Poco/Net/ServerSocket.h>
    
    class MyHttpApp : public Poco::Util::ServerApplication {
    protected:
        int main(const std::vector<std::string>& args) override;
    };
    cpp 复制代码
    #include "MyHttpApp.h"
    #include "RequestHandlerFactory.h"
    #include <iostream>
    
    int MyHttpApp::main(const std::vector<std::string>& args) {
        Poco::UInt16 port = 8080;
        Poco::Net::ServerSocket svs(port);
    
        Poco::Net::HTTPServerParams* params = new Poco::Net::HTTPServerParams;
        params->setMaxQueued(100);
        params->setMaxThreads(8);
    
        Poco::Net::HTTPServer server(new RequestHandlerFactory, svs, params);
        server.start();
        std::cout << "HTTP server started on port " << port << std::endl;
    
        waitForTerminationRequest();  // 等待 Ctrl+C 或 kill 信号
        std::cout << "Shutting down..." << std::endl;
        server.stop();
    
        return Application::EXIT_OK;
    }
    cpp 复制代码
    //main.cpp
    #include "MyHttpApp.h"
    
    int main(int argc, char** argv) {
        MyHttpApp app;
        return app.run(argc, argv);
    }
    cpp 复制代码
    #CMakeLists.txt
    cmake_minimum_required(VERSION 3.10)
    project(MyPocoHttpServer)
    
    find_package(Poco REQUIRED Net Util Foundation)
    
    add_executable(server
        main.cpp
        MyHttpApp.cpp
        RequestHandlerFactory.cpp
    )
    
    target_link_libraries(server
        Poco::Net
        Poco::Util
        Poco::Foundation
    )

    构建和运行

    bash 复制代码
    mkdir build && cd build
    cmake ..
    make
    ./server
相关推荐
CC__xy20 分钟前
04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
开发语言·javascript·harmonyos·鸿蒙
青草地溪水旁27 分钟前
UML函数原型中stereotype的含义,有啥用?
c++·uml
萤丰信息31 分钟前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
青草地溪水旁33 分钟前
UML函数原型中guard的含义,有啥用?
c++·uml
Pocker_Spades_A1 小时前
飞算JavaAI家庭记账系统:从收支记录到财务分析的全流程管理方案
java·开发语言
CHEN5_022 小时前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o2 小时前
C语言基础:变量与进制详解
java·c语言·开发语言
nightunderblackcat3 小时前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
光头闪亮亮3 小时前
C++凡人修仙法典 - 宗门版-上
c++
光头闪亮亮3 小时前
C++凡人修仙法典 - 宗门版-下
c++