Listing 4: Skeleton of the custom socket-based stream buffer

template
<
    class charT,
    class traits = std::char_traits<charT>
>
class TCPStreamBuffer :
    public std::basic_streambuf<charT, traits>
{
    typedef std::basic_streambuf<charT, traits> sbuftype;
    typedef typename sbuftype::int_type         int_type;
    typedef charT                               char_type;
    // ...

public:
    // the buffer will take ownership of the socket
    // (ie. it will close it in the destructor)
    // if takeowner == true
    explicit TCPStreamBuffer (TCPSocketWrapper &sock,
                bool takeowner = false,
                int_type bufsize = 512);

    // ...

protected:
    int_type overflow(int_type c = traits::eof());
    int_type underflow();

    // ...

private:
    // copy not supported
    TCPStreamBuffer(const TCPStreamBuffer&);
    TCPStreamBuffer& operator=(const TCPStreamBuffer&);

    TCPSocketWrapper &rsocket_;
    bool ownsocket_;

    // ...
};
— End of Listing —