在 read() 之前检查 boost::asio 缓冲区数据是否存在
我正在尝试使用 boost::asio 将我用 Unix 套接字编写的一个软件移植到带有 TCP 套接字的版本。该程序旨在在 Linux 机器上运行。
在早期版本的代码(使用 Unix 套接字)中,我使用了一个简单的检查来查看套接字缓冲区上是否有新数据,然后继续读取可预测的结构化数据:
ioctl(s_c, FIONREAD, &socketstatus);
while (socketstatus > 0)
{// do receive stuff
ioctl(s_c, FIONREAD, &socketstatus);}
是否有任何方法可以使用 boost:: 执行类似的操作亚西欧? 或者一些更好的选择?
先感谢您 CB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用bytes_read,这实现了你想要什么。
Use bytes_readable, this implements what you want.
您可以使用
native()
方法,它应该可以很好地与您现有的 代码。不过,我质疑你这样做的动机。 Asio 事件反应器在 epoll 实现轮询机制rel="nofollow">Linux。在 io_service 事件循环之外不需要轮询套接字进行读取或写入。
You can extract the native descriptor from a
boost::asio::ip::tcp::socket
using thenative()
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 theio_service
event loop.