Reading from a TCP socket synchronously
Reading from a TCP socket is an input operation that is used to receive data sent by the remote application connected to this socket. Synchronous reading is the simplest way to receive the data using a socket provided by Boost.Asio. The methods and functions that perform synchronous reading from the socket blocks the thread of execution and doesn't return until the data (at least some amount of data) is read from the socket or an error occurs.
In this recipe, we will see how to read data from a TCP socket synchronously.
How to do it…
The most basic way to read data from the socket provided by the Boost.Asio library is the read_some()
method of the asio::ip::tcp::socket
class. Let's take a look at one of the method's overloads:
template< typename MutableBufferSequence> std::size_t read_some( const MutableBufferSequence & buffers);
This method accepts an object that represents a writable buffer (single or composite) as...