为什么 boost 服务器 class 抛出运行时错误
Why is the boost server class throwing runtime error
我在以下代码中遇到运行时错误:
boost::asio::io_context io_context;
server server1(io_context, 1980);
boost::thread t(boost::bind(&boost::asio::io_context::run, &io_context));
其中 server
class 的定义是:
using boost::asio::ip::tcp;
class server
{
public:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
{
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket)
{
if (!ec)
{
std::make_shared<session>(std::move(socket))->start();
}
do_accept();
});
}
tcp::acceptor acceptor_;
};
错误在:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
Exception thrown at 0x00007FFE9D804F69 in Everest.exe: Microsoft C++
exception: boost::wrapexceptboost::system::system_error at memory
location 0x0000003AA613BDB0.
您的代码似乎缺少监听。在我的 Linux 框上,这根本没有做任何事情,但也许在 Win32 上,它在执行 async_accept
?
时由于 acceptor
的无效状态而抛出错误
这是一个简单的测试器,可以执行 linux 上的预期操作:
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;
struct session : std::enable_shared_from_this<session> {
tcp::socket _socket;
session(tcp::socket&& s) : _socket(std::move(s)) {
std::cerr << "new session " << _socket.remote_endpoint() << std::endl;
}
void start() {}
};
class server {
public:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
{
acceptor_.listen();
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket) {
std::cerr << "accept " << ec.message() << std::endl;
if (!ec) {
std::make_shared<session>(std::move(socket))->start();
do_accept();
}
});
}
tcp::acceptor acceptor_;
};
int main()
{
boost::asio::io_context io_context;
server server1(io_context, 1980);
io_context.run();
}
我在以下代码中遇到运行时错误:
boost::asio::io_context io_context;
server server1(io_context, 1980);
boost::thread t(boost::bind(&boost::asio::io_context::run, &io_context));
其中 server
class 的定义是:
using boost::asio::ip::tcp;
class server
{
public:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
{
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket)
{
if (!ec)
{
std::make_shared<session>(std::move(socket))->start();
}
do_accept();
});
}
tcp::acceptor acceptor_;
};
错误在:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
Exception thrown at 0x00007FFE9D804F69 in Everest.exe: Microsoft C++ exception: boost::wrapexceptboost::system::system_error at memory location 0x0000003AA613BDB0.
您的代码似乎缺少监听。在我的 Linux 框上,这根本没有做任何事情,但也许在 Win32 上,它在执行 async_accept
?
acceptor
的无效状态而抛出错误
这是一个简单的测试器,可以执行 linux 上的预期操作:
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;
struct session : std::enable_shared_from_this<session> {
tcp::socket _socket;
session(tcp::socket&& s) : _socket(std::move(s)) {
std::cerr << "new session " << _socket.remote_endpoint() << std::endl;
}
void start() {}
};
class server {
public:
server(boost::asio::io_context& io_context, short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
{
acceptor_.listen();
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(
[this](boost::system::error_code ec, tcp::socket socket) {
std::cerr << "accept " << ec.message() << std::endl;
if (!ec) {
std::make_shared<session>(std::move(socket))->start();
do_accept();
}
});
}
tcp::acceptor acceptor_;
};
int main()
{
boost::asio::io_context io_context;
server server1(io_context, 1980);
io_context.run();
}