Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i craete one structue i added the structur into the vector..i want to pass the vector by using socket.i want to serilaize the vector and then pass to the socket..But i don't know how to do..Please tell How to do that..
Posted

1 solution

Assumed the structure SDATA has no pointer members :) :

BYTE* MakeMemoryDump(vector<sdata>* pVector)
{
  BYTE* pData(NULL);

  if (pVector) {
    int iCount(pVectot->GetCount());
    if (iCount) {
      pData = new BYTE(iCount * sizeof(SDATA) + sizeof(DWORD));
      DWORD* pdwCount = (DWORD*) pData;
      *pdwCount = (DWORD) iCount;
      SDATA* psData = (SDATA*) (pData + sizeof(DWORD));
      for (int i = 0; i < iCount; i++) {
        memcpy_s(psData[i], sizeof(SDATA), &pVector->GetAt(i), sizeof(SDATA));
      }
    }
  }

  return pData;
}


...and :) :

void ReadMemoryDump(BYTE* pData, vector<sdata>* pToVector)
{
  if (pData && pToVector) {
    pToVector->Empty();
    DWORD* pdwCount = (DWORD*) pData;
    SDATA* psData = (SDATA*) (pData + sizeof(DWORD));
    for (DWORD dw = 0; dw < *pdwCount; dw++) {
      pToVector->Add(*psData[i]);
    }
  }
}</sdata>
 
Share this answer
 
v3
Comments
Dhanasundhari 28-Jul-11 3:06am    
How to de-serialize that sir. in my client side
Eugen Podsypalnikov 28-Jul-11 3:17am    
Please see above :)
Dhanasundhari 28-Jul-11 3:31am    
ok.. Thanks sir..
Dhanasundhari 28-Jul-11 3:36am    
#include "stdafx.h"
#include <memory.h>
#include <windows.h>
#include <winsock2.h>
#include <iostream.h>
#include <lm.h>
#include <vector>
#include <string>
#include <memory.h>
using namespace std;
struct ClientInformation
{
string sCliName;
int iPlatId;
int iMajor;
int iType;
int iMinor;
};

class cClient
{
private:
DWORD dwLevel;
DWORD dwPrefMaxLen;
LPSERVER_INFO_101 pBuf;
DWORD dwEntriesRead;
DWORD dwTotalEntries;
DWORD dwServerType;
LPTSTR pszDomainName;
NET_API_STATUS nStatus;
WSADATA wsaData;
DWORD dwResumeHandle;
char cszBuf[200];
SOCKET ClientSocket;
int iTemp;
string str;
public:
cClient()
{
dwLevel = 101;
dwPrefMaxLen = MAX_PREFERRED_LENGTH;
pBuf = NULL;
dwEntriesRead = 0;
dwTotalEntries = 0;
dwServerType = SV_TYPE_SERVER;
pszDomainName = NULL;
dwResumeHandle = 0;

}

void FindDomainSystem()
{
ClientInformation *StructObj;
vector <clientinformation> *myvec;
WSAStartup(MAKEWORD(2,2), &wsaData);
nStatus = NetServerEnum(NULL,dwLevel,(LPBYTE *) & pBuf,dwPrefMaxLen,
&dwEntriesRead,&dwTotalEntries,dwServerType,
NULL, &dwResumeHandle);
myvec = new vector <clientinformation>();

if(nStatus == NERR_Success)
{
cout<<"The NetServerEnumeration sucessfuly "<<"\n";
cout<<"The total entires is "<<dwEntriesRead<<"\n\n";
for(int i=1; i<= dwEntriesRead;i++)
{

memset(cszBuf,0,sizeof(cszBuf));

sprintf( cszBuf, "%S", pBuf->sv101_name );
cout<<"\nThe server name "<<cszBuf<<"\n";
StructObj->sCliName.erase();
StructObj->sCliName.append(cszBuf);
cout<<"The Platform id of the system is "<<pBuf->sv101_platform_id<<"\n";
StructObj->iPlatId = pBuf->sv101_platform_id;
cout<<"The type of system running is "<<pBuf->sv101_type<<"\n";
StructObj->iType = pBuf->sv101_type;
cout<<"The major version of the system is "<<pBuf->sv101_version_major<<"\n";
StructObj->iMajor = pBuf->sv101_version_major;
cout<<"The minor version of the system is "<<pBuf->sv101_version_minor<<"\n";
StructObj->iMinor = pBuf->sv101_version_minor;
send(ClientSocket,(char*)&StructObj,sizeof(StructObj),0);
myvec->push_back(*StructObj);
pBuf++;
}
}
else
{
cout<<"\nThe Net Enumeration producing Error\n";
}
cout<<"\n\n\n\nThe vector data\n";
display(myvec);

}
void display(vector <clientinformation> *my)
{
BYTE *pData = NULL;
if(my)
{
int i = my->size();
if(i)
{
pData = new BYTE(i*sizeof(ClientInformation));
ClientInformation *psData = (ClientInformation*)pData;
for(int i1=0;i1<i;i++)
{
="" memcpy_s(psdata[i1],sizeof(clientinformation),&my-="">at(i1),sizeof(ClientInformation)); //prodcing error in that place.. what i want to do now...
}
}

//int icount = my
}

}


};
int main(int argc, char* argv[])
{

cClient o;
o.FindDomainSystem();
return 0;
}

thats is the code i build sir.. but it producing error sir..
what i want to do now..
Eugen Podsypalnikov 28-Jul-11 7:21am    
0. Probably you should increment i1 (i1++) instead i (i++)
1. See also at my modified first function: the count will be "serialized" as well
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900