1

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?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Max
  • 4,345
  • 8
  • 38
  • 64

1 Answers1

2

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.

Community
  • 1
  • 1
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • Thanks! Confirmation was all I needed. Was hoping I could avoid the extra complexity. – Max Jan 24 '11 at 12:09