Friends can you please tell me how can i send object of a class over a network to the client??

Recommended Answers

All 6 Replies

On client, send the object just like you would send any other blob, then on client recreate it from the blob. Probably something like below. Make sure the class has an overloaded = operator for this to work.

// server
MyClass object;
...
send(socketID, &object, sizeof(MyClass));
// client
// this isn't going to be technically accurate, but gives you the idea.
unsigned char* buf = recv(sucketID);
MyObject object = *(MyObject*)buf;

Wow sending data is that simplified in C++?

My god, that is incredibly easy.

Well, its not quite as easy as what I posted, there is code that I omitted. Also it may be more complex depending on the class objects, such as pointers, other classes, etc.

consider using a textual format (maybe xml).

text is a universal format; it is easy for people to read, write, and edit.
and the network payload is easier to inspect, to modify and to debug.

text data is transparent and help enforce encapsulation between programs.

a textual protocol tends to future-proof your system. one reason is that ranges on numeric fields, padding of members and endianness aren't implied by the format itself. on the other hand, binary formats are platform specific and are difficult to extend.

I have read about Object Serialization in my JAVA book. Could it be possible with C++?

I have read about Object Serialization in my JAVA book. Could it be possible with C++?

See e.g. here

commented: thanks +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.