本文介绍了升压ASIO"网络连接是由本地系统&QUOT中止;在async_read_some的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建使用boost ASIO一个非常简单的基于TCP服务器 - 客户端连接的问题。当我从一个客户我的服务器的连接,并进入处理我检查错误的async_read_some的方法,并且我总是收到错误1236这给消息的网络连接是由本地系统终止。

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."

我刚刚开始升压工作,所以我不是很熟悉的图书馆是如何工作的,什么我可以做错误的。我提供了我下面的code的削减版本:

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;

其中大部分code是preTY直接从升压网站上的例子很多刚刚解禁,所以我猜我只是做了一些愚蠢的地方,但我已经看了超过codea的几次想不通的地方。

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.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

ClientConnection 的生存期后结束handle_accepted_connection()退出,因为所有的实例的shared_ptr&LT; ClientConnection方式&gt; 走出去的范围和被摧毁

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.

要避免这种情况,您可以使用 shared_from_this 成语中的 ClientConnection 成员函数或存储1 的shared_ptr&LT; ClientConnection方式&gt; 在一些连接管理器

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".

这篇关于升压ASIO&QUOT;网络连接是由本地系统&QUOT中止;在async_read_some的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:31