boost::asio how to read full buffer in right way?

AeroSun

I am learning the boost::asio and now confusing about the right way to read the full buffer. For example, when connection established I want to read uint32_t in next way:

std::uint32_t size;
size_t len = m_socket.read_some(buffer(&size, sizeof(std::uint32_t)));

As you see I set up the buffer size. In other case I received the len with length on read_some data.

So the main question: is the boost::asio guarantied that there are would be read all 4 bytes of uint32_t if I set up the needed buffer length when call buffer?

Or if it not guaranteed - how I can read full buffer? (all 4 bytes)

Barry

From the read_some reference:

This function is used to read data from the stream socket. The function call will block until one or more bytes of data has been read successfully, or until an error occurs.

With remarks:

The read_some operation may not read all of the requested number of bytes. Consider using the read function if you need to ensure that the requested amount of data is read before the blocking operation completes.

So either you'll have to call read_some in a loop, or just call read, which will:

block until one of the following conditions is true:

  • The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes.
  • An error occurred.

This operation is implemented in terms of zero or more calls to the stream's read_some function.

The usage of read in your case would be:

std::uint32_t size;
size_t len = read(m_socket, buffer(&size, sizeof(std::uint32_t)));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Packing struct in Boost Asio buffer

From Dev

boost::asio::read() blocks forever

From Dev

Boost asio bind read handler

From Dev

boost::asio read/write trouble

From Dev

Boost asio asynchronous read and then write

From Dev

Boost.Asio - Multiple buffers with custom buffer

From Dev

Secure deallocation of boost::asio::const_buffer

From Dev

Boost::Asio - read or read_until?

From Dev

Boost::Asio - read or read_until?

From Dev

How can Boost.Asio create a buffer object of size equal to the size of the array?

From Dev

How to read buffered data as it arrives rather than when the buffer is full?

From Dev

How to read ANSI encoded files in the right way?

From Dev

How to read ANSI encoded files in the right way?

From Dev

boost asio cancelling read without cancelling write

From Dev

c++, boost asio, slow read

From Dev

Read Binary Data over TCP with Boost::Asio

From Dev

Boost asio trouble with async_read_until

From Dev

boost asio cancelling read without cancelling write

From Dev

Boost asio udp read optimization questions

From Dev

Is there any way to asynchronously wait for a future in Boost Asio?

From Dev

Generic way to timeout async operations in boost::asio

From Dev

How should I use async_read_until and async_write simultaneously in the client app in boost::asio?

From Dev

Write directly to cout using boost's boost::asio::read

From Dev

Is this real scenario using boost::asio for full duplex communication?

From Dev

Concurrent read and async_read_some in boost asio

From Dev

Boost asio async_read_until followed by async_read

From Dev

Will read (socket) block until the buffer is full?

From Dev

boost::asio::read prevents boost:asio::write from sending data to Java Socket

From Dev

boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

Related Related

  1. 1

    Packing struct in Boost Asio buffer

  2. 2

    boost::asio::read() blocks forever

  3. 3

    Boost asio bind read handler

  4. 4

    boost::asio read/write trouble

  5. 5

    Boost asio asynchronous read and then write

  6. 6

    Boost.Asio - Multiple buffers with custom buffer

  7. 7

    Secure deallocation of boost::asio::const_buffer

  8. 8

    Boost::Asio - read or read_until?

  9. 9

    Boost::Asio - read or read_until?

  10. 10

    How can Boost.Asio create a buffer object of size equal to the size of the array?

  11. 11

    How to read buffered data as it arrives rather than when the buffer is full?

  12. 12

    How to read ANSI encoded files in the right way?

  13. 13

    How to read ANSI encoded files in the right way?

  14. 14

    boost asio cancelling read without cancelling write

  15. 15

    c++, boost asio, slow read

  16. 16

    Read Binary Data over TCP with Boost::Asio

  17. 17

    Boost asio trouble with async_read_until

  18. 18

    boost asio cancelling read without cancelling write

  19. 19

    Boost asio udp read optimization questions

  20. 20

    Is there any way to asynchronously wait for a future in Boost Asio?

  21. 21

    Generic way to timeout async operations in boost::asio

  22. 22

    How should I use async_read_until and async_write simultaneously in the client app in boost::asio?

  23. 23

    Write directly to cout using boost's boost::asio::read

  24. 24

    Is this real scenario using boost::asio for full duplex communication?

  25. 25

    Concurrent read and async_read_some in boost asio

  26. 26

    Boost asio async_read_until followed by async_read

  27. 27

    Will read (socket) block until the buffer is full?

  28. 28

    boost::asio::read prevents boost:asio::write from sending data to Java Socket

  29. 29

    boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

HotTag

Archive