1

I want to print the message sent by the client. But in my function handleRead, when get the data from buffer and print it , nothing is print. I don't know how to do it.

void    Network::start()
{
    boost::asio::async_write(m_socket, boost::asio::buffer(m_message),
                             boost::bind(&Network::handleWrite,shared_from_this(),
                                         boost::asio::placeholders::error));
}

void    Network::handleWrite(const boost::system::error_code &error)
{
    if (!error)
            doRead();
    else
            std::cout << error.message() << std::endl;
}

void    Network::handleRead(const boost::system::error_code &error)
    {
    if (!error) {
            std::cout << m_buffer.data()<< std::endl;
            doRead();
    }
    else
            close();
}

void    Network::doRead()
{
    boost::asio::async_read(m_socket,
                            boost::asio::buffer(m_buffer),
                            boost::bind(&Network::handleRead,
                                        shared_from_this(),
                                        boost::asio::placeholders::error));
}
SJS
  • 77
  • 1
  • 2
  • 8

2 Answers2

0

What is the type of m_buffer? If it's a container, you have to resize it in advance. If it's asio::streambuf you need to at least use the bytes_transferred parameter recevied handleRead.

There are many echo examples, so let me just point you at a few:

sehe
  • 374,641
  • 47
  • 450
  • 633
0

The async_read doen't print the message until the size of the buffer is full !

SJS
  • 77
  • 1
  • 2
  • 8