Re: Sockets newbie - how do I pass structured data around?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 5 Jul 2005 04:01:30 -0700, "zardoz" <silangdon@xxxxxxxxxxx> wrote:

>
>I've just followed a CSocket tutorial off Codeproject which seems
>straightforward enough; it passes a file from a server to a client.
>
>I'm more interesting in passing structured data around; can someone
>give me some guidelines? Say I want to pass an object like a class. I'm
>guessing that byte packing and other issues (release / debug versions?)
>mean that passing a chunk of memory (ie pClass to pClass+sizeof(Class))
>to another computer (pCopyOfClass = recieved data buffer) would
>probably heave over.
>
>Thanks

When passing data between systems you have a lot to worry about.
There is structure packing, byte order, size of variables, floating
point representation, character representation, date/time encoding,
etc. If the two systems were produced by the same code and compiler,
you should be OK. At least until you upgrade one system and recompile
with a different version of the compiler but leave the other system
unchanged.

And that is for pure data structures that contain no pointers.

Basically, to really be safe, you must essentially define every byte
of the data stream you pass. If it must contain variable length
structures (strings), define exactly the format of how the receiving
end knows how long the string is and the string encoding.

This is one case where defining user type variables like BYTE, WORD,
SHORT, LONG instead of int, short, long can be very useful -- you
can't count on the language-defined values being the same on both
ends. You can force the user defined values to agree.




.



Relevant Pages

  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... Big-O notation isn't mathematics per se, it's computer science notation ... "length of the string". ... outrun something compiled and optimized by a good C compiler. ... > either general computing culture or culture outside computing. ...
    (comp.programming)
  • Re: [EGN] Hoisting Loop Invariants (Was: Re: [EGN] Numerical Accuracy)
    ... compiler out there somewhere that did as you claim. ... > the programmer has this knowledge, then the programmer should not use ... >> string in a loop, regardless of the blatant inefficiency of doing so. ...
    (comp.programming)
  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: PL/I string representations
    ... >> of the language, so it was interesting to me, hopefully it will be to ... I found a workable compiler for Fortran in 1971 (with a bug ... >> The specified length is the minimum, and each time a character ... >> string is assigned to E, the length is stored with it. ...
    (comp.programming)
  • Why C Is Not My Favourite Programming Language
    ... C has no string type. ... compiler take care of the rest. ... Why does any normal language ... the programmer fail. ...
    (comp.lang.c)