简体   繁体   中英

Determine amount of available bytes in socket

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM