title: Network protocol object serialization in C++
body: <p>I'm writing some C++ code that will have to send data over TCP/IP. I want this code to be portable on Linux/Windows/Osx. Now, as it is the first time I write portable network code, I basically need some simple functions to add to certain objects like:</p>
  
  <pre><code>class myclass{
  
  ...member...
  
  public:
  
    string serialize(){
      std::ostringstream out();
      out &lt;&lt; member1;
      out &lt;&lt; member2;
      out &lt;&lt; member3;
      return out.str();
    }
  
  }
  </code></pre>
  
  <p>... which is all I need for now. Anyway I started reading ostringstream related docs and turns out the binary/text problem. In fact it will convert line breaks to the right sequence of everysystem. Suppose for example that a member is a pointer to const char* foo = "Hello\nMan\n", that will be translated in certain byte sequence on linux, another on windows... and so on. My bytes will go on a packet over the internet, a different OS machine will read them and I think trouble will occurr... Now I read that I might initialize <code>ostringstream</code> with <code>ostringstream(ios::bin)</code>... Will it solve the problem (provided that I will use a de-serialization function that will use a <code>istringstream(ios::bin)</code>??? I'm confused about the whole picture, if you may spend a few clarifying lines that'll be much appreciated. </p>
  
  <p>Thanks. </p>