To use Beast effectively, a prior understanding of Networking is required. This section reviews these concepts as a reminder and guide for further learning.
A [@https://en.wikipedia.org/wiki/Computer_network ['network]] allows programs located anywhere to exchange information after opting-in to communications by establishing a [@https://en.wikipedia.org/wiki/Data_link ['connection]]. Data may be reliably transferred across a connection in both directions ([@https://en.wikipedia.org/wiki/Duplex_(telecommunications) ['full-duplex]]) with bytes arriving in the same order they were sent. These connections, along with the objects and types used to represent them, are collectively termed [link beast.concepts.streams ['streams]]. The computer or device attached to the network is called a [@https://en.wikipedia.org/wiki/Host_(network) ['host]], and the program on the other end of an established connection is called a [@https://en.wikipedia.org/wiki/Peer-to-peer ['peer]].
The [@https://en.wikipedia.org/wiki/Internet ['internet]] is a global network of interconnected computers that use a variety of standardized communication protocols to exchange information. The most popular protocol is [@https://en.wikipedia.org/wiki/Transmission_Control_Protocol ['TCP/IP]], which this library relies on exclusively. The protocol takes care of the low level details so that applications see a ['stream], which is the reliable, full-duplex connection carrying the ordered set of bytes described above. A [@https://en.wikipedia.org/wiki/Server_(computing) ['server]] is a powerful, always-on host at a well-known network name or network address which provides data services. A [@https://en.wikipedia.org/wiki/Client_(computing) ['client]] is a transient peer which connects to a server to exchange data, and goes offline.
A vendor supplies a program called a [@https://en.wikipedia.org/wiki/Device_driver ['device driver]], enabling networking hardware such as an [@https://en.wikipedia.org/wiki/Network_interface_controller ['ethernet adaptor]] to talk to the operating system. This in turn permits running programs to interact with networking using various flavors of interfaces such as [@https://en.wikipedia.org/wiki/Berkeley_sockets ['Berkeley sockets]] or [@https://en.wikipedia.org/wiki/Winsock ['Windows Sockets 2]] ("Winsock").
Networking in C++, represented by __Asio__, [@https://think-async.com/Asio/ Asio], and __NetTS__, provides a layer of abstraction to interact portably with the operating system facilities for not just networking but general [@https://en.wikipedia.org/wiki/Input/output ['input/output]] ("I/O").
A [@https://en.wikipedia.org/wiki/Data_buffer ['buffer]] holds a contiguous sequence of bytes used when performing I/O. The types [@boost:/doc/html/boost_asio/reference/const_buffer.html `net::const_buffer`] and [@boost:/doc/html/boost_asio/reference/mutable_buffer.html `net::mutable_buffer`] represent these memory regions as type-safe pointer/size pairs:
The concepts __ConstBufferSequence__ and __MutableBufferSequence__ describe bidirectional ranges whose value type is convertible to `const_buffer` and `mutable_buffer` respectively. These sequences allow transacting with multiple buffers in a single function call, a technique called [@https://en.wikipedia.org/wiki/Vectored_I/O ['scatter/gather I/O]]. Buffers and buffer sequences are non-owning; copies produce shallow references and not duplicates of the underlying memory. Each of these statements declares a buffer sequence:
The functions [@boost:/doc/html/boost_asio/reference/buffer_size.html `net::buffer_size`] and [@boost:/doc/html/boost_asio/reference/buffer_copy.html `net::buffer_copy`] determine the total number of bytes in a buffer sequence, and transfer some or all of bytes from one buffer sequence to another respectively. The function `buffer_size` is a customization point: user defined overloads in foreign namespaces are possible, and callers should invoke `buffer_size` without namespace qualification. The functions [@boost:/doc/html/boost_asio/reference/buffer_sequence_begin.html `net::buffer_sequence_begin`] and [@boost:/doc/html/boost_asio/reference/buffer_sequence_end.html `net::buffer_sequence_end`] are used to obtain a pair of iterators for traversing the sequence. Beast provides a set of buffer sequence types and algorithms such as [link beast.ref.boost__beast__buffers_cat `buffers_cat`], [link beast.ref.boost__beast__buffers_front `buffers_front`], [link beast.ref.boost__beast__buffers_prefix `buffers_prefix`], [link beast.ref.boost__beast__buffers_range `buffers_range`], and [link beast.ref.boost__beast__buffers_suffix `buffers_suffix`]. This example returns the bytes in a buffer sequence as a string:
The __DynamicBuffer__ concept defines a resizable buffer sequence interface. Algorithms may be expressed in terms of dynamic buffers when the memory requirements are not known ahead of time, for example when reading an HTTP message from a stream. Beast provides a well-rounded collection of dynamic buffer types such as [link beast.ref.boost__beast__buffers_adaptor `buffers_adaptor`], [link beast.ref.boost__beast__flat_buffer `flat_buffer`], [link beast.ref.boost__beast__multi_buffer `multi_buffer`], and [link beast.ref.boost__beast__static_buffer `static_buffer`]. The following function reads data from a [link beast.ref.boost__beast__tcp_stream `tcp_stream`] into a dynamic buffer until it encountering a newline character, using [@boost:/doc/html/boost_asio/reference/buffers_iterator.html `net::buffers_iterator`] to treat the contents of the buffer as a range of characters:
Synchronous input and output is accomplished through blocking function calls that return with the result of the operation. Such operations typically cannot be canceled and do not have a method for setting a timeout. The __SyncReadStream__ and __SyncWriteStream__ concepts define requirements for ['synchronous streams]: a portable I/O abstraction that transfers data using buffer sequences to represent bytes and either `error_code` or an exception to report any failures. [@boost:/doc/html/boost_asio/reference/basic_stream_socket.html ['net::basic_stream_socket]] is a synchronous stream commonly used to form TCP/IP connections. User-defined types which meet the requirements are possible:
A ['synchronous stream algorithm] is written as a function template accepting a stream object meeting the named requirements for synchronous reading, writing, or both. This example shows an algorithm which writes text and uses exceptions to indicate errors:
An asynchronous operation begins with a call to an [@boost:/doc/html/boost_asio/reference/asynchronous_operations.html ['initiating function]], which starts the operation and returns to the caller immediately. This ['outstanding] asynchronous operation proceeds concurrently without blocking the caller. When the externally observable side effects are fully established, a movable function object known as a [@boost:/doc/html/boost_asio/reference/CompletionHandler.html ['completion handler]] provided in the initiating function call is queued for execution with the results, which may include the error code and other specific information. An asynchronous operation is said to be ['completed] after the completion handler is queued. The code that follows shows how some text may be written to a socket asynchronously, invoking a lambda when the operation is complete:
Every completion handler (also referred to as a [@https://en.wikipedia.org/wiki/Continuation ['continuation]]) has both an [@boost:/doc/html/boost_asio/overview/core/allocation.html ['associated allocator]] returned by [@boost:/doc/html/boost_asio/reference/get_associated_allocator.html `net::get_associated_allocator`], , an [@boost:/doc/html/boost_asio/reference/associated_cancellation_slot.html ['associated cancellation slot]] returned by [@boost:/doc/html/boost_asio/reference/associated_cancellation_slot.html `net::get_associated_cancellation_slot`]. and an [@boost:/doc/html/boost_asio/reference/associated_executor.html ['associated executor]] returned by [@boost:/doc/html/boost_asio/reference/get_associated_executor.html `net::get_associated_executor`]. These associations may be specified intrusively:
Or these associations may be specified non-intrusively, by specializing the class templates [@boost:/doc/html/boost_asio/reference/associated_allocator.html `net::associated_allocator`] , [@boost:/doc/html/boost_asio/reference/associated_cancellation_slot.html `net::associated_cancellation_slot`] and [@boost:/doc/html/boost_asio/reference/associated_executor.html `net::associated_executor`]:
The function [@boost:/doc/html/boost_asio/reference/bind_executor.html `net::bind_executor`] may be used when the caller wants to change the executor of a completion handler.