1. Overview
Generally, most of the types we create for receiving errors are declared as follows:
boost::system::error_code error
We use this type to accept Errors generated in functions, such:
socket.connect(endpoint, error);
If the connection fails, the error type will be saved to error. For example, if the connection to the host fails, this error may be returned.
boost::asio::error::host_not_found;
If (error) throws an exception after detecting the error
throw boost::system::system_error(error);
Note that our error is converted to system_error.
An error is displayed. STD: cout <E. What ()
2. asynchronous call
The passing of the Exception error is a problem, because asynchronous will return immediately and local variables will be destroyed. However, boost: ASIO: placeholders: error will save the abnormal state, so that we can use the asynchronous call as shown in Figure
When using Socket: async_write_some, you do not need to create boost: System: error_code error. You can directly use boost: ASIO: placeholders: error as the parameter,
Similarly, sync_write_some needs to return the size of read/write data. The good news is that boost: ASIO: placeholders: bytes_transferred can be directly used as a parameter to save the data size.
Example:
boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&tcp_connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
The reference manual clearly states that the following two classes are designed for asynchronous calls using bind.
boost::asio::placeholders::errorboost::asio::placeholders::bytes_transferred
3. synchronous call
Of course, boost: System: error_code error is also useful. We use it as a parameter during synchronous calls. For example:
boost::system::error_code error;size_t len = socket.read_some(boost::asio::buffer(buf), error);
It is also used as a parameter in the callback handle of asynchronous calls, as shown in figure
void handle_write(const boost::system::error_code& /*error*/, size_t /*bytes_transferred*/){}
4. Summary
Asynchronous mode uses boost: ASIO: placeholders: error, boost: ASIO: placeholders: bytes_transferred.
Boost: System: error_code is used for synchronization.