基于cpp-httplib库与多线程实现的http请求

文章介绍了cpp-httplib,这是一个简单的C++HTTP服务器库,只需包含一个头文件即可使用。它提供了创建HTTP服务的功能,包括设置超时和连接保持,以及处理GET请求的示例代码。在示例中,httpService类创建了一个工作线程来监听和响应请求,主函数需用while循环保持程序运行。另一个版本增加了对多个请求路径的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、cpp-httplib官网,使用只需要包含一个头文件即可

https://gitee.com/yuanfeng1897/cpp-httplib

2、示例,如下代码所示:

版本一:

#include "httplib.h"
#include <thread>
#include <unistd.h>
using namespace std;
using namespace httplib;
class httpService {
private:
  std::thread work_thread_;
  Server svr_;

public:
  httpService();
  ~httpService();

  void work();
  void start();
  void creatConnect();
  static void ev_handler(const Request &req, Response &http_res);
};

httpService::httpService() {
  int port = 1234;
  string host = "localhost";
  svr_.set_keep_alive_max_count(5);
  svr_.set_keep_alive_timeout(10);
  svr_.bind_to_port(host.c_str(), port);
}

httpService::~httpService() {}

void httpService::ev_handler(const Request &req, Response &http_res) {
  http_res.set_content("hello world", "text/html");
}
void httpService::work() {
  svr_.Get("/", this->ev_handler);
  svr_.listen_after_bind();
}

void httpService::start() {
  work_thread_ = std::thread(&httpService::work, this);
  work_thread_.detach();
}

int main() {
  httpService http_svr;
  http_svr.start();
  while (1)
  {
    /* code */
  }
  
  return 0;
}

注意:主函数必须加whiile(1)循环进行阻塞,否则类对象马上被析构,主线程的资源被销毁,子线程(也就是请求命令的线程)无法工作。

3、另外一个版本:

#include "httplib.h"
#include <thread>
#include <unistd.h>
using namespace std;
using namespace httplib;
class httpService {
private:
  std::thread work1_thread_;
  bool listening_;

public:
  Server svr_;

public:
  httpService();
  ~httpService();

  void work1();
  // void work2();
  void start();
  void stop();
  static void ev_handler1(const Request &req, Response &http_res);
  static void ev_handler2(const Request &req, Response &http_res);
};

httpService::httpService() {
  listening_ = false;
  int port = 1234;
  string host = "localhost";
  svr_.set_keep_alive_max_count(5);
  svr_.set_keep_alive_timeout(10);
  svr_.bind_to_port(host.c_str(), port);
}

httpService::~httpService() {}

void httpService::ev_handler1(const Request &req, Response &http_res) {
  http_res.set_content("path1", "text/html");
}
void httpService::ev_handler2(const Request &req, Response &http_res) {
  http_res.set_content("path2", "text/html");
}
void httpService::work1() {
  this->listening_ = true;
  svr_.Get("/path1", this->ev_handler1);
  svr_.Get("/path2", this->ev_handler2);
  svr_.listen_after_bind();
}

void httpService::start() {
  work1_thread_ = std::thread(&httpService::work1, this);
  work1_thread_.detach();
}

void httpService::stop() { this->listening_ = false; }

int main() {
  httpService http_svr;
  http_svr.start();
  cout << "hello world!" << endl;
  while (1) {
    /* code */
  }

  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值