Re: [asio-users] basic_stream_socket::async_connect -- non-blockingconnect vs async connect
Brought to you by:
chris_kohlhoff
|
From: Christopher K. <ch...@ko...> - 2007-09-20 10:20:44
|
Chauhan, Vikas wrote: > Hello all, > > I think I have found the reason for this. > > Actually, I had the following sequence of calls earlier : > 1. tcp::socket::async_connect(peer_endpoint,connect_handler ). > 2. io_service::run() > > Logically, step 2 should precede step 1, but I could not do that as > io_service::run() was failing as there was nothing registered to it. There should be no requirement to perform the run first. Starting the async_connect gives the io_service "work" to do in any case. > Now, I have created an instance of "boost::asio::io_service::work " in > the beginning of program. It helps the io_service to queue any aync > calls. > Hence now the sequence of calls is : > 1. boost::asio::io_service::work(ioservice) > 2. io_service::run() > 3. tcp::socket::async_connect(peer_endpoint,connect_handler ). > > Thanks to those who may have tried to find out reasons for the problem. I'm a bit confused about what your original problem was now :) An async_connect operation should take the same length of time to complete as a blocking connect operation. E.g. if a connect takes 20 seconds then an async_connect should also take 20 seconds. However it now sounds like you're talking about when the completion handler itself is called, and that is only allowed to occur from within a call to io_service::run() (or run_one, poll or poll_one). You can start an async_connect and then not call io_service::run() straight away, in which case your handler won't be called until you do. Cheers, Chris |