Forming protocol messages with structure instances

Discussion in 'C Programming' started by pramod, Dec 16, 2003.

  1. pramod

    pramod Guest

    Two differenet applications (running on different OS, different
    compilers) are communicating thru protocol consisting of functions and
    arguments.
    Communication link can be TCP/IP, or serial port.

    What is the best way to map structure instance to protocol messages ?

    The solutions we are thinking of -
    1. declare each structure member as packed, and use memcpy for struct
    size.
    The target application should directly memcpy this area, to the
    desired structure, which is already instantiated.

    2. serialize each entry with member data type, followed by value, and
    transmit it over link.
    Value needs to be decoded using data type flag.


    1st option might not be portable.
    2nd option requires extra efforts and overhead.

    Is there any better alternative?


    regards,
    Pramod
     
    pramod, Dec 16, 2003
    #1
    1. Advertising

  2. pramod

    Artie Gold Guest

    pramod wrote:
    > Two differenet applications (running on different OS, different
    > compilers) are communicating thru protocol consisting of functions and
    > arguments.
    > Communication link can be TCP/IP, or serial port.
    >
    > What is the best way to map structure instance to protocol messages ?
    >
    > The solutions we are thinking of -
    > 1. declare each structure member as packed, and use memcpy for struct
    > size.
    > The target application should directly memcpy this area, to the
    > desired structure, which is already instantiated.


    Portability problems:
    Endian issues
    Data type size issues (possibly less of a problem in C99)

    >
    > 2. serialize each entry with member data type, followed by value, and
    > transmit it over link.
    > Value needs to be decoded using data type flag.


    Indeed.

    >
    >
    > 1st option might not be portable.


    Right.

    > 2nd option requires extra efforts and overhead.


    Also correct.
    About all you can do is come up with the lightest weight
    serialization/deserialization protocol (or use some third party software
    to help you achieve this).

    >
    > Is there any better alternative?


    Not really. ;-(
    Standard C is much more concerned with cross platform source
    compatibility than data compatibility (at least partially due to
    historical reasons).

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
     
    Artie Gold, Dec 16, 2003
    #2
    1. Advertising

  3. pramod

    Derk Gwen Guest

    (pramod) wrote:
    # Two differenet applications (running on different OS, different
    # compilers) are communicating thru protocol consisting of functions and
    # arguments.
    # Communication link can be TCP/IP, or serial port.
    #
    # What is the best way to map structure instance to protocol messages ?
    #
    # The solutions we are thinking of -
    # 1. declare each structure member as packed, and use memcpy for struct
    # size.
    # The target application should directly memcpy this area, to the
    # desired structure, which is already instantiated.

    Not at all safe with different compilers.

    # 2. serialize each entry with member data type, followed by value, and
    # transmit it over link.
    # Value needs to be decoded using data type flag.

    You might want to look into ASN-1 and its encoding rules, rather than start
    over from scratch. ASN software and libraries are available.

    # Is there any better alternative?

    If at all possible encode into an ASCII string. Much more likely to be
    portable, avoids byte order problems, eases debugging, and if you redirect
    traffic to a file, you can use off the shelf programming tools to look at it.
    The overhead of network communications will likely overwhelm encoding
    and decoding costs.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    If you plan to shoplift, let us know.
    Thanks
     
    Derk Gwen, Dec 17, 2003
    #3
  4. Derk Gwen <> wrote in message news:<>...
    >
    > # 2. serialize each entry with member data type, followed by value, and
    > # transmit it over link.
    > # Value needs to be decoded using data type flag.
    >
    > You might want to look into ASN-1 and its encoding rules, rather than start
    > over from scratch. ASN software and libraries are available.


    FYI a lot of ASN.1 compilers and libraries are listed at
    http://asn1.elibel.tm.fr/links/#tools

    O. Dubuisson
     
    Olivier Dubuisson, Dec 29, 2003
    #4
    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. Henry Rawas [MSFT]
    Replies:
    0
    Views:
    1,080
    Henry Rawas [MSFT]
    Sep 24, 2004
  2. Jimmy Retzlaff
    Replies:
    0
    Views:
    381
    Jimmy Retzlaff
    Oct 16, 2003
  3. Replies:
    3
    Views:
    290
    Fredrik Lundh
    Nov 12, 2005
  4. Replies:
    6
    Views:
    395
    Robbie Hatley
    Jul 14, 2006
  5. Aditya Kumar

    Forming HTTP request from java code

    Aditya Kumar, May 8, 2007, in forum: Java
    Replies:
    18
    Views:
    963
Loading...

Share This Page