473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

structure byte alighment and sockets



Hello,
Consider this:

struct MBD {
double x;
double y;
int i;
}

Now, on my current machine, sizeof(struct MBD) = 24.

I assume this is due to byte alignment on 8 byte boundaries and the
shift of the int i to be 8 bytes, not four.

So... consider the case of socket data transmission, wherein, a client
transmits a struct MBD and it is read by a server.

It would go like this:
read(soc, (char*) address, sizeof(struct MBD));
write(soc, (char*) address, sizeof(struct MBD));

Now, IF the server and client run on the same archiecture, there
should be no problem.

But what if they run on different machines that align bytes differently?

There is now a problem.

One fix would be to find a way to supress byte alignment using a
compiler option
flag (if one exists... does one?)
Another would be to transmit the fields singly.
But these both appear to be kludge workarounds.

Am I thinking about this incorrectly?
Is there a more fundamental or comp.theoretical issue I am overlooking
with regard to the sizeof operator, or structures, or read/write?

thanks

Nov 14 '05 #1
5 1444
joe
Richard Harris <rh*****@kahuna.sdsu.edu> writes:
Consider this:

struct MBD {
double x;
double y;
int i;
}

Now, on my current machine, sizeof(struct MBD) = 24.

I assume this is due to byte alignment on 8 byte boundaries and the
shift of the int i to be 8 bytes, not four.

So... consider the case of socket data transmission, wherein, a client
transmits a struct MBD and it is read by a server.

It would go like this:
read(soc, (char*) address, sizeof(struct MBD));
write(soc, (char*) address, sizeof(struct MBD));

Now, IF the server and client run on the same archiecture, there
should be no problem.
That's not really true. Even on the same architecture the programs may
have different ideas about the struct size, padding, etc if different
compilers, complier flags, pragmas, etc were used to build them.
Am I thinking about this incorrectly?
Is there a more fundamental or comp.theoretical issue I am overlooking
with regard to the sizeof operator, or structures, or read/write?


One common recommendation in this regard is to use xdr, or some other
standard marshalling/unmarshalling technique. Another is to simply
convert everything to ASCII for the transmission.

Joe
--
We can't all be heroes because someone has to sit on the curb and
clap as they go by.
- Will Rogers
Nov 14 '05 #2
In article <41**************@kahuna.sdsu.edu>,
Richard Harris <rh*****@kahuna.sdsu.edu> wrote:
Hello,
Consider this:

struct MBD {
double x;
double y;
int i;
}

Now, on my current machine, sizeof(struct MBD) = 24.

I assume this is due to byte alignment on 8 byte boundaries and the
shift of the int i to be 8 bytes, not four.

So... consider the case of socket data transmission, wherein, a client
transmits a struct MBD and it is read by a server.

It would go like this:
read(soc, (char*) address, sizeof(struct MBD));
write(soc, (char*) address, sizeof(struct MBD));

Now, IF the server and client run on the same archiecture, there
should be no problem.

But what if they run on different machines that align bytes differently?
Even worse, what if they have completely different ways of representing
floating point numbers.

There is now a problem.

One fix would be to find a way to supress byte alignment using a
compiler option
flag (if one exists... does one?)
Another would be to transmit the fields singly.
But these both appear to be kludge workarounds.

Am I thinking about this incorrectly?
Is there a more fundamental or comp.theoretical issue I am overlooking
with regard to the sizeof operator, or structures, or read/write?


You should generally not depend on being able to transmit binary data
directly between different computers. You need to make use of a
standard encoding; this is the job of the Presentation Layer of the OSI
model. There are some popular libraries that handle this: XDR (which is
used by Sun RPC) and ASN.1/BER are the most well known. You could also
just convert them to text and then parse them using sscanf() after
receiving them.

--
Barry Margolin, ba****@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Nov 14 '05 #3
Richard Harris wrote:

Hello,
Consider this:

struct MBD {
double x;
double y;
int i;
}

Now, on my current machine, sizeof(struct MBD) = 24.

I assume this is due to byte alignment on 8 byte boundaries and the
shift of the int i to be 8 bytes, not four.

So... consider the case of socket data transmission, wherein, a client
transmits a struct MBD and it is read by a server.

It would go like this:
read(soc, (char*) address, sizeof(struct MBD));
write(soc, (char*) address, sizeof(struct MBD));

Now, IF the server and client run on the same archiecture, there
should be no problem.

But what if they run on different machines that align bytes differently?

There is now a problem.

One fix would be to find a way to supress byte alignment using a
compiler option
flag (if one exists... does one?)
Another would be to transmit the fields singly.
But these both appear to be kludge workarounds.

Am I thinking about this incorrectly?
Is there a more fundamental or comp.theoretical issue I am overlooking
with regard to the sizeof operator, or structures, or read/write?

thanks


My advice is not to use structures directly.
Copy the data into a buffer, in the format that you want,
then transmit it. Read the data into a buffer, extract
the data into a structure.

Here are some of the problems with using raw data structures:
1. Padding -- some compilers put it in there.
2. Floating point sizes.
3. Endianness.

My experiece is that structures are handy for lumping data
within a program. One should convert data to/from that
structure from/to the real world or outside the program's
environment.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4
In article <m3************@invalid.address>, jo*@invalid.address wrote:
That's not really true. Even on the same architecture the programs may
have different ideas about the struct size, padding, etc if different
compilers, complier flags, pragmas, etc were used to build them.


That's not really a problem in practice. If it were, you would have
problems calling kernel and library routines if they were compiled with
different compilers or options. So in the real world, structure layout
is part of the ABI of the system, and all compilers for a system have to
conform (there could be options or pragmas to override this, but the
kernel and general purpose libraries obviously shouldn't use them).

--
Barry Margolin, ba****@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Nov 14 '05 #5
joe
Barry Margolin <ba****@alum.mit.edu> writes:
In article <m3************@invalid.address>, jo*@invalid.address wrote:
That's not really true. Even on the same architecture the programs
may have different ideas about the struct size, padding, etc if
different compilers, complier flags, pragmas, etc were used to
build them.


That's not really a problem in practice. If it were, you would have
problems calling kernel and library routines if they were compiled
with different compilers or options. So in the real world,
structure layout is part of the ABI of the system, and all compilers
for a system have to conform (there could be options or pragmas to
override this, but the kernel and general purpose libraries
obviously shouldn't use them).


I thought he was talking about sending his own structs through the
net. In that case the compiler options would have an effect. For
example:

$ cat test.c
#include <stdio.h>

struct X
{
char c;
int i;
};

int main ()
{
struct X x;
printf("sizeof x = %d\n", sizeof x);
}

$ gcc test.c
$ a.out
sizeof x = 8

$ gcc -fpack-struct test.c
$ a.out
sizeof x = 5

Joe
--
We can't all be heroes because someone has to sit on the curb and
clap as they go by.
- Will Rogers
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit...
0
by: Jack | last post by:
I am trying to develop a simple telnet client in MS.NET I have a small wrapper around the System.Net.Sockets.Socket class which takes care of sending and receiving data and handing it off to a...
1
by: user | last post by:
Hello I have structure: public struct buf { public int type; public int len; public byte data; }; and in my program: String d="something";
3
by: Raj | last post by:
I want to pass a C structure from a windows server to the C# client using the Sockets. Will there be requirements to cast the data types in the C# client?
4
by: TomHL | last post by:
Hello, I want to send an additional info with byte array wiith sockets as "one packet". I know how to send the byte array by himself, but how can I send the additional info with it at the same...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.