简体   繁体   中英

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. 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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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