dynamic array through sockets(network)

G

Gurur

Hi all,

I have a doubt.
If I have 2 structures and one is parent of other , ie the child
structure is present in the parent one . And if the child structure is
declared as dynamic array in the parent , will it be possible to pass
the parent structure thru network using sockets onto other application
running on different system provided the API is known to both the
sender and the receiver.

For Ex:

struct Child
{
a: Int;
b: float;
}


struct parent
{
c : Int;
d : array of Child;(dynamic array)
}

If I make it static array, I will be able to pass it, but wat if it is
dynamic.

Thanks
 
C

Chris Dollin

Gurur said:
If I have 2 structures and one is parent of other , ie the child
structure is present in the parent one . And if the child structure is
declared as dynamic array in the parent ,

C has no "dynamic arrays" as such; it has pointers and `malloc` instead.
will it be possible to pass the parent structure thru network using
sockets onto other application running on different system provided
the API is known to both the sender and the receiver.

Of course. This being C, you just have to do "all" the work yourself.
struct Child
{
a: Int;
b: float;
}

You're in the right newsgroup, yes? C? That's not C syntax.
struct parent
{
c : Int;
d : array of Child;(dynamic array)
}

If I make it static array, I will be able to pass it, but wat if it is
dynamic.

If you're writing the code, it will do whatever you want.

If someone else writes the send-it-over-a-network code, it will do
whatever /they/ want, for which read their documentation / test cases.
 
T

Tor Rustad

Gurur said:
Hi all,

I have a doubt.
If I have 2 structures and one is parent of other , ie the child
structure is present in the parent one . And if the child structure is
declared as dynamic array in the parent , will it be possible to pass
the parent structure thru network using sockets onto other application
running on different system provided the API is known to both the
sender and the receiver.

Passing raw structures over a network between different systems, is a
rather bad idea.

For starters. The C structure layout in memory, is very much compiler
dependent. Just using a different C compiler option, might affect how
struct members are aligned.


Different type of encoding can be used (T=tag, L=length, V=value), for
fixed number of tags and fixed length:

V
V
....
V

an example here could be the IPv4 header:

0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

but notice the first 4 bits ('Version'), this is really a tag, so for
IPv6 header we can use a rather different header:

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| Traffic Class | Flow Label |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload Length | Next Header | Hop Limit |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ Source Address +
| |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ Destination Address +
| |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Hence, IP headers are an example of TV encoding!

For fixed number of tags and variable length:

LV
LV
LV
....
LV


Notice the "Total Length" or "Payload Length" field in the IP headers,
hence the IP payload is an example of LV encoding.

For variable number of tags and variable length, we could use:

TLV
TLV
....
TLV


Another option here, is to use start and end-tags, see e.g. XML and
EDIFACT. Likewise, an IP packet over Fibre Channel:

+-----+-----------+-----------+--------//-------+-----+-----+
| | | Data Field | | |
| SOF | FC Header |<--------------------------->| CRC | EOF |
| | | Optional | Frame | | |
| | | Header(s) | Payload | | |
+-----+-----------+-----------+--------//-------+-----+-----+

here Start-Of-Frame and End-Of-Frame, can be viewed as start and end
tags. One problem with end-tags, is that you can't have a payload
containing an end-tag, so when the length is unknown, such data need to
be escaped, just like when printing '%':

printf("%%");

If I make it static array, I will be able to pass it, but wat if it is
dynamic.

When using e.g. TLV, XML or EDIFACT encoding, dynamic number of data
elements can be handled at receiver. "Nobody" is sending raw C struct's
over the wire...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top