博客
关于我
Linux:使用cpp-httplib创建一个简单的服务器
阅读量:332 次
发布时间:2019-03-04

本文共 824 字,大约阅读时间需要 2 分钟。

这里写目录标题

cpp-httplib

cpp-httplib是一个header-only的跨平台HTTP/HTTPS服务器和客户端C++开源库,可以使用它来完成服务端和客户端,我们使用的时候只需要包含“httplib.h”这个头文件就可以了,非常方便。

在Linux中使用cpp-httplib库时,只需要把它下载下来,然后在写代码时包含该头文件就可以了。

下载步骤

我们在可以进入gitee直接搜索cpp-httplib,点击第一个

在这里插入图片描述
在这里插入图片描述
也可以直接点击我给大家找到的连接直接进入
在这里插入图片描述
clone或者下载zip都可以(我们主要用到的时httplib.h),下载zip的话需要把httplib.h解压出来。
下面我们就用这个库来创建一个服务器:
(1).把httpib.h复制到项目文件下
在这里插入图片描述
(2)编写一个服务端代码(包含头文件)
在这里插入图片描述

#include 
#include
#include "httplib.h"using namespace httplib;void http_callback(const Request& req, Response& resp){ (void)req; std::string body = "linux so easy"; resp.set_content(body.c_str(),body.size(),"text/html");}int main(){ Server svr; svr.Get("/aaa",http_callback); svr.listen("0.0.0.0",19998); return 0;}

注意,编译的时候要加上-lpthread,有的还要加上-std=c++11

在这里插入图片描述
运行,用浏览器访问:
在这里插入图片描述
最后给大家分享两个连接,一个是一个大佬的博客,里面对httplib.h里的函数做出了解释,另一个对里面的一些函数的例子演示。

转载地址:http://idmq.baihongyu.com/

你可能感兴趣的文章
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>