在read()之前检查boost :: asio缓冲区数据是否存在 [英] Check if boost::asio buffer data is present before read()

查看:794
本文介绍了在read()之前检查boost :: asio缓冲区数据是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个我用Unix套接字编写的软件移植到带有TCP套接字的版本,使用boost :: asio。该程序打算在Linux机器上运行。



在早期版本的代码中(使用Unix套接字),我使用一个简单的检查看是否有新数据然后继续读取可预见的结构化数据:

  ioctl(s_c,FIONREAD,& socketstatus); 
while(socketstatus> 0)
{//接收东西
ioctl(s_c,FIONREAD,& socketstatus);}
pre>

有什么办法做类似于boost :: asio的东西吗?
或更好的替代品?



提前谢谢
CB

方案

使用


实现FIONREAD IO控制命令。




  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();


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 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();

这篇关于在read()之前检查boost :: asio缓冲区数据是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆