Is there an elegant way to bridge two devices/streams in Asio?
-
12-09-2019 - |
Question
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.
Solution
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow