在 read() 之前检查 boost::asio 缓冲区数据是否存在

发布于 2024-11-25 07:52:47 字数 399 浏览 0 评论 0 原文

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

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

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

是否有任何方法可以使用 boost:: 执行类似的操作亚西欧? 或者一些更好的选择?

先感谢您 CB

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-12-02 07:52:47

使用bytes_read,这实现了你想要什么。

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

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();
屌丝范 2024-12-02 07:52:47

您可以使用 native() 方法,它应该可以很好地与您现有的 代码。

不过,我质疑你这样做的动机。 Asio 事件反应器在 epoll 实现轮询机制rel="nofollow">Linux。在 io_service 事件循环之外不需要轮询套接字进行读取或写入。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文