What is the Arg & a in the buffered_stream::buffered_stream constructor?

164 views Asked by At

http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/reference/buffered_stream/buffered_stream.html

I am considering using it as an intermediate buffer between a tcp stream and a can bus. I pass the buffer to the API concerned with writing to the can bus, which gets data using async_reads. The tcp side writes to the buffer using async_writes.

1

There are 1 answers

1
sehe On BEST ANSWER

Sure.

boost::asio::streambuf sb;

// now write:
{
    std::ostream os(&sb);
    os << "Hello 1 2 3" << std::flush;
}

// or read:
{   std::istream is(&sb);
    std::string s;
    int a, b, c;
    is >> s >> a >> b >> c;
}

Note that you can also use preconfigured streams that connect to sockets:

#include <boost/asio.hpp>
#include <iostream>

using boost::asio::ip::tcp;

int main() {
    tcp::iostream s("localhost", "http");

    s << "GET / HTTP/1.0\r\n\r\n" << std::flush;
    std::cout << s.rdbuf(); // print response
}