Re: structs for data transfer?

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 09/19/04


Date: Sun, 19 Sep 2004 20:14:18 +0100


"Oliver Gerlich" <olig9_blocker_@gmx.de> wrote in message
news:cikc2c$qet$01$1@news.t-online.com...
> Hello,
> I want to transfer messages between a client and a server (over TCP
> sockets). A message consists of a message type (like a message "subject"
> :), the size of the attached data, and the data itself. The data part
> should then be able to contain some information whose layout depends on
> the message type...
> So now I thought I could define some structs which represent the layout of
> the additional information, like this:
>
> typedef struct
> {
> char versionString[30]
> bool paused;
> int uptime;
> } MsgCoreInfoStruct;
>
> And then I use something like this:
>
> MsgCoreInfoStruct myData;
> strcpy(myData.versionString, "Server V1.0");
> myData.uptime = getUptime();
> myData.paused = false;
>
> Message m;
> m.setData( (char*)(&myData) , sizeof(myData) );
>
> The Message object then sends the data to the other side.
>
> Question: When I receive such a message, can I just cast the char* (which
> points to the data) into MsgCoreInfoStruct* myRecvData?
> And can I then use myRecvData->uptime to get the value I sent out?
> And, last but not least ;) , can I use this concept if client and server
> are running on different platforms (in this case, Linux and Win)?
>
> I have doubts about this (because of data packing, and because
> sizeof(bool) might be inconsistent between compilers...), but I'm not
> sure...

You are right to doubt.

>
> Can someone tell me if this concept is right or wrong? Or has someone a
> better solution for this problem (maybe something else than structs)?
>

Write some code to convert the struct you want to send into a char array.
Write some code to turn that char array back into a struct. Use the first
piece of code when you send, the second when you recieve. Trying to send
anything more complicated than char arrays between different types of
computers is asking for trouble.

> Thanks in advance,
> Oliver Gerlich

john



Relevant Pages

  • Re: Pre-offsetof() question
    ... > now-standard offsetof() macro. ... consider the typical layout of ... pre-offsetofcompiler decide to make ... > the layout of the struct vary, ...
    (comp.lang.c)
  • Re: [RFC] x86: xsave/xrstor support, ucontext_t extensions
    ... Appended patch adds the support for xsave/xrstor infrastructure for x86. ... only user-space consumer of kernel types: there are other libcs, ... struct sigcontext uc_mcontext; ... You're changing the layout of struct ucontext in two ways: ...
    (Linux-Kernel)
  • Re: Structure size directives
    ... struct foo { ... The fact that MANY compilers give the programmer ... The best way for the compiler to give the programmer a way to specifiy ... forcing a structure layout to meet some external constraint may be. ...
    (comp.lang.c)
  • Re: structs for data transfer?
    ... > John Harrison wrote: ... >> Write some code to convert the struct you want to send into a char array. ... or sentinels and delimeters for the ends of strings. ...
    (comp.lang.cpp)
  • Re: Right way of passing unmanaged structures between managed and native code?
    ... I knew that __value struct may not be binary compatible ... > SomeType SomeMember; ... > CLR sense as an opqaue with explicit layout and explicit ...
    (microsoft.public.dotnet.languages.vc)