Sending struct through DGRAM socket

Les Hill leh at atlantis.cis.ufl.edu
Sat Apr 20 00:37:30 AEST 1991


In article <14918 at helios.TAMU.EDU>, greg at 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 at 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 at ufl.edu  UUCP: ...!gatech!uflorida!leh  BITNET: vishnu at UFPINE



More information about the Comp.lang.c mailing list