2

I'm having an issue creating a really simple TCP based server-client connection using boost asio. When I get a connection from a client on my server and get into the method that handles the async_read_some I check for an error, and am always getting error 1236, which gives the message "The network connection was aborted by the local system."

I've just started working with boost, so I'm not really familiar with how the libraries work and what I could have done wrong. I've provided a cut down version of my code below:

/*Client connection code*/
ClientConnection::ClientConnection(boost::asio::io_service& io_service) : m_Socket(io_service)
{

}

ClientConnection::ClientConnectionPointer ClientConnection::Create(boost::asio::io_service& io_service)
{
    return ClientConnection::ClientConnectionPointer(new ClientConnection(io_service));
}

void ClientConnection::handle_write(const boost::system::error_code& error, size_t bytes_transferred)
{
    //once we've written our packet, just wait for more
    m_Socket.async_read_some(boost::asio::buffer(m_IncomingBytesBuffer, MAX_BYTES_LENGTH),
        boost::bind(&ClientConnection::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}

void ClientConnection::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
    if(!error)
    {
        //deal with the data that comes in here

    }
    else
    {
        std::cout << "Error reading port data" << std::endl;
        std::cout <<  error.message() << std::endl;
    }

}

tcp::socket& ClientConnection::GetSocket(void)
{
    return m_Socket;
}

void ClientConnection::RunClient(void)
{
    std::cout << "Client connected." << std::endl;
    //start by reading data from the connection
    m_Socket.async_read_some(boost::asio::buffer(m_IncomingBytesBuffer, MAX_BYTES_LENGTH),
        boost::bind(&ClientConnection::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}



/*Listener server code here*/
BarcodeServer::BarcodeServer(boost::asio::io_service& io_service) : m_acceptor(io_service, tcp::endpoint(tcp::v4(), SERVER_PORT_NUMBER))
{
    start_accepting_connections();
}

void BarcodeServer::start_accepting_connections(void)
{
    std::cout << "Waiting for a connection." << std::endl;
    ClientConnection::ClientConnectionPointer new_connection = ClientConnection::Create(m_acceptor.get_io_service());

    m_acceptor.async_accept(new_connection->GetSocket(), boost::bind(&BarcodeServer::handle_accepted_connection, this, new_connection, boost::asio::placeholders::error));
}

void BarcodeServer::handle_accepted_connection(ClientConnection::ClientConnectionPointer new_connection, const boost::system::error_code& error)
{
    if(!error)
    {
        new_connection->RunClient();
    }
    start_accepting_connections();
}


/*main code here*/
try
{
    boost::asio::io_service io_service;
    BarcodeServer server(io_service);
    io_service.run();
}
catch(std::exception& e)
{
    cout << "Error when running server:" << endl;
    cout << e.what() << endl;
    return RETURN_CODE_SERVER_RUN_ERROR;
}
return RETURN_CODE_SUCCESS;

Most of this code is prety much just lifted straight from examples on the boost website, so I'm guessing I've just done something silly somewhere, but I've looked over the code a few times and can't figure out where.

Any help would be much appreciated.

1 Answer 1

8

The lifetime of ClientConnection ends after handle_accepted_connection() exits, because all the instances of shared_ptr<ClientConnection> go out of scope and get destroyed.

To avoid this situation, you can either use shared_from_this idiom within ClientConnection member-functions or store 1 shared_ptr<ClientConnection> in some "connection manager".

2
  • 1
    Sorry, I should have included header code. The client connection does use enable_shared_from_this, however, looking through again with that in mind, it seems where I'm binding my handle methods I was still just using "this" instead of "this->shared_from_this()" I've changed it and it seems to work now, so I'll mark your reply as the answer. Thanks much.
    – Sam
    Jun 7, 2013 at 9:26
  • @Sam Yes, "shared_from_this idiom" in this context means binding handlers to the result of shared_from_this(), thus prolonging the object's lifetime at least until the async operation get completed.
    – Igor R.
    Jun 7, 2013 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.