views:

99

answers:

2

I am implementing a server that sends xml to clients using boost. The problem I am facing is that the buffer doesn't get sent immediately and accumulates to a point then sends the whole thing. This cause a problem on my client side, when it parses the xml, it may have incomplete xml tag (incomplete message). Is there a way in boost to flush out the socket whenever it needs to send out a message? Below is server's write code.

void 
ClientConnection::handle_write(const boost::system::error_code& error)
{

    if (!error)
    {

        m_push_message_queue.pop_front ();
        if (!m_push_message_queue.empty () && !m_disconnected)
        {

             boost::asio::async_write(m_socket,
            boost::asio::buffer(m_push_message_queue.front().data(), 
                        m_push_message_queue.front().length()),
                boost::bind(&ClientConnection::handle_write, this,
                boost::asio::placeholders::error));
        }
    }
    else
    {
      std::err << "Error writting out message...\n";
      m_disconnected = true;
      m_server->DisconnectedClient (this);

    }
}
A: 

Typically when creating applications using TCP byte streams the sender sends a fixed length header so the receiver knows how many bytes to expect. Then the receiver reads that many bytes and parses the resulting buffer into an XML object.

Sam Miller
A: 

I assume you are using TCP connection. TCP is stream type, so you can't assume your packet will come in one big packet. You need to fix your communication design, by sending size length first like San Miller answer, or sending flag or delimiter after all xml data has been sent.

flamemyst