passing structs thru sockets?

passing structs thru sockets?

Post by Adam Mig » Sun, 17 Dec 1995 04:00:00

I didn't see the original question, but by the subject i think i can
piece together what you want.  You need to use XDR, It stands for
eXternal Data Representation.  It was developed by Sun Microsystems.

XDR allows you to pass structured data over the network in a system
architecture independent way by "encoding" it into a stream in
big endian format.  The other end then decodes the stream and stores
it as dictated by the system.

This is most commonly used (do to its ease of use) in RPC since rpcgen
a tool used to develop RPC software does the dirty work for you.  It
can be used without RPC though, and picking up some RPC code from
somewhere will provide you with a fair bit of example code.

Hope this helps.

>    If I understand your question, you can't really share data areas.
>    Assuming your client and server processes are on two machines, there
>    is no common resource to share other than the network.  You would
>    have to syncronize the data structure between process whenever it
>    changes.  Not pretty but easily doable.
>    If the two processes are on the same machine, however, use shared
>    memory.  That's what it's for.
>    Hope this helps.
>    ____                      __  ___                          
>   / __ \____ __   _____     /  |/  /___  ____  _________  ___
>  / / / / __ `/ | / / _ \   / /|_/ / __ \/ __ \/ ___/ __ \/ _ \
> / /_/ / /_/ /| |/ /  __/  / /  / / /_/ / / / / /  / /_/ /  __/
>/_____/\__,_/ |___/\___/  /_/  /_/\____/_/ /_/_/   \____/\___/


|      Computer Science, Memorial University  |

1. passing structs thru sockets?

I assume that by '...share the same structure...' you mean that you want
to send a struct of data from process A to process B.  If this is not the
case, then you can ignore the rest of this.

The problem being that the send command wants the fd, a char*, the len,
and flags.  Of course, the recv command also wants these four.  To get
around this, we cast the struct to char* to make it go through like this.

struct myData
  int msgnum;
  char text[100];
  int something;
  int somethingelse;

// define the actual data
myData mydata;

// to send the stuff
send(socketfd, (char*)mydata, sizeof(myData), 0);

// to receive the stuff
recv(socketfd, (char*)mydata, sizeof(myData), 0);

I'm still putting the finishing touches on a class that uses this.  So far
it seems to be working just fine.  I'm having problems, but not with the
send/recv portion.  If anyone has a better idea or a cleaner/more standard
syntax, I'm more than willing to learn what it is.


2. Netscape Woes

3. [PATCH] pcmcia: pass struct pcmcia_socket instead of socket nr to drivers

4. defrag


6. X servers

7. Passing file descriptors thru rexec() socket

8. memory config/socket placement

9. passing structs with gtk+ and C

10. HElp! passing struct as an argument

11. Passing a struct though a POSIX fifo???

12. struct msghdr, passing fds between processes

13. Q: passing a struct record to vprintf ?