C++下最简单的Http服务器

本文采用的实现方式是使用微软的CPPREST SDK方式实现,关于这个库的安装可以参考我的另一篇文章,地址是:https://blog.csdn.net/youyicc/article/details/105971772。
闲话不多说,直接上代码,首先用VS创建一个空白程序,添加一个main.cpp的文件,里面代码如下:

#include <thread>
#include "CommandHandler.h"

//这是一个单独的线程-为了不影响主线程
void InitHttpServer()
{
	try {
		//监听9092端口
		utility::string_t address = U("http://*:9092");
		uri_builder uri(address);
		utility::string_t addr = uri.to_uri().to_string();
		CommandHandler handler(addr);
		handler.open().wait();
		while (true) {
			std::chrono::minutes dura(60);
			std::this_thread::sleep_for(dura);
		}
		handler.close().wait();
	}
	catch (std::exception& ex) {
		ucout << U("Exception: ") << ex.what() << std::endl;
	}
}
int main(int argc, const char** argv)
{
	//启动Http服务器线程
	std::thread thrd(InitHttpServer);
	thrd.detach();
	//主线程干其他事情,保证main函数不会退出即可
	while(1){
		std::chrono::seconds dura(600);
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值