The compiler takes your standard C code and puts things in memory where it
wants to, usually to maximize performance of your code. For example, it's a
*lot* faster to read a 'long' from memory if the long is located at an
address which is divisible by four. When it's not, the memory bus must make
several accesses to the memory to get the value. When it is properly
aligned, a single access occurs. So, when you declare a structure with some
types of various sizes in it, the compiler *may* arrange it in a way so that
the first field *is not contiguous* with the second, etc., as I indicated in
my last message.
Network applications commonly assume that all 'structured types' will be
sent packed together as closely as possible (to conserve bandwidth). It is
*frequently* necessary to take your nice fast run-time structures and pack
them up for shipment across the network.
What you should do, if you are writing both ends of the communication
channel is *specify what goes across the network*. Then, you can write the
code on both ends to make sure that it happens that way.
Paul T.
"Zahid" <(E-Mail Removed)> wrote in message
news:013b01c3aad4$16534350$(E-Mail Removed)...
> Hi,
>
> Paul, what do you mean by structure packing settings? I
> have been given a complete interface spec with lots of
> structure declarations.
>
> Also, Im trying to write a Test Emulator that will send
> data back so I want to know how to interpret the recieved
> data back into one of the structures and use it in my
> program.
>
> Thanks in advance.
> >-----Original Message-----
> >OK, there's still some missing information. What are
> the structure packing
> >settings on that program. The second structure,
> particularly, is going to
> >be sensitive to that. With a single character at the
> *beginning* of the
> >structure, it could be packed in several different ways:
> >
> >offset field
> >0 Request
> >1 ServerNo
> >
> >or
> >
> >0 Request
> >2 ServerNo
> >
> >or even
> >
> >0 Request
> >4 ServerNo
> >
> >Once you know that, you can start coding up something to
> do the conversion
> >for each structure in your managed code. You'd do
> something like this:
> >
> >public class TestStruct
> >
> >{
> >
> > public byte Request;
> >
> > public short ServerNo;
> >
> >
> >
> > private const int offsetRequest = 0;
> >
> > private const int sizeRequest = 1;
> >
> > private const int offsetServerNo = 2;
> >
> > private const int sizeServerNo = 2;
> >
> > private const int arraySize = 4;
> >
> >
> >
> > public byte[] ToByteArray()
> >
> > {
> >
> > byte[] ba = new byte[ arraySize ];
> >
> > byte[] requestbytes = BitConverter.GetBytes(
> Request );
> >
> > byte[] servernobytes = BitConverter.GetBytes(
> ServerNo );
> >
> > Buffer.BlockCopy( ba, offsetRequest,
> requestbytes, 0,
> >requestbytes.Length );
> >
> > Buffer.BlockCopy( ba, offsetServerNo,
> servernobytes, 0,
> >servernobytes.Length );
> >
> > return ba;
> >
> > }
> >
> >};
> >
> >
> >
> >Paul T.
> >
> >"Zahid" <(E-Mail Removed)> wrote in message
> >news:011101c3aaca$82559b70$(E-Mail Removed)...
> >> Hi,
> >>
> >> Here is the code that the C/MFC program uses:
> >>
> >> #define MAXDATA 500
> >>
> >> typedef struct xvnetpkt_def {
> >> unsigned char id
> >> unsigned char prid[2];
> >> unsigned short seqno;
> >> unsigned short length;
> >> unsigned char code;
> >> char data[MAXDATA];
> >> } XVNetPkt;
> >>
> >> typedef struct srvrlogon_def {
> >> unsigned char Request;
> >> unsigned Short ServerNo;
> >> } XV_SRVRLOGON;
> >>
> >> Now that youve seen the actual data format what would
> you
> >> suggest? How shall I go about solving this riddle?
> >>
> >> Thanks in advance.
> >> >-----Original Message-----
> >> >There is no single right answer to 'convert this
> >> structure to a byte array',
> >> >so nothing can be done automatically. If you know
> what
> >> layout is correct
> >> >for the other end of your socket connection, add a
> >> method to each structure
> >> >or class called something like ToByteArray(), which
> >> returns a byte array,
> >> >and have that method pack things into the byte array
> the
> >> way your
> >> >communication partner wants them.
> >> >
> >> >Paul T.
> >> >
> >> >"Zahid" <(E-Mail Removed)> wrote in message
> >> >news:3ebd01c3aab2$926e2b20$(E-Mail Removed)...
> >> >> Hi,
> >> >>
> >> >> I have a number of structures that I want to send
> using
> >> >> UDP protocol. How do I convert the structure into
> >> >> bytes/array of bytes? Using UDP protocol is a MUST -
> a
> >> >> Client Requirement for my PocketPC application.
> >> >>
> >> >> Am I doing it all wrong? Should I create Classes
> >> instead
> >> >> of these objects- if so how does that help?
> >> >>
> >> >> In particular How would I send/Convert the
> array "data
> >> ()"
> >> >> into Bytes to send with the structure?
> >> >>
> >> >> This code is part of an Interface specification that
> >> >> communicates with a program written C/MFC code.
> >> >>
> >> >> Thanks in advance
> >> >> Here is my code:
> >> >>
> >> >> Public Structure xvnetpkt_type
> >> >> Public id As Byte
> >> >> Public prid() As Byte
> >> >> Public seqno As UInt16
> >> >> Public length As UInt16 'Length of data
> >> >> Public code As Char 'reply Code -
> >> >> Public data() As srvrlogon_type 'Data
> >> >>
> >> >> Public Sub Initialize()
> >> >> ReDim prid(2)
> >> >> ReDim data(MAXDATA)
> >> >> End Sub
> >> >>
> >> >> End Structure
> >> >>
> >> >> Public Structure srvrlogon_type
> >> >> Public Request As Char
> >> >> Public ServerNo As UInt16
> >> >> End Structure
> >> >>
> >> >> Structure sell_type
> >> >> Public Request As Char '10 = sell items
> >> >> Public TableNo As UInt16
> >> >> Public SplitNo As UInt16
> >> >> Public nItems As UInt16 'No of ITEMINFO2
> (max
> >> 60)
> >> >> Public Items() As iteminfo2_type
> >> >> Public Sub Initialise()
> >> >> ReDim Items(60)
> >> >> End Sub
> >> >> End Structure
> >> >>
> >> >> Structure iteminfo2_type
> >> >> Public PLU As UInt64
> >> >> Public Flags As UInt16
> >> >> Public quantity As UInt16
> >> >> End Structure
> >> >>
> >> >
> >> >
> >> >.
> >> >
> >
> >
> >.
> >
|