c++ - 带有 async_read 的 boost.asio 多线程 I/O 服务器

标签 c++ boost tcp boost-asio

基本逻辑如下

主线程:

for(;;) {
accept socket
async_read head(length of packet),binded to head_handler
io_service.run() }

head_handler(非线程):

async_read body(using length read from head) ,binded to body_handler

body_handler(非线程):

parse message into Request objects (custom class).
push Request to request_pool

读线程:

for(;;) {
if request_pool not empty (lock otherwise) { perform actions described by the Request } }

所以我有一个使用上述逻辑的简单回显服务器。它会打印出传入的任何消息。但是,只会显示第一条消息,所有其他消息都会正确打印出“xxx.xxx.xxx.xxx connected”消息。但不会打印任何消息

最佳答案

如果你不发出任何异步。 body_handler 中的操作,io_service 运行结束,io_service::run() 退出。在对 io_service::run() 的任何后续调用之前,您必须调用 io_service::reset() .

关于c++ - 带有 async_read 的 boost.asio 多线程 I/O 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914186/

相关文章:

c++ - Visual Studio 不能包含头文件。

c# - 将 C++ 字符串 vector 编码到 C#

c++ - 将 std::u8string 复制到 utf8 字符的 c 风格字符串中

c++ - boost: vector256.hpp: 没有这样的文件或目录

c++ - VS 2013 RC 中的 Boost Fusion 编译错误

java - 使用 Java 套接字发送文件,丢失数据

c++ - 命名构造函数和临时生命周期延长

c++ - 错误:使用 boost::serialization 与 'operator<<' 不匹配

java - 通过tcp进行aes加解密。使用填充密码解密时输入长度必须是16的倍数

c# - 与多个客户端的C#TCP/IP简单聊天