Transferring data using buffers
Buffers are contiguous regions of memory used during I/O operations to transfer data.
Boost.Asio defines two types of buffers: mutable buffers (boost::asio::mutable_buffer
), where data can be written, and constant buffers (boost::asio::const_buffers
), which are used to create read-only buffers. Mutable buffers can be converted into constant buffers, but not the opposite. Both types of buffers provide protection against overruns.
There is also the boost::buffer
function to help with the creation of mutable or constant buffers from different data types (a pointer to raw memory and size, a string (std::string
), or an array or vector of plain old data (POD) structures (meaning a type, a structure, or a class that has no user-defined copy assignment operator or destructor, and without private or protected non-static data members). For example, to create a buffer from an array of chars, we can use the following:
char data[1024]; mutable_buffer buffer...