0

I am trying to send ostream which contains binary data by socket in C++.

I'm using Microsoft SEAL for developing about encryption.
Now I need to send a ciphertext from client to server. Microsoft SEAL provides a function

void Ciphertext::save(ostream &stream) const

to serialize the Ciphertext object to ostream, but after calling

void Ciphertext::save(ostream &stream) const,

I have no idea how to send the ciphertext (which is in ostream) by socket in client (of course also have no idea how to receive in server).

I tried the solution, but it seems that it could not work when the data is binary data.

I would like to know how to write the code of sending ostream in client and receiving in server. Thank you.

Adam
  • 2,726
  • 1
  • 9
  • 22
YD821607
  • 3
  • 1
  • 1
    You could write (or find) a custom `std::streambuf` class that wraps your socket, then you can assign that class to a standard `std::ostream`, thus allowing `save()` to output (in)directly to your socket. – Remy Lebeau May 25 '19 at 03:23

1 Answers1

0

You can pass it ostringstream to fill and the extact the string containing binary data from it:

#include <sstream>
#include <string>
// ...
std::string save_into_string(Ciphertext const& ciphertext) {
    std::ostringstream s;
    ciphertext.save(s);
    return s.str();
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271