[comp.lang.c] Sending struct through DGRAM socket

greg@carnivore.tamu.edu (Greg Economides) (04/18/91)

I am trying to communicate with a forked process using a DGRAM socket
(AF_UNIX) on a Sun 386i (OS 4.0.2).  What I really want to send through
the socket is a small struct (about five float fields).  I have been
using the `sendto' and `read' functions which need a (char *) to send
through the socket.  These work fine with character strings, but do not
do so well with structs.

Is there a quick/clean way to translate a struct into a character stream 
(and then a way to get the struct back) so that it can be sent using these
functions?  Is there a better or more straight-forward way of doing this?
I am still quite a fledgling sockets programmer.

Thanks for any and all help.  I will summarize the responses if there is
interest.

Peace,
--
Greg Economides                          "This sentence is false" 
Technical Lab Coordinator 
Texas A&M University -- Biosystems Modelling Group
Internet: greg@carnivore.tamu.edu

markh@csd4.csd.uwm.edu (Mark William Hopkins) (04/18/91)

In article <14918@helios.TAMU.EDU> greg@carnivore.tamu.edu (Greg Economides) writes:
>Is there a quick/clean way to translate a struct into a character stream 
>(and then a way to get the struct back) so that it can be sent using these
>functions?

Structs are formatted in a machine-dependent and compiler-dependent way.  You
absolutely need to set up routines to break apart and form the struct by
hand.  You know, like:

	  sprintf(Buf, "%5.3f:%5.3f:%5.3f\n", A->X, A->Y, A->Z);
          SendBytes(Buf);

and whatever the corresponding sscanf and "RecvBytes" statements are.

mjr@hussar.dco.dec.com (Marcus J. Ranum) (04/19/91)

greg@carnivore.tamu.edu (Greg Economides) writes:

>Is there a quick/clean way to translate a struct into a character stream 
>(and then a way to get the struct back) so that it can be sent using these
>functions?

	There is no "clean" way to do it - take a look at Sun's XDR
protocol (if you're on a 386i it should be in your doc kit) and all
the RPC/XDR goo. Basically, it involves writing a function that will
convert your structure into a padded, folded, spindled, and mutilated
format that will transport over the net - and then you decode it on
the other side.

mjr.

leh@atlantis.cis.ufl.edu (Les Hill) (04/19/91)

In article <14918@helios.TAMU.EDU>, greg@carnivore.tamu.edu (Greg Economides) writes:
|> I am trying to communicate with a forked process using a DGRAM socket
|> (AF_UNIX) on a Sun 386i (OS 4.0.2).  What I really want to send through
|> the socket is a small struct (about five float fields).  I have been
|> using the `sendto' and `read' functions which need a (char *) to send
|> through the socket.  These work fine with character strings, but do not
|> do so well with structs.
|> 
|> Is there a quick/clean way to translate a struct into a character stream 
|> (and then a way to get the struct back) so that it can be sent using these
|> functions?  Is there a better or more straight-forward way of doing this?
|> I am still quite a fledgling sockets programmer.
|> Greg Economides                          "This sentence is false" 
|> Technical Lab Coordinator 
|> Texas A&M University -- Biosystems Modelling Group
|> Internet: greg@carnivore.tamu.edu

If the processes are on the same machine, shared memory (shmctl, shmget, shmop,
shmat, shmdt) if available (dont have a 386i to look at) might be a viable alternative [dont forget the key routine (whose name escapes me at the moment.)]

If you are bent on sending it over the socket, try this:

	err = sendto(s, (char *)&mystruct, sizeof mystruct, to, tolen);

After all, there is nothing magic about character streams :)

BTW, the above is a BAD thing to depend on if this will be ported to any other
architectures (including other Suns!)

Les Hill
-- 
Extraordinary crimes against the people and the state have to be avenged by
agents extraordinary.  Two such people are John Steed -- top professional, and
his partner, Emma Peel -- talented amateur; otherwise known as "The Avengers."
INTERNET: leh@ufl.edu  UUCP: ...!gatech!uflorida!leh  BITNET: vishnu@UFPINE

bergquis@nms.gdc.portal.com (Brett Bergquist) (04/20/91)

mjr@hussar.dco.dec.com (Marcus J. Ranum) writes:
> greg@carnivore.tamu.edu (Greg Economides) writes:
> 
> 	There is no "clean" way to do it - take a look at Sun's XDR
> protocol (if you're on a 386i it should be in your doc kit) and all
> the RPC/XDR goo. Basically, it involves writing a function that will
> convert your structure into a padded, folded, spindled, and mutilated
> format that will transport over the net - and then you decode it on
> the other side.

I am currently using XDR in this fashion to send a structure with embedded
pointers to other structures across an unix pipe.  To do so, I created a
RPCGEN description of the message and used "rpcgen" to create the header
and XDR encoding/decoding module.  I works pretty good, and the RPCGEN
description file makes for good documentation too.-- 
Brett M. Bergquist, Principal Engineer | "Remind me" ... "to write an
General DataComm, Inc.,                | "article on the compulsive reading
Middlebury, CT 06762                   | of news." - Stranger in a Strange Land
Email: bergquis@nms.gdc.portal.com  or ...!portal!gdc!nms!bergquis