Assuming you use asynchronous methods, the completion handler given to async_read
will indicate the number of bytes received. Similarly, the completion handler given to async_write
will indicate the number of bytes written. It would be trivial to maintain a running counter as a member of a class where you bind methods as the previously described completion handlers.
#include <boost/asio.hpp>
#include <iostream>
class Socket
{
public:
Socket(
boost::asio::io_service& io_service
) :
_socket( io_service ),
_counter( 0 )
{
}
void readHandler(
const boost::system::error_code& error,
std::size_t bytes_transferred
)
{
_counter += bytes_transferred;
}
void writeHHandler(
const boost::system::error_code& error,
std::size_t bytes_transferred
)
{
_counter += bytes_transferred;
}
private:
boost::asio::ip::tcp::socket _socket;
std::size_t _counter;
};
int
main()
{
boost::asio::io_service io_service;
Socket foo( io_service );
}