2011-05-18 83 views
1

我写了一个程序,它接受N个客户端连接,然后将数据写入它们。我现在面临的问题是:我只能写信给N-1客户,第一个客户从来没有写过。我不知道为什么会发生这种情况,所以我希望你们中的一些人能够提供一些帮助。boost :: asio :: async_write没有正确写入客户端,奇怪的行为

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

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; 
} 

谢谢你的任何援助。

+0

我的眼睛受到所有命名空间限定符的伤害**但是**'std :: cout'拼写为'cout' ... – sehe 2011-05-18 21:23:58

回答

1

第一个async_accept()被调用acceptingSocket这是new'd在AcceptClientConnections()并泄漏。

随后async_accept() s的要求是new倒是在HandleAccept()push_back()“插座编入m_clientSockets

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

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

+0

Cubbi!非常感谢你的回答,你节省了我的时间。 – czchlong 2011-05-19 13:55:52

相关问题