How to build my own packets ?
I want to know how to include datatypes other than char (like float) in a packet when I''m send()ing. How can I do this?
I tried to make a struct that had different datatypes and then converting it to char just before send()ing it.
Is there any good example or article on this?
in memory, everything is represented as bytes (at least this is how I think of it)...
a C char is one byte, a float is 4. if you say
the memory used to store the float overlaps exactly with the char array. this means if you modify f, and access c, c will also will have changed.
you can also do this:
at which point a acts just like the char c[4] in the union.
if you want more complex stuff (like an entire packet in one struct like it sounds like you want), you can:
[edited by - Nypyren on February 28, 2003 5:26:14 AM]
a C char is one byte, a float is 4. if you say
union whatever{ float f; char c[4];}
the memory used to store the float overlaps exactly with the char array. this means if you modify f, and access c, c will also will have changed.
you can also do this:
float f = 10.0f;char *a = (char *)(&f);
at which point a acts just like the char c[4] in the union.
if you want more complex stuff (like an entire packet in one struct like it sounds like you want), you can:
#pragma pack 1 // or whatever your compiler uses to modify byte-alignment in structsunion{ struct s { int i; // 4 bytes float f1; // 4 bytes float f2; // 4 bytes double d; // 8 bytes } char c[20];|
[edited by - Nypyren on February 28, 2003 5:26:14 AM]
There are byte alignment issues as well.
Decide on wether data should be sent BigEndian or LittleEndian and write conversion macros/methods for your basic types such as int16, int32, float, double etc.
This is not an issue if all applications will use the same endianness but it is such a small thing to do and it is much harder to add later when you decide to port your stuff to MacOS, Gamecube or whatever.
/ Bucko aka Backman
Decide on wether data should be sent BigEndian or LittleEndian and write conversion macros/methods for your basic types such as int16, int32, float, double etc.
This is not an issue if all applications will use the same endianness but it is such a small thing to do and it is much harder to add later when you decide to port your stuff to MacOS, Gamecube or whatever.
/ Bucko aka Backman
/ Bucko aka Backman
are there processor instructions which reverse byte ordering or do we have to do it using some sort of iteration?
George D. Filiotis
![](http://www.spforge.com/logo.jpg)
I am a signature virus. Please add me to your signature so that I may multiply.
George D. Filiotis
![](http://www.spforge.com/logo.jpg)
I am a signature virus. Please add me to your signature so that I may multiply.
Geordi
George D. Filiotis
George D. Filiotis
You can use the standard htonl(htons) functions if you want, or you can write your own. If you are going from intel to intel (even if it''s linux to windows or vice versa), then I''d skip this step altogether as it''s going to use the same ordering on either side anyways. You still have to put your port number in network order for setting the address data though...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement