packet parsing

Started by
50 comments, last by hplus0603 16 years, 2 months ago
im making a UDP client server app, i have an app that sends an array of bytes via UDP to another socket, which recieves it. Now - how do i insert data into the bytes for the send packet, and then parse it back into usable data on the receiving end? i havent had much luck searching for an answer, i think im searching for the wrong keywords. could someone give me some code or point me to a tutorial? -andrew
Advertisement

I think you want to marshall/serialize the data. What language
are you using?

Brian Wood
Ebenezer Enterprises
vc++
i looked into serialization, but im not sure that's exactly what i want.

i just need to be able to send, say:

int_MsgFlagValue, int_value, int_value, float_value

as a packet, then turn it back into the usuable variables on the receiving side


this is for a client-server game to transmit game info.
The Forum FAQ says that you can stuff your data into a struct, and then use the address and size of that struct as the data you pass to send() and recv().
Or you can memcpy() between your struct and some char array.
enum Bool { True, False, FileNotFound };
Binary Packet

That might help you. There's a C++ example at the bottom, but it's not as efficient as it can be, but works.

hplus, your networking ability and programming is probably much better than mine. Have you thought of writing one of these as a solid example. (I've been away from C++ for a long time, and I don't know any of the tricks for optimizing these operations).
Actualy the way i do this is write a BitStream class which alows you to pack data together: bits, bytes, strings etc. And you create a function in that class to retreive the pointer which you use to send the data. This way it is very easy to reuse code, write wathever data to your stream.

So you could do this :

BitStream Stream;

Stream.Write(&MsgFlagValue,sizeof(int) * 8); // overloads int
Stream.Write(&value1,sizeof(int) * 8); // overloads int
Stream.Write(&value2,sizeof(int) * 8); // overloads int
Stream.Write(&value3,sizeof(int) * 8); // overloads int

SocketLayer->SendTo(Stream.GetData(),etc..);
Hope this helps.

Cheers
Ponl
Quote:Have you thought of writing one of these as a solid example.


I already did.
enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
Quote:Have you thought of writing one of these as a solid example.


I already did.

Yeah I've seen the buffer handled like that, but I meant more like this:
C# example
It's C#, and is still in development, but shows the basic idea.

A C++ version would have support for all the basic containers in C++ like std::map, std::vector, std::list and such. Also optimized forms of things like the custom floating point precision. (I wrote mine up and noticed there's an example of the concept in the FAQ). Also std::string supporting 7bit or more optimized custom serialization. It's fun trying to see how efficient you can make things. Makes development much quicker for serializing objects. Things like WriteID also forms a standard that people can understand in formatting their messages.

I've been using this format for a while and it's much easier to deal with more complex games that require serialization or efficiency. Each bit can be used easily without worrying about the inner workings. As long as one knows the format they can use the packet to read and write. I'm still researching the theory of optimizing packet formats, but I imagine it can be used much more efficiently than what normal games do.
Quote:Original post by Sirisian

A C++ version would have support for all the basic containers in C++ like std::map, std::vector, std::list and such. Also optimized forms of things like the custom floating point precision. (I wrote mine up and noticed there's an example of the concept in the FAQ). Also std::string supporting 7bit or more optimized custom serialization. It's fun trying to see how efficient you can make things. Makes development much quicker for serializing objects. Things like WriteID also forms a standard that people can understand in formatting their messages.


GPG 3, 5.5 covers this very topic in great detail, and in a very sane manner.

Outside of copying that, most of the rest is really just tiny tweaks and trade-off strategies.

This topic is closed to new replies.

Advertisement