Click to See Complete Forum and Search --> : How to convert a structure to a char stream and back


miteshpandey
December 8th, 2004, 10:24 PM
I have a socket application and both the client and the server use the
following structure to store message.


struct MyMsg
{
CString m_strText;
CString m_strPlayerName;
int m_BitmapIndex;
};


The socket uses the following class to read and write.


class DataBlock {
public:
//// Public interface
// Ctors and dtor
DataBlock() :
pcData(0),
nBytes(0) { }
DataBlock(char* data, int bytes) :
pcData(data),
nBytes(bytes) { }
DataBlock(const DataBlock& db) :
pcData(db.pcData),
nBytes(db.nBytes) { }
virtual ~DataBlock() { }

// Makes a shallow copy of rhs, since this class's semantics say
// that the object's creator owns the data block, and that we just
// refer to that data.
//
// NOTE:
// This should work no differently than the default operator=,
// but we want to allow subclasses to be able to redefine it
// polymorphically.
virtual DataBlock& operator=(const DataBlock& rhs);

//// Public data
// Pointer to the data
char* pcData;

// Number of bytes that pcData points to
int nBytes;
};


My question is this:

How do I convert MyMsg to DataBlock.

In the sample program DataBlock is used like this:


// Set up output buffers and such
DataBlock DBOut(20);
ostrstream outs(DBOut.pcData, DBOut.nBytes);

// Send 10 packets on this connection
int i;
for (i = 0; i < 10; i++) {
outs.seekp(0);
outs << "Hello #" << (i + 1) << ends;
DBOut.nBytes = strlen(DBOut.pcData) + 1;
ClientConnection.Write(DBOut);

}


DataBlock is returned when the socket reads.


DataBlock *pDB = ClientConnection.Read();


Please help. How do I do the conversions.

Paul McKenzie
December 8th, 2004, 11:05 PM
You need to give us more information. Nowhere in your code do you show how MyMsg is to be used.

You posted a MyMsg structure, but then you don't do anything with it -- so we can't convert something where we have no idea how it's used.

Regards,

Paul McKenzie

Andreas Masur
December 9th, 2004, 04:04 AM
[ Moved thread ]

miteshpandey
December 9th, 2004, 03:11 PM
Sorry for the late reply.

My application is MFC based

Earlier I used CSockets but it didn't work

I had a CMsg class as below:


enum GameType {
UNDETERMINED,
MARRIAGE,
CALLBREAK,
HEARTS,
TWENTYNINE,
POPLU
};

class CMsg : public CObject
{
public:
CMsg()
{
Init();
}

void Init();
virtual void Serialize(CArchive & ar);
CCard m_Card;
CString m_PlayerName;
CString m_strText;
GameType m_GameType;
};



I serialized all the data members in the Serialize(...) function

This CMsg worked together with CArchive and CFileSocket. Since this didn't work I abandoned the thought of using CSocket.

Now I use Windows Socket wrapper classes found on the net.

Please refer to the code I posted.

I am thinking of doing something like this but it is going no where.


// Set up output buffers and such
DataBlock DBOut(20);
ostrstream outs(DBOut.pcData, DBOut.nBytes);

MyMsg Msg;

// Send 10 packets on this connection
int i;
for (i = 0; i < 10; i++) {
Msg.m_strText = "Hello";
Msg.m_strPlayerName = "Anyone";
Msg.m_BitmapIndex = i+1;
outs.seekp(0);
outs << Msg.m_strText.GetBuffer(1) << Msg.m_strPlayerName.GetBuffer(1) << Msg.m_BitmapIndex << ends;

BOut.nBytes = strlen(DBOut.pcData) + 1;
ClientConnection.Write(DBOut);
}


I have poor confidence in the above code.
The highlighted line and the GetBuffer(...) I think doesn't work.

I think this code will help to understand what I want to do.

This was the Write(...) part.

Read() fetches pointer to DataBlock type

How to convert this DataBlock pointer to the structure I presented?

Please Help. Any guidance to this will be appreciated much.

miteshpandey
December 10th, 2004, 12:11 PM
Anyone?

Please Help

Mathew Joy
December 11th, 2004, 02:09 AM
I'm not a MFC guy. But looking at the title of the thread see if the following FAQ provides any insight to your problem.

How do I transfer structure? (http://www.codeguru.com/forum/showthread.php?t=306399)

miteshpandey
December 11th, 2004, 11:36 AM
Thank you for the link. I think it will help me a lot.