boost :: asio signal_set - 进入循环

时间:2012-02-18 08:51:50

标签: c++ boost boost-asio signals

我正在尝试编写一个简单的客户端,它将向服务器发送消息。要发送的消息数将基于stdin给出的输入。该程序预计将持续运行,直至终止。对于终止例程,我使用增强信号集。在运行程序时,进程将等待我键入迭代次数。现在,如果我发出kill -SIGTERM,则进程循环并开始打印“输入消息数量到泵:”。你能告诉我这里的错误是什么吗?

    boost::asio::io_service io_service;
    int main(int argc, char* argv[])
    {
        // Hardcoded the server IP and port
        tcp::socket sock(io_service);
        boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 5001);
        boost::asio::signal_set signals(io_service, SIGTERM);
        signals.async_wait(boost::bind(&boost::asio::io_service::stop, &io_service));
        sock.connect(endpoint);

        while (true)
        {
          char request[max_length] = "Message to be sent";

          int iter = 0;
          cout << "Enter number of messages to pump:";
          cin >> iter;

          for(int i=0 ; i < iter; i++)
          {
            size_t request_length = strlen(request);
            boost::asio::write(sock, boost::asio::buffer(request, request_length));
            cout << "Message written [" << request_length << "]:" << request << "\n";
          }
        }
    }

1 个答案:

答案 0 :(得分:2)

永远不会调用您的信号处理程序,因为除非调用io_service.run(),否则async_wait将不起作用。