boost::asio - Can't get timeout on accept to work
Hello, I would like to implement a timeout while accepting a connection on a TCP socket. I have reviewed the following example: http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/example/timeouts/ac... And here is my implementation of it, though it does not seem to wait for the given timeout... And I don't seem to see CProtobufSocket::HandleAccept being called. Could anybody tell me what I am missing with this code? Thanks! Jean-Sebastien int CProtobufSocket::Receive( string port, google::protobuf::Message & message, const boost::posix_time::milliseconds timeout ) { int result = -1; boost::asio::io_service ioService; boost::asio::ip::tcp::acceptor acceptor(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), atoi(port.data()))); boost::asio::ip::tcp::socket socket(ioService); try { boost::asio::deadline_timer mytimer(ioService); acceptor.async_accept(socket, boost::bind(CProtobufSocket::HandleAccept, boost::asio::placeholders::error, result)); mytimer.expires_from_now(timeout); mytimer.async_wait(boost::bind(CProtobufSocket::Close, &acceptor)); if(0 == result) { // receive on the socket. result = Receive(socket, message, timeout); } else {} } catch (std::exception& e) { // jstoezel 2910208: removes warning C4101: 'e' : unreferenced local variable e; } try { socket.shutdown(socket.shutdown_both); socket.close(); } catch (std::exception& e) { // jstoezel 2910208: removes warning C4101: 'e' : unreferenced local variable e; } return result; } void CProtobufSocket::Close( boost::asio::ip::tcp::acceptor * acceptor ) { acceptor->close(); } void CProtobufSocket::HandleAccept(const boost::system::error_code &err, int & result) { err; if(err) {} else { result = 0; } }
try { boost::asio::deadline_timer mytimer(ioService);
acceptor.async_accept(socket, boost::bind(CProtobufSocket::HandleAccept, boost::asio::placeholders::error, result)); mytimer.expires_from_now(timeout); mytimer.async_wait(boost::bind(CProtobufSocket::Close, &acceptor));
In your real code do you also define the timer as a local object? If so, it's destroyed by the end of the scope.
...after looking at code a bit more, I noticed that you also define the io_service as local object, and you do not run io_service at all. Please, refer to ASIO documentation to find the correct usage of the library.
Hello,
Well I did a copy/paste of what the code is, so wysiwyg ;). I have
moved the timer outside of the try block, though I still get the same
behavior, mytimer.async_wait returns right away with no connection...
Since mytimer.async_wait woudl block, my understanding is that the
timer would not be destroyed until the code resumes and get outside of
the scope where it was defined...
Am I missing something?
Jean
int CProtobufSocket::Receive(
string port,
google::protobuf::Message & message,
const boost::posix_time::milliseconds timeout
)
{
int result = -1;
boost::asio::io_service ioService;
boost::asio::deadline_timer mytimer(ioService);
//boost::asio::ip::tcp::acceptor acceptor(ioService,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
atoi(port.data())));
boost::asio::ip::tcp::acceptor acceptor(ioService,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 7777));
boost::asio::ip::tcp::socket socket(ioService);
try
{
acceptor.async_accept(socket,
boost::bind(&CProtobufSocket::HandleAccept,
boost::asio::placeholders::error, result));
//mytimer.expires_from_now(timeout);
mytimer.expires_from_now(timeout);
mytimer.async_wait(boost::bind(&CProtobufSocket::Close, &acceptor));
On Thu, Feb 18, 2010 at 9:18 AM, Igor R
try { boost::asio::deadline_timer mytimer(ioService);
acceptor.async_accept(socket, boost::bind(CProtobufSocket::HandleAccept, boost::asio::placeholders::error, result)); mytimer.expires_from_now(timeout); mytimer.async_wait(boost::bind(CProtobufSocket::Close, &acceptor));
In your real code do you also define the timer as a local object? If so, it's destroyed by the end of the scope. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Igor R
-
Jean-Sebastien Stoezel