Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/MFC
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:
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
  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 26-Sep-12 22:41pm
Edited 26-Sep-12 22:44pm
JF201553.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:

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:

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

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

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

  Permalink  
v3
Comments
shanmugarajaa at 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 at 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 at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
   char *Buff;
   oModel = new SModelInfo;
   pCustInfoFile->Read(oModel,sizeof(SModelInfo));
   
   strncpy(Buff,(char*)&oModel,sizeof(SModelInfo));
It should read
   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.
SModelInfo *oModel1;
oModel1 = new SModelInfo;
oModel1 = (SModelInfo*)buff;
This should be
SModelInfo *oModel1 = (SModelInfo*)buff; // just point at the existing buffer.
  Permalink  
v6
Comments
Shmuel Zang at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 369
1 _Amy 230
2 OriginalGriff 220
3 Peter Leow 205
4 Andreas Gieriet 180
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,412
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100