Boost::Asio No write handler called after async_write

Hi everyone, this (I think) is about boost::asio. Could somebody please tell me why in the following code my handle_write function is never called after the async_write? This tries to be an hybrid mixture of sync read and async write through a socket and even though the string gets written, no handler is called at the end. #include <boost/asio.hpp> #include <boost/thread.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/bind.hpp> #include <iostream> #include <string> using boost::asio::ip::tcp; using namespace std; class Server : public boost::enable_shared_from_this<Server> { public: Server(); virtual ~Server(); void run(tcp::socket &); void handle_write(const boost::system::error_code &, size_t); }; Server::Server() { } Server::~Server() { } void Server::run(tcp::socket & sock) { string message("Hi!"); try { async_write(sock, boost::asio::buffer(message), boost::bind( &Server::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } catch (exception& e) { cerr << e.what() << endl; } return; } void Server::handle_write(const boost::system::error_code & errc, size_t bytes) { cout << "Function called" << endl; return; } int main() { try { boost::asio::io_service ioserv; tcp::acceptor acceptor(ioserv, tcp::endpoint(tcp::v4(), 1337)); boost::shared_ptr<Server> newserver(new Server()); for (;;) { tcp::socket newsocket(ioserv); acceptor.accept(newsocket); cout << "Somebody arrived" << endl; newserver->run(newsocket); } } catch (exception& e) { cerr << e.what() << endl; } return 0; }

Thanks for the answer, but all examples I see in the documentation using io_service:::run() are using an async-read/async-write model. How would io_service::run() fit in my sync-read/async-write model? Or is it impossible to do async-write if using async-read? On Tue, Apr 14, 2009 at 3:51 AM, Igor R <boost.lists@gmail.com> wrote:

I don't see in your code snippet where you intend to perform sync.read. Anyway, there's no technical limitation to use both sync.read and async.write - for example, you can call sync.read in handle_write, and it will work. As for io_service::run, you can place it just before "return" in Server::run.
participants (2)
-
David Narvaez
-
Igor R