🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Sending objects as packets

Started by
2 comments, last by bose 22 years, 7 months ago
I want to create packet objects or structures to send as pakcte information along my socket to all connected clients. How would I serialize a structure or object in order to send it along winsock as a buffer? Thanks
Advertisement
It really depends. If your structure is just that - a flat structure - you can pass it directly as buffer to send(). However, if it contains pointers, you''ll just have to write a function which translates that structure (and potentially the stuff the pointers point to) into such a flat struct.

Note that a class is exactly the same as a struct in this case. Note that if your class or struct contains virtual functions, you''ll have a VMT pointer there.

On the receiving end it''s really the same. If it''s a flat structure, you can just use a local structure as recv() buffer which will then be filled in (in reality, it''ll be a bit more complex with intermediate buffering in TCP), and in the complex case you''ll need another translation function.

You''ll almost always write such a translation function. I always end up having structures in two different layouts, one local and one for the network, where the networking structures are e.g. more packed, have string table indexes instead of string pointers etc...

cu,
Prefect
Widelands - laid back, free software strategy
Hi

Take a look at the MFC Serialization mechanisme.
It is also described on my homepage.

www.bunnz.com (PurpleLib)

Have fun
Bunnz
Have fun BunnzPunch'n'Crunch
I am using CAsyncSocket which doesn''t work with CArchive.

Does anyone know how to serialize a class without using CArchive? Or some kind of way to make the object persistant?

I tried casting the class like:

Send((void*)&PacketObject, sizeof(PacketType));

Which DOES work to a point, but for some reason it gets messed up after sending 1 or 2 packets. Can''t figure out why!

Thanks

This topic is closed to new replies.

Advertisement