Posted on 2013-07-04 17:28
kongkongzi 阅读(5519)
评论(0) 编辑 收藏 引用 所属分类:
c++ network library
cpp-netlib:
参考:
1.
http://cpp-netlib.org/
解析版本: cpp-netlib-0.9.4
注:使用的boost版本为boost_1_53_0。 若使用boost_1_46_1在Windows上编译会出现错误。
cpp-netlib-0.9.4版本编译后:- Linux:
libcppnetlib-client-connections.a
libcppnetlib-server-parsers.a
libcppnetlib-uri.a
- Windows:H:\work\boost\cpp-netlib-0.9.4\libs\network\src\Release
cppnetlib-client-connections.lib
cppnetlib-server-parsers.lib
cppnetlib-uri.lib - Windows工程配置:
- include: H:/work/boost/boost_1_53_0;H:/work/boost/cpp-netlib-0.9.4;
- preprocessor: _SCL_SECURE_NO_WARNINGS; _WIN32_WINNT=0x0501;
测试代码testServer.cpp:
#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>
namespace http = boost::network::http;
struct hello_world;
typedef http::server<hello_world> server;
struct hello_world {
void operator() (server::request const &request,
server::response &response) {
std::string ip = source(request);
response = server::response::stock_reply(
server::response::ok, std::string("Hello, ") + ip + "!");
}
void log(const char* msg)
{
std::cout << msg << std::endl;
}
};
int
main(int argc, char * argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
return 1;
}
try {
hello_world handler;
server server_(argv[1], argv[2], handler);
server_.run();
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
图1:cppnetlib-listening
图2:cppnetlib-handleAccept
图3:cppnetlib-class
说明:
1. http::server与http::async_server的区别:
(1)http::async_server使用了多线程来处理每个连接上的请求,详细分析查看《cpp-netlib之async_server解析》:
http://www.cppblog.com/toMyself/archive/2013/07/15/201819.html。
thread_pool().post(boost::bind(&Handler::operator(), &handler, cref(request_), async_connection<Tag,Handler>::shared_from_this()))。