Question

I am following the link below to implement a blocking read with timeout.

http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/example/timeouts/blocking_tcp_client.cpp

It works fine. Now I want to implement another function that do not use timeout. so I have tried to call directly:

socket_.async_receive(boost::asio::buffer(buf,size),
         boost::bind(&TCPClient::HandleReceive, _1, _2, &ec, &length));

The problem is the function do not block and return immediately with length 0.

Which function should I call before async_receive to block indefinitely?

Regards

Était-ce utile?

La solution

The prefix async implies that the function runs asynchronously.

Simply call socket::receive instead.

If you need to read a fixed number of bytes before the call returns, you'd better look at asio::read, because the receive operation may not receive all of the requested number of bytes.

Autres conseils

You can just call io_service::run() to wait for the work to complete.

Remember to reset() the service before using it for more work, though.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top