2

boost asio を使用して、非常に単純な TCP ベースのサーバー/クライアント接続を作成する際に問題が発生しています。サーバー上のクライアントから接続を取得し、async_read_some を処理するメソッドに入ると、エラーをチェックしますが、常にエラー 1236 が発生し、「ネットワーク接続はローカル システムによって中止されました」というメッセージが表示されます。

ブーストを使い始めたばかりなので、ライブラリがどのように機能するのか、何が間違っていたのかよくわかりません。私は以下の私のコードのカットダウンバージョンを提供しました:

/*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;

このコードのほとんどは、boost Web サイトの例からそのまま引用しただけなので、どこかでばかげたことをしただけだと思いますが、コードを数回調べたところ、どこにあるのかわかりません。

どんな助けでも大歓迎です。

4

1 に答える 1