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:
class myclass{
...member...
public:
string serialize(){
std::ostringstream out();
out << member1;
out << member2;
out << member3;
return out.str();
}
}
... 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 ostringstream
with ostringstream(ios::bin)
... Will it solve the problem (provided that I will use a de-serialization function that will use a istringstream(ios::bin)
??? I'm confused about the whole picture, if you may spend a few clarifying lines that'll be much appreciated.
Thanks.