Re: [Boost-users] boost asio async_read_some

Igor,
Thanks for the response. I've been out for a few days, so I haven't
been able to reply. Here is a self-contained example that illustrates
the problem. This example creates a echo server thread that should
echo back to the caller anything the caller sends to the thread. (Note
that I have a windows Sleep() in there.) The server thread appears to
do what I expect. The client thread works until the call to
async_read_some(). The call returns, but I never hit the breakpoint I
set in my handler, nor do I see any output from the handler.
Igor, or anyone, please have a look...
Thanks,
Chris
=============================================START==================================================
#include <string>
#include
Here's a simplified version of my call and handler:
unsigned char tmpBuffer[512]; //global
void MyClass::handler(const boost::system::error_code& error, size_t bytes_transferred) { boost::system::error_code e; std::cerr << "handler called()" << "\n"; }
void MyClass::test() {
... socketPtr->async_read_some(boost::asio::buffer(tmpBuffer, 512), boost::bind(&MyClass::handler, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)); }
Can anyone give me any idea as to how I can get my handler to be called?
Could you please provide some code-excerpt that should compile and work? I.e. code that contains socket initialization, io_service run and your object creation - because the mistake is probably somewhere at those points.
Invocation of the handler will be performed in a manner equivalent to using boost::asio::io_service::post()." I'm not sure exactly what this means...what is this equivalent manner that is mentioned?
It means that the handler would be invoked in one of the threads that run the io_service object used by the socked.- Hide quoted text - - Show quoted text -

The client thread works until the call to async_read_some(). The call returns, but I never hit the breakpoint I set in my handler, nor do I see any output from the handler.
I can't see where you call io_service::run(). If your socket's io_service is not running, noone would process your async. calls. Please, refer to the asio examples/tutorial to find out how to organize an asyncronous i/o.

Thanks for the pointer Igor. I overlooked this part of some of the examples. Ultimately, what I want is a client that does sync. reads and writes, but also polls for data using async. reads., all using the same socket. Looking at some of the examples, it looks like polling would be done by starting another async_read from the handler supplied to the async_read. Thanks again, Chris =======================

Looking at some of the examples, it looks like polling would be done by starting another async_read from the handler supplied to the async_read.
Yes, exactly. In general, you can mix sync/async i/o, but note that you should not start a read operation before the previous read (on the same socket) is completed (the same about write).

Hmm...it's still not working for me. To the code I posted before, I added the call ioService.run(); in the doConnect() routine, after the loop where the connect is done. I get the same behavior: after the async_read_some() is called, I can see the the buffer is updated (so it read what was there), but my handler was never called. I'm sure there's a simpler explanation for my problem, but, FWIW, looking at the stack for the async_read_some call, I see the call to WSARecv(). I don't have much experience with that routine, but I did notice that the last argument(LPWSAOVERLAPPED_COMPLETION_ROUTINE) was hardcoded to 0. This may be correct, but at first glance it seems like that should be a handler to my handler routine. -- =======================
participants (2)
-
Chris Freehill
-
Igor R