title: Sending a StringStream of Binary Data from Cereal with ENet
body: <p>I have been working on wrapping ENet into a set of easy to use functions for a few weeks now and seem to have a bit of an issue.</p>
  
  <p>I have a std::stringstream and am attempting to send the contents to a remote machine using ENet then reconstruct the std::stringstream on the remote machine.</p>
  
  <p>The reason I need to use a std::stringstream is due to the fact that I'm serializing my data with the Cereal Serialization Library which requires a stream.</p>
  
  <p>With Azoth's help he has identified that I need to be using std::istringstream and std::ostringstream. Previously I was only using std::stringstream which was causing an exception.</p>
  
  <p>However now an exception is being thrown within Cereal at portable_binary.hpp line 156:</p>
  
  <pre><code>throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
  </code></pre>
  
  <p>Here's what I'm doing:</p>
  
  <pre><code>void Send(ENetHost* Host)
  {
      std::ostringstream SData;
      {
          cereal::PortableBinaryOutputArchive Archive(SData);
          Archive(PacketData);
      }
  
      std::string Out = SData.str();
  
      ENetPacket* Packet = enet_packet_create(Out.c_str(), Out.size(), ENET_PACKET_FLAG_RELIABLE);
      enet_host_broadcast(Host, 0, Packet);
  }
  </code></pre>
  
  <p>A Cereal Portable Binary Data Archive is constructed to hold a single vector.
  The std::ostringstream is sent off to the host using ENet.</p>
  
  <p>This part seems to work okay, I can print the information out before and after and it appears to be the same, albeit some weird symbols, but they print out the same on both ends.</p>
  
  <p>Now a std::istringstream is created on the host with the data we received.</p>
  
  <pre><code>NetPacket(enet_uint8 const* Data)
  {
      std::istringstream SData(reinterpret_cast&lt;char const*&gt;(Data));
      {
          cereal::PortableBinaryInputArchive Archive(SData);
          Archive(PacketData);
      }
  }
  </code></pre>
  
  <p>At this point I receive the exception at line:</p>
  
  <pre><code>Archive(PacketData)
  </code></pre>
  
  <p>I have a feeling the data is being changed somehow when it's sent through ENet and/or I'm not pulling the data out of the std::ostringstream correctly and/or not putting the data back into the std::istringstream correctly.</p>
  
  <p>Thank you very much for your time I greatly appreciate it.</p>