public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* sending structure containing a pointer to another structure over TCP socket
@ 2006-07-23  7:52 Abid Ghufran
  2006-07-23 17:56 ` John (Eljay) Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Abid Ghufran @ 2006-07-23  7:52 UTC (permalink / raw)
  To: gcc-help

I am working on an application over fedora using c language and
sockets (TCP). I have to send a structure which contains a pointer to
another strcuture. Now what happens is that when the primary structure
(containing the pointer) gets sent, instead of the secondary structure
(pointed to) being sent, only its pointer get sent. This is quite
normal and obvious as the original strucutre, contains the pointer as
a data member, and does not contain as a member the structure being
pointed to.

I am using send and receive for this purpose. How can i manage the
send(ing) and receive(ing) of the primary and the secondary structure?

[The primary structure contains the control information and the
secondary structure contains the data information, and they together
form sort of a header and payload].

Thank you,

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: sending structure containing a pointer to another structure over TCP socket
  2006-07-23  7:52 sending structure containing a pointer to another structure over TCP socket Abid Ghufran
@ 2006-07-23 17:56 ` John (Eljay) Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John (Eljay) Love-Jensen @ 2006-07-23 17:56 UTC (permalink / raw)
  To: Abid Ghufran, gcc-help

Hi Abid,
 
Let's suppose you had this:
 
struct Second;
 
struct First
{
  short value;
  char* s;
  Second* second;
};
 
struct Second
{
  int a;
  char b;
  long c;
};
 
And let's assume that on your platform, char is 1 byte, short is 2 byte, int is 4 byte, and long is 8 byte.  And let's assumet that pointers much be 4-byte aligned, and int must be 4-byte aligned, and long must be 8-byte aligned.
 
If you want to send First over the wire, and you want to include Second, what you should do is write a send routine, and a receive routine.  I presume you use the hton and ntoh inside the low level data types Send and Recieve routines, which you'd have written.
 
void Send(int fd, First const& first)
{
  SendInt16(fd, first.value);
  SendCharArray(fd, size, first.s);
  Send(fd, *(first.second));
};
 
void Send(int fd, Second const& second)
{
  SendInt32(fd, second.a); 
  SendInt8(fd, second.b); 
  SendInt64(fd, second.c);
};
 
void Receive(int fd, First& first)
{
  ReceiveInt32(fd, first.value);
  ReceiveCharArray(fd, first.s);
  first.second = new Second;
  Receive(fd, *(first.second));
}
 
void Recieve(int fd, Second& second)
{
  RecieveInt32(fd, second.a); 
  RecieveInt8(fd, second.b); 
  RecieveInt64(fd, second.c);
}
 
This is C++.  You can use something similar for C (using pointers instead of references, and distinguished names).  Don't forget to use ntoh (network Big Endian byte order to host byte order) and hton (host byte order to network Big Endian byte order) functions!
 
Alternatively, you can "flatten" your data structures into a textual representation, such as XML, and reconstitute it on the remote side.  Some situations, textual format may be preferable over some canonical binary format.
 
Notice that we have avoided your pointer problem.  We've also avoided sending along garbage intrastructure padding bytes and trailing structure alignment bytes, and we've avoided Big Endian / Little Endian problems, and we've avoided the issues caused by different platforms (OS and C/C++ compiler) having different sized fundamental data types.
 
Yet another alternative, is to work with IDL such as CORBA, or RPC, to use as a higher level language designed to address this exact problem domain.  But that may be swatting a mosquito with a nuclear warhead, depending on your needs.
 
HTH,
--Eljay
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-07-23 17:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-23  7:52 sending structure containing a pointer to another structure over TCP socket Abid Ghufran
2006-07-23 17:56 ` John (Eljay) Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).