title: C++ STL-based sending of binary serialized data for network transmission using sockets without the use of libraries
body: <p>I need to send a number of complex objects over the network to a peer. I have written the code to serialize them using ostream and the operator&lt;&lt; for each class member in the objects that need to be serialized. The code I have written works successfully for serialization and network sending (htonl(), htons() etc. correctly used--I checked the aforementioned by writing to an ofstream (local file) in binary format (std::ios::bin). My next task, writing this binary data over the network socket, is where I am having issues.</p>
  
  <p>I have Socket class which translates std::string objects into C-style strings before sending them over the socket like so: </p>
  
  <pre><code>int Socket::send (const std::string goodies) const
  {
      status = ::send (socket, goodies.c_str(), goodies.size(), 0);
              return status;
  }
  </code></pre>
  
  <p>the same Socket class, which I use in the receiver, uses the recv() to place the incoming message into a std::string before passing it to the deserializing application:</p>
  
  <pre><code>int Socket::recv (std::string&amp; goodies)
  {
      char buf [1024];
      goodies = "";
      memset (buf, 0, 1025);
  
      int status = ::recv (socket, buf, 1024, 0);
  
      if (status &lt; 0)
      {
          return -1;
      }
      else if (status == 0)
      {
          return 0;
      }
      else
      {
          goodies = buf;
          return status;
      }
  }
  </code></pre>
  
  <p>I do the sending using the following code:</p>
  
  <pre><code>ostringstream os (std::ios::binary);
  GiantObject giantComplexObjectWithWholeLoadOfOtherObjects;
  // Initialize and set up 
  // giantComplexObjectWithWholeLoadOfOtherObjects.
  
  // The following call works well--I tested it by writing to ofstream locally (file)
  // and checked it out using a hex dump.
  // Now, of course, my intent is to send it over the network, so I write to
  // ostream&amp;:
  giantComplexObjectWithWholeLoadOfOtherObjects.serialize (os);
  
  std::string someStr = os.str(); // Get me the stream in std::string format
  
  mySocket.send(someStr); // Does not work--send sent correctly, but recv received 0 bytes
  </code></pre>
  
  <p>However, if I try:</p>
  
  <pre><code>std::string someStr ("Some silly string");
  mySocket.send (someStr); // received by receiver (receiver is similar arch).
  </code></pre>
  
  <p>Thereby, something is not right about my call to send binary std::string to the socket. Any help is greatly appreciated. Once again, I do not want to use Boost, protobuf etc.</p>
  
  <p>PS: I have spent a considerable amount of time looking over the old posts here, and the first response these types of questions receive is to use Boost. Please--there are other non-Boost, non-Protobuf ways, and I want to understand those other ways. I do appreciate what Boost and Protobuf bring to the table, I just want to do this in the manner I have set up the code.</p>