Boost.Asio和Boost的联系和区别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Boost.Asio和Boost的联系和区别相关的知识,希望对你有一定的参考价值。
参考技术A namespace// strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行.
// io_service不能保证线程安全
boost::asio::io_service m_service;
boost::asio::strand m_ www.hbbz08.com strand(m_service);
boost::mutex m_mutex;
void print(int id)
// boost::mutex::scoped_lock lock(m_mutex);
static int count = 0;
PRINT_DEBUG("id: " << boost::lexical_cast<std::string>(id));
PRINT_DEBUG("count: " << boost::lexical_cast<std::string>(++count));
void ioRun1()
while(true)
m_service.run();
void ioRun2()
while(true)
m_service.run();
void strand_print1()
// PRINT_DEBUG("Enter print1");
m_strand.dispatch(boost::bind(print, 1));
// PRINT_DEBUG("Exit print1");
void strand_print2()
// PRINT_DEBUG("Enter print2");
m_strand.post(boost::bind(print, 2));
// PRINT_DEBUG("Exit print2");
void strand_print3()
// PRINT_DEBUG("Enter print3");
m_strand.post(boost::bind(print, 3));
// PRINT_DEBUG("Exit print3");
void strand_print4()
// PRINT_DEBUG("Enter print4");
m_strand.post(boost::bind(print, 4));
// PRINT_DEBUG("Exit print4");
// 将上面的m_strand换成m_service后,
void service_print1()
// PRINT_DEBUG("Enter print1");
m_service.dispatch(boost::bind(print, 1));
// PRINT_DEBUG("Exit print1");
void service_print2()
// PRINT_DEBUG("Enter print2");
m_service.post(boost::bind(print, 2));
// PRINT_DEBUG("Exit print2");
void service_print3()
// PRINT_DEBUG("Enter print3");
m_service.post(boost::bind(print, 3));
// PRINT_DEBUG("Exit print3");
void service_print4()
// PRINT_DEBUG("Enter print4");
m_service.post(boost::bind(print, 4));
// PRINT_DEBUG("Exit print4");
void test_strand()
boost::thread ios1(ioRun1);
boost::thread ios2(ioRun2);
boost::thread t1(strand_print1);
boost::thread t2(strand_print2);
boost::thread t3(strand_print3);
boost::thread t4(strand_print4);
t1.join();
t2.join();
t3.join();
t4.join();
m_server.run();
void test_service()
boost::thread ios1(ioRun1);
boost::thread ios2(ioRun2);
boost::thread t1(service_print1);
boost::thread t2(service_print2);
boost::thread t3(service_print3);
boost::thread t4(service_print4);
t1.join();
t2.join();
t3.join();
t4.join();
m_service.run();
test_strand的执行结果:
[cpp] view plain copy print?
2013-01-05 17:25:34 626 [8228] DEBUG - id: 4
2013-01-05 17:25:34 631 [8228] DEBUG - count: 1
2013-01-05 17:25:34 634 [5692] DEBUG - id: 1
2013-01-05 17:25:34 637 [5692] DEBUG - count: 2
2013-01-05 17:25:34 640 [5692] DEBUG - id: 2
2013-01-05 17:25:34 642 [5692] DEBUG - count: 3
2013-01-05 17:25:34 646 [5692] DEBUG - id: 3
2013-01-05 17:25:34 649 [5692] DEBUG - count: 4
test_ioserivice的执行结果:
[cpp] view plain copy print?
2013-01-05 17:26:28 071 [3236] DEBUG - id: 1
2013-01-05 17:26:28 071 [5768] DEBUG - id: 2
2013-01-05 17:26:28 071 [5108] DEBUG - id: 3
2013-01-05 17:26:28 076 [3236] DEBUG - cou
C++ boost::asio https 通过代理
【中文标题】C++ boost::asio https 通过代理【英文标题】:C++ boost::asio https through proxy 【发布时间】:2017-08-10 15:13:50 【问题描述】:所以我有这段代码可以很好地使用 boost::asio 连接 ssl:
打开了 Fiddler,但我根本看不到这些联系。 从我读到的内容中,我收集到我必须通过代理传递请求。 所以我尝试了所有我能找到的示例和答案,但我无法让它发挥作用。
我得到的最好的结果是握手从未触发回调。
我错过了什么?
void performHTTPRequest(std::string host, std::string path)
std::ostream request_stream(&mRequest);
request_stream << "GET " << path << " HTTP/1.1\r\n";
request_stream << "Host: " << host << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
tcp::resolver::query query(host, "https");
mResolver.async_resolve(query,
boost::bind(handle_resolve,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
void handle_resolve(const boost::system::error_code& err,
tcp::resolver::iterator endpoint_iterator)
if (!err)
boost::asio::async_connect(mSocket.lowest_layer(),
endpoint_iterator,
boost::bind(&handle_connect, this,
boost::asio::placeholders::error));
else
delete this;
void handle_connect(const boost::system::error_code& err)
if (!err)
mSocket.async_handshake(
boost::asio::ssl::stream_base::handshake_type::client,
boost::bind(&handle_handshake, this,
boost::asio::placeholders::error));
else
delete this;
void handle_handshake(const boost::system::error_code& err)
if (!err)
boost::asio::async_write(mSocket, mRequest,
boost::bind(&handle_write_request, this,
boost::asio::placeholders::error));
else
delete this;
void handle_write_request(const boost::system::error_code& err)
if (!err)
boost::asio::async_read_until(mSocket, mResponse, "\r\n",
boost::bind(&handle_read_status_line, this,
boost::asio::placeholders::error));
else
delete this;
void handle_read_status_line(const boost::system::error_code& err)
if (!err)
std::istream response_stream(&mResponse);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
return;
if (status_code != 200)
return;
else
delete this;
【问题讨论】:
你在任何时候都会调用 ioservice::run() 吗?只是问一下,因为这是一个常见的错误。 在每个处理程序中放置一些调试消息,并让我们知道您的进展情况,尤其是主机解析到的端点或 IP 地址。但是,既然您说 Fiddler 没有显示任何内容,我的猜测是连接也失败了。此外,从等式中删除 Fiddler 并改用 Wireshark/tcpdump 进行测试。 【参考方案1】:看来只是改变了这一行:
tcp::resolver::query query(host, "https");
有了这个:
tcp::resolver::query query(proxyUrl, proxyPort);
成功了。
问题在于不允许 HTTPS 通信的代理配置。
【讨论】:
供将来参考:async_resolve()
重载采用 query
已被弃用并替换为 2 个参数(url、端口)。 boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/…以上是关于Boost.Asio和Boost的联系和区别的主要内容,如果未能解决你的问题,请参考以下文章
Qt 事件处理程序和 boost ASIO 的 io_service 有啥区别?