Software‎ > ‎

Using boost::asio for non-blocking IO with 3rd party API

posted Feb 8, 2011, 10:18 PM by Andrew Jessop   [ updated Dec 23, 2011, 3:45 AM ]
Recently I have had to deal with a C++ library that maintains control of the creation, binding, connection, buffering etc of a TCP connection and associated file descriptor.  I wanted to use the boost::asio framework for this application and below is how I went about it.

Update: This doesn't seem to be using epoll() or select() as I had intended.  The code will need changing.

#include <boost/asio.hpp>
#include <boost/bind.hpp>

#include <custom/api.hh>


    void connect()
        // other API stuff here
        mySocket.assign(boost::asio::ip::tcp::v4(), api->fd());
        boost::asio::ip::tcp::socket::non_blocking_io nb(true);


    void startIO()
            boost::bind(&APIWrapper::ioRead, this)
            boost::bind(&APIWrapper::ioWrite, this)

    void action()
        // Since the API either directly, or via a buffer, writes to the socket
        // nothing else has to be done here. We will get the ioWrite() callback
        // from boost::asio once we're ready to write data back out.
    boost::asio::ip::tcp::socket mySocket;
    API *api;

    void ioRead()
    void ioWrite()

int main()
    // Create the main service
    boost::asio::io_service io_service;
    boost::asio::io_service::work work(io_service);

    // Connect via API
    APIWrapper client;

    // Perform some initial action

    // Start the main event loop;

    return 0;