boost::asio signal_set - 进入循环

发布于 2025-01-07 00:04:36 字数 1196 浏览 0 评论 0原文

我正在尝试编写一个简单的客户端,它将向服务器发送消息。要发送的消息数量将基于标准输入给出的输入。该程序预计将持续运行直到终止。对于终止例程,我使用升压信号集。运行程序时,该过程将等待我输入迭代次数。现在,如果我发出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";
          }
        }
    }

I am trying to write a simple client which will send a message to server. The number of messages to send will be based on input given from stdin. The program is expected to run continuously until it is terminated. For termination routine, I am using boost signal set. On running the program the process will wait for me to key in the number of iterations. Now if I issue a kill -SIGTERM , the process goes in loop and starts printing "Enter number of messages to pump:". Can you please tell me what is the mistake here?

    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";
          }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-01-14 00:04:36

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

Your signal handler will never be called because async_wait will not work unless io_service.run() is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文