I have a struct that looks like this
struct message_header
{
unsigned long msg_num : 32; //0-3 message id
unsigned long msg_len : 32; //4-7 message length
unsigned long hardware_version : 16; //8-9 hardware version
unsigned long sender_location : 32 //10-13 location
unsigned long message; //14 ... messages
};
message_header * msg_ptr;
After receiving the character array (recvbuf
) from recvfrom
function, i will do
reinterpret_cast
,
msg_ptr = reinterpret_cast<message_header*>(recvbuf)
However, after my simulator (from linux, VM) sends the data, and my receiver (the struct is in the receiver) (on windows), the output data does not tally.
Assuming the data send from the simulator is:
msg num : 1010
msg len : 20
hardware version: 1
location: 25
messages: //rest of the bytes
Packets from the wireshark shows:
00 00 03 F2
00 00 00 14
00 01 00 00
00 19 00 00
...
The output print is:
msg num : 1010
msg len : 20
hardware version: 1
location: 1638400
After much debugging, I noticed that during the cast, after hardware version, the 00 00
after the 01
was either discarded, or skipped, i am not sure which as I can't find a way to determine it, and the next 4 bytes of 00 19 00 00
was cast into my sender_location
.
All the message types and length, from the sender is build base on the sender's design specification, and the hardware version
on the sender is an unsigned short
.
I have read up on the following question and answers
Fields in a struct skipping bytes
And i have tried but to no avail, please advise.