'iostream like networking in C++?

is it possible in C++ to do stream like networking?

Something like:

sstream<"www.google.com"> google;
sstream<socket_data> data;
google << "hello";//send hello
google >> data;//read response
if(data.size() > 0)//ok
{
    //manipulate data
}

If yes, how?

I cannot find any information on this topic.



Solution 1:[1]

I wrote crossplatform network you can find the source here: https://bitbucket.org/ptroen/crossplatformnetwork/src/master/ (it's built on top of boost::asio)

The library generalizes a network protocol quite a bit while getting high performance. For instance this would be a http client (extracted from https://bitbucket.org/ptroen/crossplatformnetwork/src/master/OSI/Application/Stub/HTTPClient/main.cc) OSI::Transport::Interface::IClientTransportInitializationParameters init_parameters; init_parameters.ParseServerArgs(&(*argv), argc, 80, 80);

OSI::Transport::HTTP::HTTPClientTransport<SampleProtocol::IncomingPayload<OSI::Transport::Interface::IClientTransportInitializationParameters>, SampleProtocol::OutgoingPayload<OSI::Transport::Interface::IClientTransportInitializationParameters>, SampleProtocol::SampleProtocolClientSession<OSI::Transport::Interface::IClientTransportInitializationParameters>, OSI::Transport::Interface::IClientTransportInitializationParameters> client(init_parameters);
SampleProtocol::IncomingPayload< OSI::Transport::Interface::IClientTransportInitializationParameters> request(init_parameters);
client.RunClient((char*)init_parameters.ipAddress.c_str(), request);

where SampleProtocol is just a class with methods that are called by the templates(see size(),max_size(),ToString() and FromString() but if your in a hurry you could probably just use it as your payload.

With a little work you can wrap it with sstream like interface to achieve that network iostream effect and even pass it to sstream internally if you want extractors.For instance here's a reference on how to derive from std::ostream https://horstmann.com/cpp/iostreams.html (It's used for a different use case but is good reference reading).

Anways to make a network stream library the recipe you need todo is the following:

  1. Make a sstream like class overloading the extraction operators to pass in a string and vice versa.
  2. In the constructor span a new thread(via std::thread) to call the networking code above
  3. Add into the sample protocol(see above) std::atomic or a mutex inside the tostring and from string to pass to the main thread the asyncronous messages back. Basically when you need a mechanism to pass back to the main thread that a payload is ready so your iostream wrapper can pick it up on the main thread. Or you could do blocking(https://www.tutorialspoint.com/what-is-blocking-networks-and-non-blocking-networks-in-computer-architecture) implementation which doesn't require threading but then your networking performance would be worse(because your blocking).

If you want blocking you could just use invoke curl(see here https://linuxize.com/post/curl-command-examples/ if windows install cgywin) on the command line which then a separate thread is not necessary. After curl is invoked take the stream, parse the command line response and pass it back to a string and insert inside your iostream class. The other drawback with this curl approach is you basically have to implement a http client parser of sorts.

Have a nice day :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1