博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 网络库 cpp-netlib的简单使用
阅读量:3577 次
发布时间:2019-05-20

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

详情还请移步至个人博客

示例代码 (C++11, cpp-netlib版本:0.12.0)

需要链接库文件

target_link_libraries(app    boost_system    network-uri    cppnetlib-server-parsers    )
#include 
#include
#include
#include
namespace http = boost::network::http;using json = nlohmann::json;struct Controller;using SimpleHTTPServer = http::server
;struct Controller {
void operator() (SimpleHTTPServer::request const &request, SimpleHTTPServer::connection_ptr response) {
if( request.method == "GET") {
response->set_status(SimpleHTTPServer::connection::ok); response->set_headers(std::map
{ { "Content-Type", "application/json;charset=utf-8"} }); response->write(json{ { "say", "Who are you?"} }.dump(2)); } SSPD_LOG_INFO << "[REQ] Source: " << request.source; /// client info:
:
SSPD_LOG_INFO << "[REQ] Destination: " << request.destination; /// path: /index?name=sb SSPD_LOG_INFO << "[REQ] Body: " << request.body; /// Body data: {"data": [0, 0]} SSPD_LOG_INFO << "[REQ] Version: " /// HTTP version << static_cast
(request.http_version_major) << '.' << static_cast
(request.http_version_minor); network::uri uri("http://localhost:9090" + request.destination); SSPD_LOG_INFO << "[URI] Host: " << uri.host(); SSPD_LOG_INFO << "[URI] Port: " << uri.port
(); SSPD_LOG_INFO << "[URI] Query: " << uri.query(); SSPD_LOG_INFO << "[URI] Route: " << uri.path(); } void log(SimpleHTTPServer::string_type const &info) { std::cerr << "ERROR: " << info << '\n'; }};int main(int,char**){ Controller control{ }; SimpleHTTPServer::options options(control); SimpleHTTPServer server(options.address("0.0.0.0").port("9090")); server.run(); return 0;}

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

你可能感兴趣的文章
Java中时间戳和时间格式的转换
查看>>
Dubbo基础知识整理
查看>>
计算机网络知识整理
查看>>
Java基础知识
查看>>
操作系统知识整理
查看>>
实现自己的权限管理系统(二):环境配置以及遇到的坑
查看>>
实现自己的权限管理系统(四): 异常处理
查看>>
实现自己的权限管理系统(十):角色模块
查看>>
实现自己的权限管理系统(十二):权限操作记录
查看>>
实现自己的权限管理系统(十三):redis做缓存
查看>>
实现自己的权限管理系统(十四):工具类
查看>>
JavaWeb面经(二):2019.9.16 Synchronized关键字底层原理及作用
查看>>
牛客的AI模拟面试(1)
查看>>
深入浅出MyBatis:MyBatis解析和运行原理
查看>>
Mybatis与Ibatis
查看>>
字节码文件(Class文件)
查看>>
java中的IO流(一)----概述
查看>>
StringBuilder
查看>>
集合,Collection
查看>>
泛型详解
查看>>