Sign in to follow this  

The importance of proper packet serialization with Winsock?

This topic is 2481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was reading this, [url="http://www.amazon.com/TCP-Sockets-Bundle-Practical-Programmers/dp/0123745403/ref=pd_sim_b_3"]http://www.amazon.co.../ref=pd_sim_b_3[/url]
and this, [url="http://beej.us/guide/bgnet/output/html/multipage/index.html"]http://beej.us/guide...page/index.html[/url]
and finally this, [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]http://www.gamedev.n...tion=rules&f=15[/url]

I like the FAQ method the most because it's extremely simple- you just send the data without any kind of framing, encoding/decoding, or the use of the C99 standard types- which to me seems like a big project in itself just to send basic data structures. What I'm interested in knowing is exactly what trade offs will I be making if I choose the simplified method over a "proper" one. The most obvious consequence would be reducing the portability my application since I'm not using the standard C99 types but would that actually affect me if I only develop for Windows 7 and XP and don't use any 64 bit data types? Would it affect performance? Is there anything else?

Share this post


Link to post
Share on other sites
Unfortunately I don't have time to read all those sources to see the difference between the methods you mention. However I don't think the Beej guide has anything to say about this, does it?

But anyway, sending data via trivial binary copying is not as simple as it appears. For example:
[list][*]if you have pointers or non-plain data types in there (eg. strings, lists, maps, whatever) then that data will not be sent, and the resulting variable may crash the program if used[*] if the struct padding settings are different across the two programs then the data will end up in the wrong place and appear corrupted[*]if you change the struct on one program but not the other (for example, say you upgrade the server but an old client tries to connect), again the data will end up in the wrong place and will be corrupted[*]the other side still has to be told what struct it's actually expecting - this usually means you need 2 structs per message, one which is some sort of header telling you what sort of message it is, and then follow that with the message contents struct itself. Some systems unify this by using unions.[/list]So you either have to be very careful about how you pack your data into the structs, or you write a serialisation/deserialisation system to convert your objects into network messages and back again. To decide what is best for you, it might be worth going through a sample of messages you're likely to send and consider how easy it will be to fit them into a simple struct. If most or all of your data can easily be handled that way, then you may as well go with that rather than a complex serialisation approach. But often, you do need a bit more than just to exchange simple data.

Winsock is nothing to do with it, by the way - it just puts the bytes onto the network and pulls it off again. It doesn't care how you got those bytes or what you do with them.

Share this post


Link to post
Share on other sites
[quote name='FordPrefectA' timestamp='1298170664' post='4776540']
I was reading this, [url="http://www.amazon.com/TCP-Sockets-Bundle-Practical-Programmers/dp/0123745403/ref=pd_sim_b_3"]http://www.amazon.co.../ref=pd_sim_b_3[/url]
and this, [url="http://beej.us/guide/bgnet/output/html/multipage/index.html"]http://beej.us/guide...page/index.html[/url]
and finally this, [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=15"]http://www.gamedev.n...tion=rules&f=15[/url]

I like the FAQ method the most because it's extremely simple- you just send the data without any kind of framing, encoding/decoding, or the use of the C99 standard types- which to me seems like a big project in itself
[/quote]

If you can put the bits in one end, and understand the bits on the other end, and the bits are able to support your use cases with acceptable performance, then you're done.
Note that some use cases may require fancier formatting of the bits -- forward/backward compatibility, version negotiation, decoding packets captured from the wire, and others.

Share this post


Link to post
Share on other sites
[quote name='FordPrefectA' timestamp='1298838566' post='4779811']
Are there any comprehensive books or other sources that you know of about how to do it properly? The books I've seen on networking don't talk about it at all or just skim over it.
[/quote]

When you think too hard about it, you end up with CORBA, or DCOM, or Thrift or something. It just isn't that hard -- it's bits. It's information theory. If you have a solid CS background (especially in telecommunications and network protocols), it's just something you do. If you don't, then I recommend some CS textbooks on telecom and network protocols :-)

Share this post


Link to post
Share on other sites
[url="http://wiki.gamedev.net/index.php/Binary_Packet"]Here's a quick introduction to writing a binary protocol.[/url] Most people end up figure it out as Hplus mentioned. I think I linked this to you in IRC a few weeks ago though. I can't remember if you're the same person though.

Share this post


Link to post
Share on other sites

This topic is 2481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this