问题描述
我试图将一个我用Unix套接字编写的软件移植到带有TCP套接字的版本,使用boost :: asio。该程序打算在Linux机器上运行。
在早期版本的代码中(使用Unix套接字),我使用一个简单的检查看是否有新数据然后继续读取可预见的结构化数据:
ioctl(s_c,FIONREAD,& socketstatus);
pre>
while(socketstatus> 0)
{//接收东西
ioctl(s_c,FIONREAD,& socketstatus);}
有什么办法做类似于boost :: asio的东西吗?
或更好的替代品?
提前谢谢
CB方案
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 advanceCB
解决方案Use bytes_readable, this implements what you want.
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缓冲区数据是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!