Daaksin
Daaksin

Reputation: 894

What is the usual way of sending data across network?

In C#, I use Binary Serialization to send information across the Stream. This is quite simple as all you do is create a class, serialize it and then deserialize it on the receiving end.

My question is, how can I do something similar in C++? All of the network examples I've seen sends a const char * like;

send("LOGIN");

If I wish to send a string and an integer over network, must I really go to the trouble of converting both to a string, putting a command in front of it, and then sending;

[PSEUDO-CODE]
send("COMMAND|" + tostring(someInteger) + "|" + someString);

Then on the receiving end, splitting this string and parsing? I imagine this would get complicated when I have a vector stored and I want to send this over the network.

What can I do?

Upvotes: 1

Views: 187

Answers (3)

dvasanth
dvasanth

Reputation: 1377

C# has Windows Communication Foundation(WCF) using which you can serialise & send messages across the network. It also handles underlying transport over HTTP, TCP or HTTPS. You can learn more about this here: http://msdn.microsoft.com/en-us/library/vstudio/ms731079%28v=vs.90%29.aspx

Upvotes: 1

James
James

Reputation: 9278

Google's protobuf library actually isn't bad

Upvotes: -1

There are several serialization formats and libraries (but outside of the C++11 standard).

In practice, your question is operating system specific. I guess you use Linux or some Posix system.

I usually recommend serializing in a textual format (easier to debug). Then consider formats like JSON (or YAML, perhaps even XML -but I don't recommend it since it is too complex- etc...) and use libraries like JSONCPP

In most cases (but not always!), network transport -milliseconds- is much slower than serialization computing -microseconds- so the textual serialization overhead does not matter much

If you want some binary serialization (beware of endianess issues), consider perhaps libs11n, XDR, etc... See also htonl(3)

Always document (and keep up to date the documentation) your protocol.

For transport, use TCP/IP (or something above it, e.g. HTTP, 0MQ, etc...). On Linux and Posix, see socket(7) and tcp(7) and read some tutorial on BSD sockets.

You may need to code some event loop using poll(2) (or use an existing event loop framework, libev, libevent, Glib from GTK, QtCore from Qt, etc etc...)

See also JSON-RPC, MPI etc

Upvotes: 2

Related Questions