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

c – boost :: asio,异步读错误,第1张

概述由于某种原因,这会导致访问冲突,但是没有任何详细的文档/帮助,我不知道我在哪里做错了.由于我在boost站点上看到的这应该是正确的,并将每个asio :: write调用的内容从客户端打印到新行.客户似乎工作正常.虽然服务器崩溃,但它还没有发送任何东西. 访问冲突发生在第275行的basic_stream_socket.hpp中.原因似乎是对象(boost :: asio :: stream_so 由于某种原因,这会导致访问冲突,但是没有任何详细的文档/帮助,我不知道我在哪里做错了.由于我在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 "@R_989_3013@.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 – boost :: asio,异步读错误全部内容,希望文章能够帮你解决c – boost :: asio,异步读错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1243326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存