I am trying to write my own asynchronous network client using boost::asio. One of the things I am observing in the examples available is the lack of clarity on whether to create a single io_service, resolver and query object or to create new instances of these objects for every connection that I am creating.
I have a client which will only contact a specific server (unique ip & host) but will do this several 100 times a day.
I am planning to have my own network class and in the class itself I have io_service, resolver, query and socket variables all set up at the time of constructing the object.
myclient::myclient() : io_service_(), resolver_(io_service_),
query_(tcp::v4(), host_, port_), socket_(io_service_)
{
}
//...
resolver.async_resolve_();
io_service_.reset();
io_service_.run_one();
Does this look okay?
You should try and limit yourself to one io_service for the process that you have described above the io_service class is thread-safe so can be used by many threads at once to dispatch work. Have a quick read of the io_service class documentation where there are some examples of use at the bottom.
Also have a look at the chat client example and note how the io_service is created in the main function and then passed by reference to the chat_client class.