Sending class over Socket

  • Thread starter Thread starter MasterChief
  • Start date Start date
M

MasterChief

Hy!

I am trying to write a programm which can send "user defined" messages over
the network to a client and receive the messages.
The Message is a class I have written and I am using recv() and send() to
get and send the data.
Message class looks like:

class MsgEx

{

public:

MsgEx();

~MsgEx();



private:

string mSource;

string mDestination;

static unsigned long mMessageId;

MsgTimeEx mMsgTime;

MsgExType mMsgType;

string mData;

};



Now I am able to send char[] over my program but I can´t send my own class.

What do I have to do to send this kind of data?



Ronny
 
Hy!

I am trying to write a programm which can send "user defined" messages over
the network to a client and receive the messages.
The Message is a class I have written and I am using recv() and send() to
get and send the data.
Message class looks like:

class MsgEx

{
public:
MsgEx();
~MsgEx();
private:
string mSource;
string mDestination;
static unsigned long mMessageId;
MsgTimeEx mMsgTime;
MsgExType mMsgType;
string mData;
};

Now I am able to send char[] over my program but I can´t send my own class.
What do I have to do to send this kind of data?

Add a new function to your class that converts all the information you
need to transmit into a properly formatted char array and send that.

Also add a new constructor for your class that takes a char[] and
parses it into the separate data fields in your class.
 
Hy!

Thanks for your response. I had this idea also, but I wasn´t able to
allocate the right size for the char[].
For example
how should I return:

as char[]. This is the point I was failing.
I thought about solving it with something like serializing or streams, but I
don´t know exactly where I should start.

Doesn´t anyone have a small example. I think somebody already solved
something like that :-)

Ronny

r norman said:
Hy!

I am trying to write a programm which can send "user defined" messages
over
the network to a client and receive the messages.
The Message is a class I have written and I am using recv() and send() to
get and send the data.
Message class looks like:

class MsgEx

{
public:
MsgEx();
~MsgEx();
private:
string mSource;
string mDestination;
static unsigned long mMessageId;
MsgTimeEx mMsgTime;
MsgExType mMsgType;
string mData;
};

Now I am able to send char[] over my program but I can´t send my own
class.
What do I have to do to send this kind of data?

Add a new function to your class that converts all the information you
need to transmit into a properly formatted char array and send that.

Also add a new constructor for your class that takes a char[] and
parses it into the separate data fields in your class.
 
r norman said:
Hy!

I am trying to write a programm which can send "user defined" messages
over
the network to a client and receive the messages.
The Message is a class I have written and I am using recv() and send() to
get and send the data.
Message class looks like:

class MsgEx

{
public:
MsgEx();
~MsgEx();
private:
string mSource;
string mDestination;
static unsigned long mMessageId;
MsgTimeEx mMsgTime;
MsgExType mMsgType;
string mData;
};

Now I am able to send char[] over my program but I can´t send my own
class.
What do I have to do to send this kind of data?

Add a new function to your class that converts all the information you
need to transmit into a properly formatted char array and send that.

Also add a new constructor for your class that takes a char[] and
parses it into the separate data fields in your class.

Hy!

Thanks for your response. I had this idea also, but I wasn´t able to
allocate the right size for the char[].
For example
how should I return:

as char[]. This is the point I was failing.
I thought about solving it with something like serializing or streams, but I
don´t know exactly where I should start.

Doesn´t anyone have a small example. I think somebody already solved
something like that :-)

You are going to have to think through very carefully just what type
of character stream you need in the message to transmit all the
details that you need. You can certainly calculate the size of each
string in order to compute the size of the overall message but you
have to tell the receiving end just how to know how big the message is
going to be and, depending on the message format, just how big each
component string is.

This is a system design problem, not a coding problem. Yes, there are
already many solutions. But mine, for example, are deeply embedded in
so much other code that I can't easily strip out enough details to
show you..
 
Hy!
You are going to have to think through very carefully just what type
of character stream you need in the message to transmit all the
details that you need. You can certainly calculate the size of each
string in order to compute the size of the overall message but you
have to tell the receiving end just how to know how big the message is
going to be and, depending on the message format, just how big each
component string is.

This is a system design problem, not a coding problem. Yes, there are
already many solutions. But mine, for example, are deeply embedded in
so much other code that I can't easily strip out enough details to
show you..


But in this case I have to define some kind of protocol.
Like this:
bytes
0 32 64
string 1 string2

Isn´t there an easier way to send a class?

Still looking for a simple example for the solution :-)

Ronny
 
Back
Top