racette
April 9th, 2008, 05:04 PM
Hi gurus!
I have two software that communicate with each other through the use of sockets (winsock2) and the UDP protocol.
Software A runs on a laptop with WinXP.
Software B runs on a desktop with WinXP.
Software A sends a struct to software B.
Both software include the exact same header file containing the definition of the struct.
The struct has three members:
-an int (4 bytes)
-another int (4 bytes)
-a char array with 504 elements (thats 512 minus the size of 2 int)
for a total of 512 bytes.
The problem is:
When software B receives the buffer, it seems like the char array starts at offset 4 instead of 8. As if my 2 int were packed together in 4 bytes.
While debugging, I did the following:
In software A, right before sending the struct, i casted a pointer to the struct into a char pointer then memcpy everything in a char array containing 512 elements. That way, I was able to look at the data inside the char array and I could see that both my int were there and that they occupied 8 bytes together.
Now, when I look at the buffer received from software B, the buffer is a char array containing 512 elements, but as I stated before, both int are packed in 4 bytes.
What is the meaning of this?
Does it have anything to do with struct alignment? struct padding? internal int representation? big/little indian?
Any idea?
Thanks
racette
student in software engineering
I have two software that communicate with each other through the use of sockets (winsock2) and the UDP protocol.
Software A runs on a laptop with WinXP.
Software B runs on a desktop with WinXP.
Software A sends a struct to software B.
Both software include the exact same header file containing the definition of the struct.
The struct has three members:
-an int (4 bytes)
-another int (4 bytes)
-a char array with 504 elements (thats 512 minus the size of 2 int)
for a total of 512 bytes.
The problem is:
When software B receives the buffer, it seems like the char array starts at offset 4 instead of 8. As if my 2 int were packed together in 4 bytes.
While debugging, I did the following:
In software A, right before sending the struct, i casted a pointer to the struct into a char pointer then memcpy everything in a char array containing 512 elements. That way, I was able to look at the data inside the char array and I could see that both my int were there and that they occupied 8 bytes together.
Now, when I look at the buffer received from software B, the buffer is a char array containing 512 elements, but as I stated before, both int are packed in 4 bytes.
What is the meaning of this?
Does it have anything to do with struct alignment? struct padding? internal int representation? big/little indian?
Any idea?
Thanks
racette
student in software engineering