1

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

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

我是否应该检查一下stream.gcount(),下一次stream.gcount() == 0是否意味着没有更多可读取的数据(TCP 会话已完成)?

4

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

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.

于 2013-05-15T12:30:07.823 回答