I'm looking to use the ASIO standalone library (not Boost ASIO), I am trying to set up a client to connect to a server on a specific port.
I saw in the porthopper example that it is possible to get the endpoint without having to deal with an iterator.
asio::io_service io_service;
// Determine the location of the server.
tcp::resolver resolver(io_service);
tcp::resolver::query query(host_name, port);
tcp::endpoint remote_endpoint = *resolver.resolve(query);
I am trying to do the resolve of the query using resolver async_resolve() member function.
This is the code I currently have:
asio::io_service IOService;
asio::ip::tcp::resolver resolver(IOService);
asio::ip::tcp::resolver::query query(ADDRESS, PORT);
resolver.async_resolve(query,
[this](const tcp::endpoint srvEndpoint, std::error_code error)
{
IOService->post(
[this, error, srvEndpoint]
{
handle_resolve_handler(error, srvEndpoint);
});
});
Is there a way to do what was shown in the porthopper example but perform it asynchronously?
Oh, but
deals with the iterator very much! It uses it to dereference. Note that cute star? It's the pointer indirection operator.
As for your call:
That does not satisfy the completion handler requirements. Indeed, trying to compile it with Boost Asio² gives a slew of errors: Live On Coliru:
The docs say:
Lo and behold, there's your iterator again! This is by no means an accident. The design of the library is such that async calls will always return the same data regardless of the chosen interface.¹
Cobbling it together: Live On Coliru
Prints³:
¹ cf. the difference when using coroutines (
yield
oryield[ec]
),asio::use_future
etc.: How to set error_code to asio::yield_context² basically
s/boost::system::error_code/std::error_code/
³ On systems with network access