简体   繁体   English

boost :: socket只收到一条消息

[英]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.

相关问题 C++ 客户端套接字只接收消息的首字母 - C++ Client socket receives only first letter of message 在派生套接字上侦听时,只有一个进程接收UDP数据包是一种可靠的行为吗? - Is it a reliable behavior that only one process receives a UDP packet when listening on a forked socket? 只有一个套接字接收数据。 两个UDP服务器sockets绑定到同一个端口,Windows上的不同地址 - Only one socket receives data. Two UDP server sockets bound to same port, different addresses on Windows C++ 套接字只接收第一次发送? - C++ socket only receives first send? boost::asio UDP 广播客户端仅接收“快速”数据包 - boost::asio UDP Broadcast Client Only Receives “fast” Packets 升压套接字通信在一次交换后无法正常工作 - boost socket comms are not working past one exchange 提升asio打开更多的一个套接字 - boost asio open more that one socket 套接字[C ++ \\ Java] ServerSocket仅在客户端关闭时才接收字符串 - Socket [C++\Java] The ServerSocket receives a string only when the client closes boost :: system :: error_code :: message()使用boost :: asio socket抛出访问冲突异常 - boost::system::error_code::message() throwing access violation exception with boost::asio socket 为什么这个 Boost TCP 套接字以一种方法工作而不是另一种方法? - Why does this Boost TCP socket work in one method and not in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM