You are right, I did notice an example code given by http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/timeouts/blocking_udp_client.cpp where the async_receive and timer are used. I left this out in my original post because I have lots of issues from that example, let me know if you'd like me to have a separate post, here are the problems:

(1) That example can only work with an IP address in local machine, if I change to a remote machine IP address, it got an error of "Exception: bind: Cannot assign requested address'.

(2) If my server is running at the same local machine with the same port number, it got an error of "Exception: bind: Address already in use".

How can test that example? I need to add an async_send and I need to run it with a local server.

Thank you.



On Thu, Apr 13, 2017 at 1:46 AM, TONGARI J via Boost-users <boost-users@lists.boost.org> wrote:
2017-04-12 19:17 GMT+08:00 jupiter via Boost-users <boost-users@lists.boost.org>:
Hi,

My program needs a sync udp socket send / receiver so I use the deadline_timer as timeout for udp socket receive_from on Debian Jessie:

boost::asio::deadline_timer deadline(io_service, boost::posix_time::seconds(2));

In checkDeadline function, when the timer expires, it calls socket.cancel, otherwise calls the deadline.async_wait(checkDeadline); again.

Apparently when the program runs to socket.receive_from(), it is blocked and the timer is never called because both socket.receive_from and the timer in the same thread.

I've been thinking to run the timer in another thread, but I checked Internet, there are comments said deadline_timer is not thread safe, please correct me if I am wrong here. It will be a simple solution for me if I can run timer on another thread. Anyone has other ideas to run the timer effectively?

Unfortunately, asio timers are useless with those blocking calls, you have to use the async version (e.g. async_receive_from) and code in async fashion instead.

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users