dynamic array through sockets(network)

Discussion in 'C Programming' started by Gurur, Sep 13, 2007.

  1. Gurur

    Gurur Guest

    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
    Gurur, Sep 13, 2007
    #1
    1. Advertising

  2. Gurur

    Chris Dollin Guest

    Gurur wrote:

    > 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.

    --
    If Any Hedgehog
    "A facility for quotation covers the absence of original thought." /Gaudy Night/
    Chris Dollin, Sep 13, 2007
    #2
    1. Advertising

  3. Gurur

    Tor Rustad Guest

    Gurur wrote:
    > 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...

    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, Sep 14, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Jaquet

    [Network] Sockets and Instant Messaging

    Alexandre Jaquet, Oct 21, 2003, in forum: Java
    Replies:
    3
    Views:
    379
    Thomas G. Marshall
    Oct 21, 2003
  2. Replies:
    5
    Views:
    406
    Ghost
    May 11, 2005
  3. Chandra
    Replies:
    4
    Views:
    463
    James Kanze
    Sep 14, 2007
  4. Tompyna
    Replies:
    2
    Views:
    257
    Ben Morrow
    Feb 17, 2004
  5. Aaron
    Replies:
    2
    Views:
    520
    dhtml
    Apr 10, 2011
Loading...

Share This Page