boost :: asio :: async_write没有正确写入客户端,奇怪的行为

时间:2011-05-18 21:03:20

标签: boost boost-asio

我编写了一个接受N个客户端连接然后将数据写入其中的程序。我现在遇到的问题是:我只能写入N-1个客户端,第一个客户端从未写入。我不知道为什么会这样,所以我希望你们中的一些人能够提供一些帮助。

我提供了可能与此问题相关的代码部分:

void ClientPartitionServer::AcceptClientConnections(int port) {
  cout << "Listening to connections..." << endl;
  cout << "Number of PartitionInstanceConnections: " <<
    m_partitionInstanceConnections.size() << endl;
  m_acceptor = new boost::asio::ip::tcp::acceptor(m_IOService);
  m_endpoint = new boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
    m_port);
  m_acceptor->open(m_endpoint->protocol());
  m_acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  m_acceptor->bind(*m_endpoint);
  m_acceptor->listen();
  boost::asio::ip::tcp::socket* acceptingSocket =
    new boost::asio::ip::tcp::socket(m_IOService);
  m_acceptor->async_accept(*acceptingSocket, boost::bind(
    &ClientPartitionServer::HandleAccept, this, acceptingSocket,
    boost::asio::placeholders::error));
}
void ClientPartitionServer::HandleAccept(boost::asio::ip::tcp::socket* socket,
    const boost::system::error_code& error) {
  cout << "Connection established..." << endl;
  m_clientSockets.push_back(new boost::asio::ip::tcp::socket(m_IOService));
  cout << m_clientSockets.back()->is_open() << endl;
  ++m_clientSocketsCounter;
  cout << "ClientPartitionServer identifier: " << m_identifier << endl;
  cout << "Client connected on port:         " << m_port << endl;
  cout << "Number of clients on port:        " << m_clientSocketsCounter <<
    endl;
  m_acceptor->async_accept(*m_clientSockets.back(), boost::bind(
    &ClientPartitionServer::HandleAccept, this, m_clientSockets.back(),
    boost::asio::placeholders::error));
}

void ClientPartitionServer::HandleSignal(char* content, int transferSize,
    int identifier) {
  if(identifier == m_identifier) {
    TransferToQueueBuffer(content, transferSize);
    if(m_writeCompleteFlag) {
      TransferToWriteBuffer(m_queueBuffer, m_queueBufferSize);
      if(m_clientSockets.size() != 0) {
        for(vector<boost::asio::ip::tcp::socket*>::const_iterator i =
            m_clientSockets.begin(); i != m_clientSockets.end(); ++i) {
          WriteToClient(m_writeBuffer, m_queueBufferSize, *i);
        }
      }
    }
  }
}

void ClientPartitionServer::WriteToClient(char* content, int transferSize,
    boost::asio::ip::tcp::socket* clientSocket) {
  boost::lock_guard<boost::mutex> lock(m_writeToClientMutex);
  m_writeCompleteFlag = false;
  boost::asio::async_write(*clientSocket, boost::asio::buffer("ABC ",
    4), boost::bind(&ClientPartitionServer::HandleWrite,
      this, boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void ClientPartitionServer::HandleWrite(const boost::system::error_code& ec,
    size_t bytes_transferred) {
  cout << "handlewrite" << endl;
  m_writeCompleteFlag = true;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

第一个async_accept()acceptingSocket上被调用,newAcceptClientConnections()内被泄露。

后续的async_accept()会在new HandleAccept()以及push_back()加入m_clientSockets的套接字上调用。

WriteToClient()仅在m_clientSockets中找到的套接字上执行,永远不会在第一个套接字上执行。

解决方案:将AcceptClientConnections()中的第一个套接字push_back转换为m_clientSockets