gpt4 book ai didi

c++ - Boost:asio 获取异常

转载 作者:行者123 更新时间:2023-11-28 05:33:07 28 4
gpt4 key购买 nike

我正在使用 boost.asio 编写客户端服务器演示。我浏览了 http 演示并从头开始编写了自己的代码。但是,我在接受器初始化时遇到了一个异常(很可能)。我不确定为什么我会收到此异常,而我的示例 http 代码(来自 boost 站点)正在做同样的事情并且运行良好。

这是我的 server.h 代码示例,

 #include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include "Connection.h"
#include <iostream>

using boost::asio::ip::tcp;

class Server : private boost::noncopyable
{
public:
explicit Server(const std::string& address, const std::string& port);

void run();

private:
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::io_service _ioService;
CONNECTION_SHARED_POINTER _connection;

void start_accept();
void handle_accept(const boost::system::error_code& e);
void handle_stop();
};

server.cpp代码

#include "Server.h"
#include <boost/bind.hpp>


Server::Server(const std::string& address, const std::string& port)
:_acceptor(_ioService),
_connection()
{

boost::asio::ip::tcp::resolver resolver(_ioService);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);

_acceptor.open(endpoint.protocol());
_acceptor.set_option(
boost::asio::ip::tcp::acceptor::reuse_address(true));
_acceptor.bind(endpoint);
_acceptor.listen();


//Starting the acceptor
start_accept();
}

void Server::run()
{
std::cout<< "Running ioService" <<std::endl;
_ioService.run();
}

void Server::start_accept()
{
_connection.reset(new Connection(_ioService));

std::cout<< "Accept connection" <<std::endl;
_acceptor.async_accept( _connection->socket(),
boost::bind( &Server::handle_accept, this,
boost::asio::placeholders::error));


}

void Server::handle_accept(const boost::system::error_code& e)
{

if (!e)
{
_connection->start();
}

start_accept();
}

还有我的 main.cpp

#include "Server.h"
#include <iostream>

int main(int argc, char* argv[])
{
try
{
Server s("0.0.0.0", "7000");
s.run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}

return 0;
}

现在我的代码在服务器 CTOR 中得到一个异常,同时尝试初始化 _acceptor。错误信息如下:

Unhandled exception at 0x7747d7f6 in VrfServer.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.

堆栈转储如下:

 ntdll.dll!000000007747d7f6()   
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
VrfServer.exe!boost::asio::detail::win_mutex::lock() Line 51 C++
VrfServer.exe!boost::asio::detail::scoped_lock<boost::asio::detail::win_mutex>::scoped_lock<boost::asio::detail::win_mutex>(boost::asio::detail::win_mutex & m) Line 47 C++
VrfServer.exe!boost::asio::detail::service_registry::do_use_service(const boost::asio::io_service::service::key & key, boost::asio::io_service::service * (boost::asio::io_service &)* factory) Line 108 + 0x12 bytes C++
VrfServer.exe!boost::asio::detail::service_registry::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >() Line 49 C++
VrfServer.exe!boost::asio::use_service<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & ios) Line 34 C++
VrfServer.exe!boost::asio::basic_io_object<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::basic_io_object<boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & io_service) Line 91 + 0x36 bytes C++
VrfServer.exe!boost::asio::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >(boost::asio::io_service & io_service) Line 86 C++
VrfServer.exe!Server::Server(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & address, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & port) Line 8 + 0x76 bytes C++
VrfServer.exe!main(int argc, char * * argv) Line 8 + 0x52 bytes C++
VrfServer.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
VrfServer.exe!mainCRTStartup() Line 371 C
kernel32.dll!00000000772259bd()
ntdll.dll!000000007745a2e1()

任何人都可以阐明吗?

最佳答案

问题与 asio 无关,这只是 C++ 的工作方式

在您的 Server 类中声明:

   private:
boost::asio::ip::tcp::acceptor _acceptor;
boost::asio::io_service _ioService;
CONNECTION_SHARED_POINTER _connection;

然后按如下方式初始化它:

Server::Server(const std::string& address, const std::string& port)
:_acceptor(_ioService),
_connection()

当你初始化_acceptor时,_ioService还没有被初始化,因为在类中声明的顺序。

   boost::asio::io_service          _ioService;
boost::asio::ip::tcp::acceptor _acceptor;
CONNECTION_SHARED_POINTER _connection;

应该可以解决问题。

关于c++ - Boost:asio 获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966022/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com