boost asio ssl async_shutdown always finishes with an error?

For a cryptographically secure shutdown, both parties musts execute shutdown operations on the boost::asio::ssl::stream by either invoking shutdown() or async_shutdown() and running the io_service. If the operation completes with an error_code that does not have an SSL category and was not cancelled before part of the shutdown could occur, then the connection was securely shutdown … Read more

Boost::Asio : io_service.run() vs poll() or how do I integrate boost::asio in mainloop

Using io_service::poll instead of io_service::run is perfectly acceptable. The difference is explained in the documentation The poll() function may also be used to dispatch ready handlers, but without blocking. Note that io_service::run will block if there’s any work left in the queue The work class is used to inform the io_service when work starts and … Read more

boost::asio cleanly disconnecting

I think you should probably have a call to socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec) in there before the call to socket.close(). The boost::asio documentation for basic_stream_socket::close states: For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket. This should ensure that any pending operations on the socket are properly cancelled … Read more

Pros & cons of a callback (std::function/std::bind) vs an interface (abstract class)

I strongly prefer the first way for several reasons: Representing concepts/functionality via interfaces/class hierarchies makes the code base less generic, flexible, and then more difficult to mantain or scale in the future. That kind of design imposes a set of requirements on the type (the type implementing the required functionality) which makes it difficult to … Read more

Copy a streambuf’s contents to a string

I don’t know whether it counts as “excessive copying“, but you can use a stringstream: std::ostringstream ss; ss << someStreamBuf; std::string s = ss.str(); Like, to read everything from stdin into a string, do std::ostringstream ss; ss << std::cin.rdbuf(); std::string s = ss.str(); Alternatively, you may also use a istreambuf_iterator. You will have to measure … Read more

Boost asio io_service dispatch vs post

Well, it depends on the context of the call, i.e. is it run from within the io_service or without: post will not call the function directly, ever, but postpone the call. dispatch will call it rightaway if the dispatch-caller was called from io_service itself, but queue it otherwise. So, it depends on the function calling … Read more

Should std::bind be compatible with boost::asio?

I now have a solution The problem is that when I first tried to switch to std::bind and std::shared_ptr I was still using the boost::asio::placeholders with std::bind, this resulted in a large amount of template compiler errors, so I then tried to switch piecemeal. I first tried switching just boost::shared_ptr to std::shared_ptr, this failed because … Read more

io_service, why and how is it used?

Asio’s io_service is the facilitator for operating on asynchronous functions. Once an async operation is ready, it uses one of io_service‘s running threads to call you back. If no such thread exists it uses its own internal thread to call you. Think of it as a queue containing operations. It guarantees you that those operations, … Read more

How to set a timeout on blocking sockets in boost asio?

TL;DR socket.set_option(boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO>{ 200 }); FULL ANSWER This question keep being asked over and over again for many years. Answers I saw so far are quite poor. I’ll add this info right here in one of the first occurrences of this question. Everybody trying to use ASIO to simplify their networking code would be perfectly … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)