[Boost-users] boost::asio - Can't get timeout on accept to work

906 views
Skip to first unread message

Jean-Sebastien Stoezel

unread,
Feb 18, 2010, 9:26:06 AM2/18/10
to boost...@lists.boost.org
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/accept_timeout.cpp

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;
}
}
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Igor R

unread,
Feb 18, 2010, 10:18:10 AM2/18/10
to boost...@lists.boost.org
>   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.

Igor R

unread,
Feb 18, 2010, 10:21:14 AM2/18/10
to boost...@lists.boost.org
...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.

Jean-Sebastien Stoezel

unread,
Feb 18, 2010, 10:26:02 AM2/18/10
to boost...@lists.boost.org
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));

Igor R

unread,
Feb 18, 2010, 10:32:06 AM2/18/10
to boost...@lists.boost.org
> Since mytimer.async_wait woudl block, my understanding is that the timer would not be destroyed

async_XXX operations never block, that's why they're called asynchronous :).
You can use a blocking version: myTimer.wait(), if that's what you need.

Reply all
Reply to author
Forward
0 new messages