[Boost-users] Boost::Asio No write handler called after async_write

1,008 views
Skip to first unread message

David Narvaez

unread,
Apr 14, 2009, 12:18:46 AM4/14/09
to boost...@lists.boost.org
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;
}

Igor R

unread,
Apr 14, 2009, 4:51:27 AM4/14/09
to boost...@lists.boost.org
> 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?

You never call io_service::run(), that's why no handler is invoked.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

David Narvaez

unread,
Apr 14, 2009, 7:57:24 AM4/14/09
to boost...@lists.boost.org
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?

Igor R

unread,
Apr 14, 2009, 12:15:29 PM4/14/09
to boost...@lists.boost.org
> 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?

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.

David Narvaez

unread,
Apr 14, 2009, 11:17:24 PM4/14/09
to boost...@lists.boost.org
Thanks! Working like a charm now.

David E. Narváez
Reply all
Reply to author
Forward
0 new messages