1

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

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

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.

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35