Click here to Skip to main content
13,050,358 members (140,456 online)
Rate this:
Please Sign up or sign in to 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:
   SModelInfo *oModel;  //"SModelInfo" is a structure
   char *Buff;
   oModel = new SModelInfo;

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
Updated 26-Sep-12 22:44pm
Rate this: bad
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;

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


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));

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.
Rate this: bad
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;


It should read
char *Buff;
oModel = new SModelInfo;
Buff = new char[sizeof(SModelInfo)]; // allocate space for buffer

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2017
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