I have a problem related to the transfer of data over a network via DirectPlay (Client-Server system). Basically, the computer receives data packets through a connection that vary in size (TCP). The data in a packet is represented using a PBYTE. However, while they may contain different amounts of data, all packets supposedly share a common attribute – the first piece of data in the buffer is a DWORD that defines the ‘type’ of packet. The idea is that I extract the DWORD from the data buffer and then read the remaining data depending on what the DWORD is.
Put simply, I have the data buffer (PBYE) stored as pMsg->pReceiveData, and I need to take the first piece of data the size of a DWORD and store it in DWORD Id, and then take the rest of the data and store it in PBYE Data;
It is a simple task extracting the DWORD from the buffer (C++):
DWORD Id;
PBYE Data;
memcpy( &Id, pMsg->pReceiveData, sizeof(DWORD) );
How can I go about taking the remaining data and inserting it into Data? I tried the following with no success:
#pragma pack(push, 1) // changes byte alignment to 1
struct TempMessage
{
DWORD Id;
PBYTE Data;
};
#pragma pack(pop) // changes byte alignment to 1
TempMessage * temp = (TempMessage*) pMsg->pReceiveData;
//The theory was that by doing this the first piece of data the size of DWORD ends up in
//temp->Id and the rest of it ends up in temp->Data. Predictably this did not work.
//I don’t think it is entirely relevant but here is how I was attempting to send messages:
WCHAR wszData[256];
//strcpy wszData to something…
TempMessage temp;
temp.Id = 10;
temp.Data = (BYTE*) wszData;
DPN_BUFFER_DESC dpnBuffer;
dpnBuffer.pBufferData = (BYTE*) &temp
dpnBuffer.dwBufferSize = sizeof(char)*256 + sizeof(DWORD);
//Send dpnBuffer;
Any ideas? This is not exactly a networking specific problem, so if no solutions surface here can help me I shall ask in the General Programming (after this thread has disappeared from the first page)…
Thanks for any help.
Jackson Allan