简体   繁体   中英

Will Boost.Asio's async_read_some the whole message?

I am trying to receive a package via http in my C++ program. I am using Boost.Asio for that purpose.

In my handleAccept method I have this code to read the package:

    // m_buffer is declared as: std::array<char, 8192> m_buffer;
    auto buf = boost::asio::buffer(m_buffer);
    newConnection->getSocket().async_read_some(
        buf,
        boost::bind(&TcpServer::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
    );

Is this enough to get any package? How do I make sure that it will get packages that are bigger than 8192 bytes?

async_read_some reads a received message up to the size of the buffer that you give it. The number of bytes in a received message is returned in the bytes_transferred placeholder of the callback function, see basic_stream_socket::async_read_some

If you know the maximum size of the messages that you expect to receive, you can simply use a buffer that is large enough for that message. Otherwise, you'll have to copy the contents of the async_read_some buffer elsewhere and build up the whole received message there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM