开发者

Boost::asio udp socket - how should I use the API to allow cancellation of reads?

开发者 https://www.devze.com 2023-02-06 01:42 出处:网络
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. Rig

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?


There's no good way to do what you want using the synchronous receive_from method. You should use the asynchronous async_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号