如何在 boost tcp/udp 服务器中处理 control-c
我如何处理 control-C 事件或停止我的 boost::asio 服务器。我有一个 TCP & udp 组合服务器并希望能够在我按 ctrl-c 时干净地退出。我得到了未处理的 control-C 的第一次机会异常。这是我的代码
void startTCP()
{
http::syncServer::server serv( 2);
// Set console control handler to allow server to be stopped.
// console_ctrl_function = boost::bind(&http::syncServer::server::stop, &serv);
//SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
// Run the server until stopped.
serv.run();
}
void startUDP()
{
boost::asio::io_service io_service;
http::syncServer::udp_server server(io_service);
// console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
// SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
io_service.run();
}
int main(int argc, char* argv[])
{
try
{
boost::shared_ptr<boost::thread> tcpThread( new boost::thread(startTCP));
boost::shared_ptr<boost::thread> udpThread (new boost::thread(startUDP));
/*console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
SetConsoleCtrlHandler(console_ctrl_handler, FALSE);*/
tcpThread->join();
udpThread->join();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
How do I handle the control-C event or stop my boost::asio server. I have a tcp & udp combined server and would like to be able to exit cleanly when I press ctrl-c. I get a first chance exception for unhandled control-C. Here is my code
void startTCP()
{
http::syncServer::server serv( 2);
// Set console control handler to allow server to be stopped.
// console_ctrl_function = boost::bind(&http::syncServer::server::stop, &serv);
//SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
// Run the server until stopped.
serv.run();
}
void startUDP()
{
boost::asio::io_service io_service;
http::syncServer::udp_server server(io_service);
// console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
// SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
io_service.run();
}
int main(int argc, char* argv[])
{
try
{
boost::shared_ptr<boost::thread> tcpThread( new boost::thread(startTCP));
boost::shared_ptr<boost::thread> udpThread (new boost::thread(startUDP));
/*console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
SetConsoleCtrlHandler(console_ctrl_handler, FALSE);*/
tcpThread->join();
udpThread->join();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 boost 1.47 开始,您可以使用 asio 的 signal_set:
注意,最新 Ubuntu 的 boost 版本是 1.46 - 所以这是前沿(primo 2012)。
干杯,
迈克尔
As of boost 1.47 you can use signal_set from asio:
Beware, boost versions of e.g. latest Ubuntu is 1.46 - so this is bleeding edge (primo 2012).
Cheers,
Michael
C 标准库包含
(例如 参见此处),您可以使用它注册 SIGINT (Ctrl-C) 的信号处理程序。假设您的平台支持信号,这应该可以解决问题。您可能还想为 SIGTERM 注册一个处理程序,以优雅地响应被kill(1)ed。
正如 Sam Miller 所建议的,假设您的主循环是一个带有一些
m_io_service.run()
的单线程 boost.asio 循环。然后,您可以代替全局标志(假设m_io_service
可见)从信号处理程序中向 io 服务发布停止处理程序。The C standard library contains
<signal.h>
(e.g. see here), with which you can register a signal handler for SIGINT (Ctrl-C). That should do the trick, supposing your platform supports signals.You might also want to register a handler for SIGTERM to respond gracefully to being kill(1)ed.
As suggested by Sam Miller, suppose your main loop is a single-threaded boost.asio loop with some
m_io_service.run()
. Then instead of the global flags you could (assumingm_io_service
is visible) post a stop handler to the io service from within the signal handlers.您可以使用 asio 中的 signal_set,如下所示
文档在这里
You can use signal_set from asio, like this
The docs are here