Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,
Right now i am developing socket program using TCP/IP. Now problem is, I want to transmit structure data from server and in client, i want to receive same data using same structure.

Server Side:
Step1: Using CFile object "pCustInfoFile" struct obj "oModeld passed to read daa from file.
Step2: Now i Assign this struct obj "oModel" into char *Buff.
Step3: using strncpy function, copied data from oMdel in to Buff.
Step4: Using Socket obj "pSocket", the data sent to client side.

Server side code for your reference:
C++
if(pCustInfoFile->Open(FName,CFile::modeRead))
{
   SModelInfo *oModel;  //"SModelInfo" is a structure
   char *Buff;
   oModel = new SModelInfo;
   pCustInfoFile->Read(oModel,sizeof(SModelInfo));
   
   strncpy(Buff,(char*)&oModel,sizeof(SModelInfo));
   pSocket->Send(Buff,sizeof(Buff));

}

Client Side:

Step1: Using char buff with same size and data is receives in client side.
Step2: oModel = (SModelInfo*)buff;
Step3: If assigning oModel->Name to sName (sName = oModel->Name)variable then the value(Name) is not getting
C++
int br=Receive(buff,sizeof(SModelInfo));
SModelInfo *oModel;
oModel = (SModelInfo*)buff;
CString sName;
sName = oModel->Name;

Help me to fix this problem.

Thanks and Regards
S.Shanmuga Raja
Posted
Updated 26-Sep-12 22:44pm
v2

In the server side, you declared the Buff pointer but, didn't point it to a specific memory. Try to allocate memory for Buff. Something like:

C++
char *Buff = new char[sizeof(SModelInfo)];


And, why do you use strncpy with the pointer to the pointer to SModelInfo (you declared oModel as a pointer...). Try to declare oModel in the stack instead. Something like:

C++
SModelInfo oModel;
pCustInfoFile->Read(&oModel,sizeof(SModelInfo));


Another issue: Try to change the Send function to use the struct size. Something like:

C++
pSocket->Send(Buff,sizeof(SModelInfo));

 
Share this answer
 
v3
Comments
shanmugarajaa 27-Sep-12 6:32am    
Now i am getting problem in this block of code

char *buff;
SCustInfo *pCustInfo;
CString sReceiveStr;
int br=Receive(buff,sizeof(SModelInfo));
buff[br]='\0';


SModelInfo *oModel1;
oModel1 = new SModelInfo;
oModel1 = (SModelInfo*)buff;

Main problem is, i need to append one by one data which is retrieve from file and finally sent to client and same way data to be received. Plz can you give sample code or suggestion.

Thanks and Regards,
S.Shanmuga Raja
Shmuel Zang 27-Sep-12 7:16am    
I'm not familiar with your data-structures and your business-logic. But I don't see where the memory of the buff pointer is allocated (again...). Try to declare oModel1 on the stack and, point buff to it. Something like: SModelInfo oModel1; char *buff = (char*)&oModel1;. And you don't need the buff[br]='\0'; line.
Richard MacCutchan 27-Sep-12 9:12am    
You seem to have a fundamental misunderstanding about pointers and memory allocation, take a look here for some useful information.
Shmuel Zang 27-Sep-12 9:32am    
Can you explain why do you think like that? What from the above, you consider as a fundamental misunderstanding?
Richard MacCutchan 27-Sep-12 9:54am    
Sorry, my comments were directed at the OP and I have added my suggestions below. I mis-posted them in your solution, rather than the original question.
In the following code you are trying to copy from the structure oModel to Buff, but that pointer has not been initialised so you will corrupt some memory or cause a memory access error.
C++
char *Buff;
oModel = new SModelInfo;
pCustInfoFile->Read(oModel,sizeof(SModelInfo));

strncpy(Buff,(char*)&oModel,sizeof(SModelInfo));

It should read
C++
char *Buff;
oModel = new SModelInfo;
pCustInfoFile->Read(oModel,sizeof(SModelInfo));
Buff = new char[sizeof(SModelInfo)]; // allocate space for buffer
strncpy(Buff,(char*)oModel,sizeof(SModelInfo));


In the following code you create a pointer, initialise it with new and then immediately overwrite it with a different pointer thus losing the previously allocated block.
C++
SModelInfo *oModel1;
oModel1 = new SModelInfo;
oModel1 = (SModelInfo*)buff;

This should be
C++
SModelInfo *oModel1 = (SModelInfo*)buff; // just point at the existing buffer.
 
Share this answer
 
v6
Comments
Shmuel Zang 27-Sep-12 10:17am    
Why do you use &oModel in your strncpy calls? It's a pointer to the pointer to the SModelInfo structure. In that way you copy the address of the structure instead of its content.
Richard MacCutchan 27-Sep-12 10:29am    
I just copied the OP's code and did not check it closely enough. Just another example of what I said earlier. Corrected.

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