Re: Using structs as custom protocol headers

From: Derk Gwen (derkgwen@HotPOP.com)
Date: 04/02/03


From: Derk Gwen <derkgwen@HotPOP.com>
Date: Wed, 02 Apr 2003 03:26:45 -0000


# Ok. He (the lecturer) thinks structs can be used if "youre carefull",
# i'd like to use them but if you and he say it's difficult then I'd
# probably be foolish to try.

If you're using the same compiler on the same hardware with the same options,
that would be being careful. The problem is that a C compiler will layout the
fields in the same order, but is allowed to put invisible padding bytes in the
layout as well. And different compilers, or different versions of the same
compiler can pad a structure in different ways.

# Ive done a bit of reading on endian-ness and know I have to convert
# the IP and port number, but do I have to convert all the data as
# well?

I try do it on all integers I output to a file or socket. That way I know I
can read in on any other machine.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.


Relevant Pages

  • Re: struct layout
    ... I have a doubt regarding the layout of structs in C. ... You can use preprocessor macros for this, or you could use an enum. ... Preprocessor constants are not available to the compiler and also no to ...
    (comp.lang.c)
  • Re: how is a struct represented internally.
    ... if the compiler decided to optimize the structures ... structs with identical initial layout, wich would break under a compiler ... that wouldn't lay out those structs the same way. ... there that simply assumes that there is such a guarantee. ...
    (comp.lang.c)
  • Re: Types in C
    ... Structs and unions are user-defined. ... definitions without direct compiler support. ... be portable to compilers that don't have inline functions". ...
    (comp.lang.c)
  • Re: Dynamically load a dll and us it (in a rexx dll)
    ... The available API functions are dependent on the ODBC version ... The default is usually compiler dependent. ... Depending on the data types used in the structs you ... leave .dll out, it just mentioned path. ...
    (comp.os.os2.programmer.misc)
  • Re: Byte aligned structs
    ... 'data packet' via the serial port. ... structs - i get framing errors for alignment when i apply structs on ... How can i get the compiler to byte align rather than word align. ... routines or macros to insert the shorts and integers for writing and to ...
    (comp.sys.acorn.programmer)