The importance of proper packet serialization with Winsock?

Started by
4 comments, last by Sirisian 13 years, 1 month ago
I was reading this, http://www.amazon.co.../ref=pd_sim_b_3
and this, http://beej.us/guide...page/index.html
and finally this, http://www.gamedev.n...tion=rules&f=15

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?
Advertisement
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:
  • 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.
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.

I was reading this, http://www.amazon.co.../ref=pd_sim_b_3
and this, http://beej.us/guide...page/index.html
and finally this, http://www.gamedev.n...tion=rules&f=15

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


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.
enum Bool { True, False, FileNotFound };
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.

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.


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 :-)
enum Bool { True, False, FileNotFound };
Here's a quick introduction to writing a binary protocol. 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.

This topic is closed to new replies.

Advertisement