开发者

Check if boost::asio buffer data is present before read()

I'm trying to port a piece of software I wrote with Unix sockets to a version with TCP sockets, using boost::asio. The program is intended to run on a Linux machine.

In the earlier version of the code (using开发者_JAVA技巧 Unix sockets) I used a simple check to see if there was new data on the socket buffer, and then proceeding with reading predictably structured data:

    ioctl(s_c, FIONREAD, &socketstatus);
    while (socketstatus > 0)
    {// do receive stuff
    ioctl(s_c, FIONREAD, &socketstatus);}

Is there any way to do something similar with boost::asio? Or some better alternatives?

Thank you in advance CB


Use bytes_readable, this implements what you want.

Implements the FIONREAD IO control command.

boost::asio::ip::tcp::socket socket(io_service); 
...
boost::asio::socket_base::bytes_readable command(true);
socket.io_control(command);
std::size_t bytes_readable = command.get();


You can extract the native descriptor from a boost::asio::ip::tcp::socket using the native() method, which should work just fine with your existing code.

Though, I question your motivation for doing this. The Asio event reactor implements polling mechanics using epoll on Linux. There should be no need to poll a socket for reading or writing outside of the io_service event loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜