Class data to byte stream transfer mechinisim

Discussion in 'C++' started by Babbit, Sep 24, 2005.

  1. Babbit

    Babbit Guest

    I'm working with a bunch of configuration data that needs to be sent over
    from a workstation to a server. The old method was to simply memcpy
    structures into a buffer and send that across, however now I'm trying to
    use variable sized classes and coming into some difficulty in the copying
    data to a bytestream without going too crazy. So I ask, what are your
    experiences/suggestions/help with doing the following:
    (Pointing me to other documetation, or new nifty libraries is fine)


    #include <vector>
    #include <memory>

    class A {
    public:
    int aa;
    int bb;
    };

    class B {
    public:
    std::vector<A*> aList;
    void insert(A *element){
    aList.push_back(element);
    }
    };

    char Lan_Data[1000];

    void workstation(){
    A a1,a2;
    B b;

    a1.aa=10;a1.bb=20;
    a2.aa=30;a2.bb=40;

    b.insert(&a1);
    b.insert(&a2);

    //Now, copy data of 'b' (no matter how many elements)
    //into a char buffer (Lan_Data) to send to a socket

    // TBD:????
    }

    void server(){
    B b;
    //Take data from Lan_Data and load up the 'b' element.

    // TBD:????
    }

    int main(){
    memset(Lan_Data,0,sizeof(Lan_Data));
    workstation();
    server();
    return 0;
    }
    Babbit, Sep 24, 2005
    #1
    1. Advertising

  2. Babbit

    Guest

    You should not use A's pointer in aList. try to define vector<A>
    aList which will contain the whole object instead of obejct's pointer.
    you also need to define a copy
    construct function if object includes members of pointers
    , Sep 24, 2005
    #2
    1. Advertising

  3. Babbit wrote:
    > I'm working with a bunch of configuration data that needs to be sent over
    > from a workstation to a server. The old method was to simply memcpy
    > structures into a buffer and send that across, however now I'm trying to
    > use variable sized classes and coming into some difficulty in the copying
    > data to a bytestream without going too crazy.


    this may be of interest
    http://www.parashift.com/c -faq-lite/serialization.html


    --
    Nick Keighley
    Nick Keighley, Sep 24, 2005
    #3
  4. Babbit

    Greg Guest

    Babbit wrote:
    > I'm working with a bunch of configuration data that needs to be sent over
    > from a workstation to a server. The old method was to simply memcpy
    > structures into a buffer and send that across, however now I'm trying to
    > use variable sized classes and coming into some difficulty in the copying
    > data to a bytestream without going too crazy. So I ask, what are your
    > experiences/suggestions/help with doing the following:
    > (Pointing me to other documetation, or new nifty libraries is fine)
    >
    >
    > #include <vector>
    > #include <memory>
    >
    > class A {
    > public:
    > int aa;
    > int bb;
    > };
    >
    > class B {
    > public:
    > std::vector<A*> aList;
    > void insert(A *element){
    > aList.push_back(element);
    > }
    > };
    >
    > char Lan_Data[1000];
    >
    > void workstation(){
    > A a1,a2;
    > B b;
    >
    > a1.aa=10;a1.bb=20;
    > a2.aa=30;a2.bb=40;
    >
    > b.insert(&a1);
    > b.insert(&a2);
    >
    > //Now, copy data of 'b' (no matter how many elements)
    > //into a char buffer (Lan_Data) to send to a socket
    >
    > // TBD:????
    > }
    >
    > void server(){
    > B b;
    > //Take data from Lan_Data and load up the 'b' element.
    >
    > // TBD:????
    > }
    >
    > int main(){
    > memset(Lan_Data,0,sizeof(Lan_Data));
    > workstation();
    > server();
    > return 0;
    > }


    You could encode the structures using using BER (Binary Encoding Rules)
    (see ASN.1). There are numerous implementations available.

    You could also encode the structure as XML, send it over, and then
    rebuild it on the other side by parsing the XML. Actually this
    suggestion just repackages the first one. In both cases the receiver
    would be able to recreate the original data structures, without knowing
    their structure beforehand.

    Greg
    Greg, Sep 25, 2005
    #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. Jean-Daniel Gamache
    Replies:
    0
    Views:
    411
    Jean-Daniel Gamache
    Jul 14, 2004
  2. Replies:
    3
    Views:
    371
    Richard Maher
    Dec 8, 2006
  3. Deep
    Replies:
    6
    Views:
    472
    Nick Keighley
    Feb 28, 2007
  4. dolphin
    Replies:
    6
    Views:
    544
    Thomas Fritsch
    Mar 18, 2007
  5. Roedy Green

    byte stream vs char stream buffer

    Roedy Green, May 7, 2014, in forum: Java
    Replies:
    20
    Views:
    162
    Silvio
    May 18, 2014
Loading...

Share This Page