[英]boost::socket receives only one message
I am writing simple server using boost::asio
. 我正在使用
boost::asio
编写简单的服务器。 Currently I have ready part of the server that receives data. 目前,我已经准备好接收数据的服务器部分。 But I have problem.
但是我有问题。
Boost::asio
receives only one message (my client program sends multiple number of messages). Boost::asio
仅接收一条消息(我的客户端程序发送了多条消息)。
After receiving message I must do something to be able to receive another message from the same client? 收到消息后,我必须做些什么才能能够接收来自同一客户端的另一条消息?
TCPConnection::TCPConnection(boost::asio::io_service& io_service)
: socket_(io_service),
socketActive(true)
{
Traces() << "\n" << "LOG: TCPConnection::TCPConnection(boost::asio::io_service& io_service) : socket_(io_service)";
}
void TCPConnection::Start()
{
Traces() << "\n" << "LOG: void TCPConnection::Start()";
socket_.async_read_some(boost::asio::buffer(buffer_),
boost::bind(&TCPConnection::HandleRead, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void TCPConnection::HandleRead(const boost::system::error_code& e,
std::size_t bytes_transferred)
{
Traces() << "\n" << "LOG: void TCPConnection::HandleRead(const boost::system::error_code& e, std::size_t bytes_transferred)";
if (!e)
{
Traces() << "\n" << "LOG: New message";
Message tempMessage;
tempMessage.CopyData((boost::shared_ptr<TCPConnection>)this, buffer_.data());
messageQueue->PushBack(tempMessage);
}
else if (e != boost::asio::error::operation_aborted)
{
Traces() << "\n" << "LOG: Close connection";
this->Stop();
}
}
Every time you successfully read data, you need to request the io_service
/ socket
to once again handle a data read. 每次成功读取数据时,都需要请求
io_service
/ socket
再次处理读取的数据。 My usual solution looks like this: 我通常的解决方案如下所示:
void handle_read(asio::error_code ec, size_t bytes_read) {
if(!ec) {
/*Do Stuff*/
socket.async_read(/*buffer*/, handle_read);
} else {
/*Error handling*/
}
}
I haven't syntax checked the code I'm providing here, but you should get the right idea. 我没有语法检查我在这里提供的代码,但是您应该有正确的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.