Question on using different types within a struct.

D

Durango

I am looking to use structs in my program so that I can pass them as
"packets" over a network socket created within the program.

I am particularly interested in the following scenario:

typedef struct A
{
int a;
int b;
int c;
}pkt_A;

typedef struct B
{
int def;
char msg[32];
}pkt_B;

typedef struct C
{
double d;
char msg1[32];
char msg2[32];
}pkt_C;

typedef struct D
{
int code;
void *body;
}pkt_D;


Now I want struct D to act as the container for the other structs that
are created. I am wondering if this would work or if there is a better
way to do this? I am not sure if I am on the right track.

Thanks for any advice.
 
A

Anders Wegge Keller

Durango said:
Now I want struct D to act as the container for the other structs
that are created. I am wondering if this would work or if there is
a better way to do this? I am not sure if I am on the right track.

typedef struct D {
int code;
union {
pkt_A a;
pkt_B b;
pkt_C c;
}
}

This will work as long as code identifies which of the three data
structures are used.
 
J

Jens Thoms Toerring

Durango said:
I am looking to use structs in my program so that I can pass them as
"packets" over a network socket created within the program.

First of all sending structures over the net is typically
not a good idea because a structure created on machine A,
using the exact same code, can look rather different from
one created on machine B. Even somethig simple as an int
can have different sizes on different machines, the binary
representation of the values may be different (i.e. you
may have different endian-ness on the machines). Then
the compiler may insert different amounts of "padding"
between the members of the structure - even if the archi-
tectures of machine A and B are identical, i.e. use the
same sizes and endian-ness for all types you use the
same structure created by different compiler or even
different versions of the same compiler can be different.

So sending structures from one machine to another (or
just storing data by writing a structure directly to
a file) is usually a rather bad idea (I tried that my-
self once before I understood the implictions and got
bitten by it badly;-)

The simplest way to transmit (or store) values is
by converting them to ASCII strings. Simple, if the
data are already strings, also very simple for inte-
ger types, a bit more tricky for floating point
values since you have to consider which precision
you need.
I am particularly interested in the following scenario:
typedef struct A
{
int a;
int b;
int c;
}pkt_A;
typedef struct B
{
int def;
char msg[32];
}pkt_B;
typedef struct C
{
double d;
char msg1[32];
char msg2[32];
}pkt_C;
typedef struct D
{
int code;
void *body;
}pkt_D;

This looks completely wrong - a pointer on the machine you
send this structure from has no meaning on the machine you
receive it on, so it can't be used for anything.
Now I want struct D to act as the container for the other structs that
are created. I am wondering if this would work or if there is a better
way to do this? I am not sure if I am on the right track.

What do you mean by "as the container"? You have four struc-
tures of different sizes, how could be one of them with pro-
bably one of the smallest sizes be used as a "container" for
the others?

Did you intent to set the 'code' member of 'pkt_D' to some-
thing indicate the type of one of the other three and make
the 'body' member point to it and then send just this over
the network? If that's the case that won't work at all.
When the other machine gets the structure of type 'pkt_D'
(even when assuming that both machines are identical and
you used the same compiler and version on both of them
to compile the program) having a pointer on B that is
the address of something in the memory of machine A is
rather useless since the program running on B can't access
that (even if these would be two programs running on the
same machine it wouldn't work since typically, i.e. on
machines with a virtual memory system, a program can't
access the memory of another).

Regards, Jens
 
D

Durango

Regards, Jens

Thank you for your input Jens, it makes sense now after I read your post.
After some more research I will look into using serialization method to
accomplish this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top