Boost::asio udp 套接字 - 我应该如何使用 API 来允许取消读取?
我使用 boost::asio::ip::udp::socket
进行通信。我使用 socket.receive_from(...)
接收来自客户端的消息。目前工作正常,但我希望能够关闭我的服务器。现在我在 while 循环中调用 receive_from
,这取决于我可以设置的 bool 条件。但是,如果我无法强制线程定期或在某个调用时退出 receive_from ,那么这是毫无用处的。
这可能吗?我尝试过谷歌搜索,但没有找到明确的答案。我尝试过使用 socket.cancel()
但这似乎没有效果。
我是否以正确的方式使用套接字?
I am using boost::asio::ip::udp::socket
to communicate. I use socket.receive_from(...)
to receive messages from clients. This is working fine for now, but I want to be able to shut down my server. Right now I am calling receive_from
in a while-loop, which depends on a bool condition which I can set. However, this is pretty useless if I cannot force the thread to exit receive_from
at regular intervals or at a certain call.
Is this even possible? I have tried googling, but found no clear answer. I have tried using socket.cancel()
but this seems to have no effect.
Am I using the socket in the correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有好的方法可以使用同步
receive_from
方法来完成您想要的操作。如果您需要超时和可取消性,您应该使用异步async_receive_from
方法。 Boost.Asio trac 网站上有一个 ticket 描述了这一点。我回答了类似问题 最近您可能会发现它也很有用。
There's no good way to do what you want using the synchronous
receive_from
method. You should use the asynchronousasync_receive_from
method if you desire timeouts and cancelability. There's a ticket on the Boost.Asio trac website describing this.I answered a similar question recently that you might find useful as well.