In this hands-on example, we will be creating a serial-based echo server. An echo server (regardless of the type) takes an input and echoes the input to the program's output (similar to the first example, but in this case using a server-style application over a serial port).
To see the full sources for this example, please see the following: https://github.com/PacktPublishing/Hands-On-System-Programming-with-CPP/blob/master/Chapter06/example2.cpp.
#include <fstream>
#include <iostream>
#include <gsl/gsl>
using namespace gsl;
void
redirect_output(
const std::ifstream &is,
const std::ofstream &os,
std::function<void()> f)
{
auto cinrdbuf = std::cin.rdbuf();
auto coutrdbuf = std::cout.rdbuf();
std::cin.rdbuf(is.rdbuf());
std::cout.rdbuf(os.rdbuf());
f();
std::cin.rdbuf(cinrdbuf...