Questions tagged [boost-asio]

Boost.Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach.

Most programs interact with the outside world in some way, whether it be via a file, a network, a serial cable, or the console. Sometimes, as is the case with networking, individual I/O operations can take a long time to complete. This poses particular challenges to application development. The Boost.Asio library is intended for programmers using C++ for systems programming, where access to operating system functionality such as networking is often required.

More information on Boost.Asio can be found here:

Notable alternatives for Boost.Asio:

4582 questions
18
votes
2 answers

boost::asio and async SSL stream: how to detect end of data/connection close?

I'm trying to make asio and SSL friends. Everything going well, but one thing is causing inconvenience: how to detect if peer close connection, and distinguish it from situation when peer just take a short break in sending data, aiming to…
Amdei
  • 223
  • 3
  • 8
18
votes
3 answers

Difference between read_some/write_some and receive/send?

I am beginning to work with Boost Asio's TCP sockets. What is the difference between read_some and receive and what is the difference between write_some and send? Thanks!
Brian
  • 1,201
  • 2
  • 14
  • 26
18
votes
1 answer

What exacty is io_context?

I have been going throught the boost asio library in which most of the objects need the io_context object as argument to the constructor. I have read what io_context object,according to the documentation it states that it is The io_context class…
Bad_Panda
  • 427
  • 1
  • 5
  • 11
18
votes
1 answer

When to use `asio_handler_invoke`?

Question When is it necessary to use asio_handler_invoke to achieve something that cannot be done by simply wrapping the handler? A canonical example that demonstrates a case where asio_handler_invoke is required would be ideal. Background The boost…
Zero
  • 11,593
  • 9
  • 52
  • 70
18
votes
4 answers

How to asynchronously read to std::string using Boost::asio?

I'm learning Boost::asio and all that async stuff. How can I asynchronously read to variable user_ of type std::string? Boost::asio::buffer(user_) works only with async_write(), but not with async_read(). It works with vector, so what is the reason…
SpyBot
  • 487
  • 2
  • 6
  • 16
18
votes
2 answers

Difference between `deadline_timer`and `waitable_timer` in `boost asio`

To expire a timer in 5 seconds, is there any practical difference between these two? Is any one preferable(performance, resource, etc.) to the other for this case? [Option 1] deadline_timer: boost::asio::deadline_timer…
ALittleDiff
  • 1,191
  • 1
  • 7
  • 24
18
votes
1 answer

How to gracefully shutdown a boost asio ssl client?

The client does some ssl::stream::async_read_some()/ssl::stream::async_write() calls and at some point needs to exit, i.e. it needs to shutdown the connection. Calling ssl::stream::lowest_layer().close() works,…
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
18
votes
2 answers

How do I handle fork() correctly with boost::asio in a multithreaded program?

I'm having some trouble grasping how to correctly handle creating a child process from a multithreaded program that uses Boost Asio in a multithreaded fashion. If I understand correctly, the way to launch a child process in the Unix world is to call…
villintehaspam
  • 8,540
  • 6
  • 45
  • 76
18
votes
2 answers

How to trick boost::asio to allow move-only handlers

In a RPC communication protocol, after the invocation of a method I'm sending "done" messages back to the caller. Since the methods are invoked in a concurrent fashion, the buffer containing the response (a std::string) needs to be protected by a…
marton78
  • 3,899
  • 2
  • 27
  • 38
17
votes
2 answers

Boost iostreams: How to create buffered (for reading) TCP stream?

So my main question here is how to implement a structure on top of asio tcp::socket or tcp::iostream that would implement some kind of input-seekable filter with buffer up to say 1kb?
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
17
votes
3 answers

How to countercheck a Boost Error Code appropriately?

I have a callback function which is bound to a boost::asio::deadline_timer. Now the function is called when the timer is cancelled or it expires. Since I need to distinguish between this two cases I need to check the passed Error code. The basic…
Toby
  • 3,815
  • 14
  • 51
  • 67
17
votes
1 answer

Boost.Asio with google protocol buffers

I've currently investigating ways of improving our current c++ network hand-made serialization mechanism maintaining our existing binary protocol. The first approach taken was to code it using Boost.Asio with Boost.Serialisation using binary…
jvaz
  • 201
  • 2
  • 7
17
votes
1 answer

shared_from_this() causes std::bad_weak_ptr even when correctly using make_shared

I am creating a C++ server application using standalone Asio and C++11 and am getting an error, which is why I am asking for help. The error In the class worker_thread, during the call to shared_from_this(), a bad_weak_ptr exception is raised, which…
Petar Nikić
  • 181
  • 1
  • 5
17
votes
3 answers

How do I perform a nonblocking read using asio?

I am attempting to use boost::asio to read and write from a device on a serial port. Both boost::asio:read() and boost::asio::serial_port::read_some() block when there is nothing to read. Instead I would like to detect this condition and write a…
Joe Ludwig
  • 6,756
  • 7
  • 30
  • 29
17
votes
2 answers

How do you correctly close sockets in boost::asio?

I am trying to wrap my head around resource management in boost::asio. I am seeing callbacks called after the corresponding sockets are already destroyed. A good example of this is in the boost::asio official example:…
Julian Stecklina
  • 1,271
  • 1
  • 10
  • 24