I have a boost::asio::ip::tcp::socket
and want to determine if the socket contains data which is available to read. I read about available()
, however that function sometimes also returns 0
if there is data available (in case it can be read without blocking). The executable is running on Ubuntu - maybe a system call could help?
Asked
Active
Viewed 427 times
1

Bobface
- 2,782
- 4
- 24
- 61
-
If no data is available for a read I would continue and check back later. I know async_read would solve that too, but I already have a big polling loop so it would be perfect if there was a simple way of checking the available bytes. – Bobface Jan 10 '17 at 19:01
-
At the downvoter: Why did you downvote? Can I add any more important information? – Bobface Jan 10 '17 at 19:01
-
As you said, I all I need to know if a socket contains readable data. Is there a function to do that? – Bobface Jan 10 '17 at 19:09
-
Maybe http://stackoverflow.com/q/12984816/1741542 answers your question. – Olaf Dietsche Jan 10 '17 at 19:12
1 Answers
2
Since you just need to know whether the socket contains readable data, just issue your regular non-blocking read
from the socket. If there is data, you need to read it anyway, so just attempt the read.
If you're trying to avoid the expense of a read
if there's nothing to read, realize that you're just adding an extra operation. Checking if there's data to read is not significantly cheaper than attempting to read.
Of course, just having a pending async_read
is better. This lets Boost's reactor do its job.

David Schwartz
- 179,497
- 17
- 214
- 278