Class data to byte stream transfer mechinisim

  • Follow


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;
}
0
Reply me7582 (375) 9/24/2005 2:39:22 AM

   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

0
Reply up3000 (4) 9/24/2005 3:40:06 AM


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

0
Reply nick_keighley_nospam (4574) 9/24/2005 11:55:43 AM

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

0
Reply greghe (676) 9/25/2005 10:34:03 AM

3 Replies
31 Views

(page loaded in 0.097 seconds)

Similiar Articles:













7/6/2012 11:27:47 PM


Reply: