Quote Originally Posted by monarch_dodra View Post
Well for starters, you need to guarantee that things like u_short is indeed 2 bytes long. I don't know what your u_short is, but if it is just an alias on the type unsigned short, it could be 4 bytes long.

An option would be to use the low level C bit fields:

Code:
struct msg
{
     //struct in_addr addr1, addr2;  // 4-byte
    u_int32_t s : 32;             //4-byte
    u_int32_t a : 32;             //4-byte
    u_char f : 8;              //1-byte
    u_int32_t : 8;      //1-byte padding //make anonymous, no name
    //u_char : 0; //This is equivalent, and tells the compiler to pad to the next alignment. This might be better, as your compiler knows if 4 byte or 8 byte is better. However, you might lose the portability you are looking for.
    u_short sp : 16;             //2-byte
    u_short dp : 16;             //2-byte
    u_short w : 16;               //2-byte
    u_int32_t : 64;     //8-byte padding //again, anonymous. You might need to use a u_long64_t though.
    u_char myArray[1500]; //Cross your fingers this is aligned
    u_int32_t : 0; //Final padding. This is always implicit, but might as well write it, for the future.
}
The above method should GUARATEE, things are where they belong, as long as the "receiving" architecture is 8 bits per byte octet architecture (almost guaranteed, unless you are on an embedded platform). If it isn't you are screwed anyways.

I'm not sure if you can use this on the in_addr struct. Probably can, but I'm not sure, and I would not know their size, so I will let you work things out.

If you need more information on this, lookup "C bit field".
Another thing you want to look up, is the more general "C++ binary serialization".

That is all I can help you with, I'm a bit knowledgeable on this stuff, but not that knowledgeable.
All the sizes for data types that I listed in my posting have been verified to be correctby using sizeof() statements.