C# byte[] to struct packet

Started by
11 comments, last by Trashcann 20 years ago
I need to send a struct

struct _packet
{
  public pType header;
  public byte[] data;
};
enum pType
{
  AwaitingRequest = 0,
  Transfer = 1,
  FileName = 2,
  FileSize = 3,
  BeginTransfer = 4,
  EndTransfer = 5,
  ReadyToRecieve = 6
};
 
to a socket, but the socket.Send() only accepts byte[]s. How do you suggest that I put this struct in an array of bytes? [edited by - Trashcann on April 3, 2004 5:50:47 PM]
ermokwww.truevision3d.com
Advertisement
You need to cast it:

(char *)&yourStructInstance

~Graham
Whoops, forgot I''m not on my other machine, so the login information wasn''t saved.

~Graham

----
while (your_engine >= my_engine)
my_engine++;
I tried (byte[])pack; and (byte[])(object)pack; but on every cast I get Cast not valid.
ermokwww.truevision3d.com
quote:Original post by Anonymous Poster
You need to cast it:

(char *)&yourStructInstance

~Graham

C# is not C++.

Make a new byte array and put the values in manually.

P.S. Won''t you need to put the length of the data payload into the packet also?
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Manually assigning values works, but another method is as follows:

[StructLayout(LayoutKind.Sequential, Pack=1)]
struct YourClass {
// Whatever
}


GCHandle handle = GCHandle.Alloc(test, GCHandleType.Pinned);
try {
t = (Test2)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Test2));
}
finally {
handle.Free();
}

There''s a number of ways to accomplish this if this doesn''t do it for you, try gooling it.
Doh, wasn''t paying enough attention to the title. My apologies

~Graham

----
while (your_engine >= my_engine)
my_engine++;
@antareus: no, since an int will take the first 2 bytes, all I have to do is take whats left of it... as for seeing how long it is, thats what .Length is for

@cavemanbob: Thanks, I''ll try that out.
ermokwww.truevision3d.com
The only trouble with my method above is that you need to tell it how big the data bit is going to be. It''s probably just easier to make a constructor in the struct that takes the byte array and fills the header member and Array.Copy the rest of the data over.
You could also use MemoryStream and write to the byte[] array like a stream. That way you can use stuff like WriteInt32()... (or whatever size type you want to use to send your enum)


[edited by - joanusdmentia on April 4, 2004 6:07:23 AM]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement