473,725 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ 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.theoretica l issue I am overlooking
with regard to the sizeof operator, or structures, or read/write?

thanks

Nov 14 '05 #1
5 1464
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.theoretica l 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.theoretica l 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.theoretica l 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.l earn.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.addres s>, jo*@invalid.add ress 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.mi t.edu> writes:
In article <m3************ @invalid.addres s>, jo*@invalid.add ress 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
4347
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 this sound (file) on a Windows socket (not using FTP but TCP), can I read the file into a byte array? Because if it is possible then I can send it on the socket but the problems I foresee are: How will I repack it into the file. Will just reading...
0
2079
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 protocol handler. What I am finding is that if I set my receive buffer size to 1 byte this code will 'lose' characters, yet when I set the buffer to more than 1 byte all works fine.
1
1607
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
4181
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
1712
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 time (as one packet)? thx for yout answers.
9
12687
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
4993
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 get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
5
3792
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
10
6373
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 and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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 we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.