简体   繁体   English

如何知道boost :: asio :: ip :: tcp :: iostream上没有可用数据?

[英]how to know that no data available on boost::asio::ip::tcp::iostream?

I'm using boost::asio::ip::tcp::iostream to read binary data from TCP stream. 我正在使用boost::asio::ip::tcp::iostream从TCP流读取二进制数据。 I do this like that: 我这样做是这样的:

stream.read(reinterpret_cast<char*>(&packetSize), 4); // first 4 bytes is length
stream.read(buffer, packetSize);

Should I just check stream.gcount() and when next stream.gcount() == 0 that means that no data more available for reading (TCP session is finished)? 我应该只检查stream.gcount() ,当下一个stream.gcount() == 0 ,意味着没有更多数据可读取(TCP会话完成)?

The stream members you are looking for are deeply buried in the base class http://en.cppreference.com/w/cpp/io/basic_ios 您要查找的流成员深深地埋入了基类http://en.cppreference.com/w/cpp/io/basic_ios

Use good() and !eof() to see if you got the data. 使用good()和!eof()来查看是否有数据。 You can set flags on the stream object ( exception() ) to instruct the stream to throw exceptions in case, that an error occurred. 您可以在流对象上设置标志(exception()),以指示流在发生错误的情况下引发异常。 This makes handling some times easier. 这使得处理有时更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM