I have read the documentation :-)
My understanding is that it's blocking if there's no data left to be read and you ask it to read from the socket. In my case, the server has sent the data and it's there waiting for it to read. I've confirmed this via wireshark and, as I mentioned, the plain POSIX call to read (which is also blocking by the way) works as expected.
________________________________
From: nullable.type
To: boostusers@googlegroups.com
Cc: "boost-users@lists.boost.org" ; boost-users@lists.boost.org; ssiegel95@yahoo.com
Sent: Thursday, October 25, 2012 3:34 AM
Subject: Re: [Boost-users] Occasional blocking with tcp::socket::receive
Hi
Read the documentation:
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/basic_str...
A call to boost::asio::ip::tcp::socket::receive(..) IS a blocking call. If you wannt to avoid blocking, just use the async_* functions.
Cheers,
Simon
On Friday, October 19, 2012 3:50:11 PM UTC+2, Stuart Siegel wrote:
The call to receive below will occasionally hang. All interactions are synchronous and single threaded. Things work perfectly if I use the alternative (POSIX) implementation under the #else. Since I'm pretty new to boost::asio, I must be doing something wrong but after many hours of trying different things, I'm still stuck. Any advice would be appreciated. #if 1
namespace bas = boost::asio; remaining = X //(something typically about 5000 bytes of char data) bas::streambuf b;
bas::streambuf::mutable_ buffers_type bufs = b.prepare(1024); while (remaining > 0) { size_t n = sock.receive(bufs); //<-------------this blocks every so often remaining -= n; for ( bas::buffers_iterator it = bas::buffers_begin(bufs); it < (bas::buffers_begin(bufs) + n); it++ ) qba.append(*it); }
#else //this works perfectly in the 250000 or so test calls I've hit it with char buf[1024]; while (remaining > 0) { n = read( sock, buf, min( sizeof(buf), remaining ) ); remaining -= n; for (int i = 0 ; i < n; i++ ) qba.append(buf); }
#endif