c – boost :: asio,异步读错误

前端之家收集整理的这篇文章主要介绍了c – boost :: asio,异步读错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于某种原因,这会导致访问冲突,但是没有任何详细的文档/帮助,我不知道我在哪里做错了.由于我在boost站点上看到的这应该是正确的,并将每个asio :: write调用内容从客户端打印到新行.客户似乎工作正常.虽然服务器崩溃,但它还没有发送任何东西.

访问冲突发生在第275行的basic_stream_socket.hpp中.原因似乎是对象(boost :: asio :: stream_socket_service)没有初始化(this指针的值是0xfeeefeee),但我不明白为什么事实并非如此.

程序输出

Start server
Server::startAccept()
Server::handleAccept()
Connection accepted
Connection::startRead()
Server::startAccept()
Connection::handleRead()
READ ERROR: The I/O operation has been aborted because either a thread exited or an application request
Connection::startRead()

代码

#include "precompiled.h"
#include "db.h"
class Connection
    : public boost::enable_shared_from_this<Connection>
{
public:
    typedef boost::shared_ptr<Connection> Pointer;

    static Pointer create(boost::asio::io_service& ioService)
    {
        return Pointer(new Connection(ioService));
    }

    ip::tcp::socket& getSocket()
    {
        return socket;
    }

    void startRead()
    {
        std::cout << "Connection::startRead()" << std::endl;
        socket.async_read_some(boost::asio::buffer(readBuffer),boost::bind(&Connection::handleRead,this,_1,_2));
    }
private:
    Connection(asio::io_service& ioService)
        : socket(ioService)
    {
    }

    void handleWrite(const boost::system::error_code&,size_t)
    {
    }
    void handleRead(const boost::system::error_code&error,size_t len)
    {
        std::cout << "Connection::handleRead()" << std::endl;
        if(error)
        {
            std::cout << "READ ERROR: ";
            std::cout << boost::system::system_error(error).what();
            std::cout << std::endl;
        }
        else
        {
            std::cout << "read: ";
            std::cout.write(readBuffer.data(),len);
            std::cout << std::endl;
        }
        startRead();
    }
    boost::array<char,256> readBuffer;
    ip::tcp::socket socket;
};

class Server
{
public:
    Server(asio::io_service& ioService)
        :acceptor(ioService,ip::tcp::endpoint(ip::tcp::v4(),getPort()))
    {
        startAccept();
    }
private:
    void startAccept()
    {
        std::cout << "RServer::startAccept()" << std::endl;
        Connection::Pointer newConn =
            Connection::create(acceptor.io_service());

        acceptor.async_accept(newConn->getSocket(),boost::bind(&Server::handleAccept,newConn,asio::placeholders::error));
    }
    void handleAccept(Connection::Pointer newConn,const boost::system::error_code& error)
    {
        std::cout << "Server::handleAccept()" << std::endl;
        if(error)
        {
            std::cout << "CONNECTION ERROR: ";
            std::cout << boost::system::system_error(error).what();
            std::cout << std::endl;
        }
        else
        {
            std::cout << "Connection accepted" << std::endl;
            startAccept();
            newConn->startRead();
        }
    }
    ip::tcp::acceptor acceptor;
};

int main()
{
    std::cout << "Start server" << std::endl;
    asio::io_service ioService;
    RemoteAdminServer server(ioService);
    boost::system::error_code error;
    ioService.run(error);
}

解决方法

您应该更改此代码段:
void startRead()
   {
       std::cout << "Connection::startRead()" << std::endl;
       socket.async_read_some(boost::asio::buffer(readBuffer),_2));
   }

至:

void startRead()
{
     std::cout << "Connection::startRead()" << std::endl;
     socket.async_read_some(boost::asio::buffer(readBuffer),this->shared_from_this(),_2));
}

请注意,我将共享指针传递给bind.这将保持Connection实例,直到调用处理程序.否则,在Server :: startAccept中使用计数变为零,并删除该对象.然后,当调用处理程序时,内存无效,您会遇到可怕的“未定义行为”.

猜你在找的C&C++相关文章