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

时间:2022-09-09 11:08:13

I'm using boost::asio::ip::tcp::iostream to read binary data from TCP stream. I do this like that:

我正在使用boost :: asio :: ip :: tcp :: iostream从TCP流中读取二进制数据。我这样做:

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会话已完成)?

1 个解决方案

#1


0  

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. You can set flags on the stream object ( exception() ) to instruct the stream to throw exceptions in case, that an error occurred. This makes handling some times easier.

使用good()和!eof()来查看是否有数据。您可以在流对象(exception())上设置标志,以指示流在发生错误的情况下抛出异常。这使处理更容易一些。

#1


0  

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. You can set flags on the stream object ( exception() ) to instruct the stream to throw exceptions in case, that an error occurred. This makes handling some times easier.

使用good()和!eof()来查看是否有数据。您可以在流对象(exception())上设置标志,以指示流在发生错误的情况下抛出异常。这使处理更容易一些。