我尝试写tcp服务器。从boost指南链接enter link description here。当我尝试打开acceptor -segmentation fault
时,IDE显示这行object_pool_access::prev(live_list_) = o;
我做错了什么?我尝试另一种方式,例如运行线程的上下文,但这不起作用。有时当我尝试修复时,我得到这样一条消息-
tpp.c:82: __pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= fifo_min_prio && new_prio <= fifo_max_prio)' failed.
Aborted (core dumped)
代码包含在第一个文件中。应用程序类与io_context,他在这里,因为我用他在程序的其他部分:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <memory>
using namespace boost::asio;
class api_application
{
public:
virtual ~api_application() {}
virtual void start() = 0;
virtual boost::asio::io_context& get_context() = 0;
};
class application : public api_application
{
public:
application();
~application() override {}
public:
virtual void start() override;
virtual boost::asio::io_context& get_context() override;
void stop();
private:
boost::asio::io_context _context;
std::shared_ptr<tcp_server> _server;
};
application::application()
:
_context(),
_server(std::make_shared<tcp_server>(*this))
{}
void application::start()
{
_server->start();
_context.run();
}
void application::stop()
{
_context.stop();
}
boost::asio::io_context& application::get_context()
{
return _context;
}
带有错误行的联网代码:
class connection : public std::enable_shared_from_this<connection>
{
public:
typedef std::shared_ptr<connection> con_ptr;
static con_ptr create(boost::asio::io_context& io_service)
{
return con_ptr(new connection(io_service));
}
ip::tcp::socket& socket();
void send(std::string message);
private:
connection(boost::asio::io_context& io_service);
void handle_write(const boost::system::error_code& err, size_t s);
private:
ip::tcp::socket _socket;
};
class tcp_server
{
public:
tcp_server(api_application& app);
void start();
private:
void start_accept();
void handle_accept(connection::con_ptr new_connection, const boost::system::error_code& error);
api_application& _app;
ip::tcp::endpoint _endpoint;
ip::tcp::acceptor _acceptor;
};
tcp_server::tcp_server(api_application& app)
:
_app(app),
_endpoint(boost::asio::ip::address_v4::any(), 80),
_acceptor(app.get_context())
{}
void tcp_server::start()
{
if(_acceptor.is_open())
return;
_acceptor.open(_endpoint.protocol()); // Here segfault
_acceptor.set_option(ip::tcp::socket::reuse_address(true));
_acceptor.bind(_endpoint);
_acceptor.listen();
start_accept();
}
void tcp_server::start_accept()
{
connection::con_ptr new_connection =
connection::create((boost::asio::io_context&)_acceptor.get_executor().context());
_acceptor.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void tcp_server::handle_accept(connection::con_ptr new_connection, const boost::system::error_code& error)
{
if (!error)
{
new_connection->send("Success connection");
}
start_accept();
}
void connection::send(std::string message)
{
boost::asio::async_write(_socket, boost::asio::buffer(message),
boost::bind(&connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
connection::connection(boost::asio::io_context& io_service)
: _socket(io_service)
{}
int main(int argc, const char** argv)
{
application app();
app.start();
return 0;
}
1条答案
按热度按时间0g0grzrc1#
我在重写代码以避免上下文引用时发现了可能的问题。请参见更新
我对代码没有问题,想象一下缺失的部分:
图纸,例如
总结
你的问题在别的地方,权限,异常处理,ODR,也许你没有运行你认为你在运行的代码。
此外,这段代码似乎过于复杂,而且有点过时(
io_service
已经过时很长时间了)。更新
这是高度可疑的代码
连接::连接指针新连接=连接::创建((asio::io上下文&)接受者.获取执行者().上下文());
如果它能工作,那是由于没有文档记录的实现细节。从技术上讲,它是Undefined Behaviour,因为它做了一个硬的重新解释转换。相反,只要按预期使用执行器就行了!
以下是为避免上下文引用而重新编写的整个代码:
仍在打印相同的**Live On Coliru**