Class data to byte stream transfer mechinisim

B

Babbit

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;
}
 
U

up3000

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
 
N

Nick Keighley

Babbit said:
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
 
G

Greg

Babbit said:
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
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top