views:

210

answers:

1

Given two stream-oriented I/O objects in Asio, what is the simplest way to forward data from one device to the other in both directions? Could this be done with boost::iostreams::combination or boost::iostreams:copy perhaps? Or is a manual approach better--waiting for data on each end and then writing it out to the other stream? In other words,how does one leverage Boost and Asio to produce a minimal amount of code?

An example application would be streaming between a serial port and TCP socket as requested in this question.

+3  A: 

With standard C++ streams you can do the following, can't you do something similar with Asio?

// Read all data from in and write to out.
void forward_data( std::istream& in, std::ostream& out )
{
  out << in.rdbuf();
}
Peter Jansson
Does that block until the input stream is closed? What about bidirectional streams?
Judge Maygarden
out will be fed data until in end-of-file is reached, you will have to look into how Asio implements the buffer; if an end-of-file is sent when the input is closed or not. rdbuf() is a method of std::ios so it's available on both std::istream and std::ostream.
Peter Jansson