Data transfer between sockets has recently been implemented using structs and byte array conversions. Now start tidying up. For Marshal, you can view MSDN, about byte arrays and struct-body transcoding as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Runtime.InteropServices;namespacefilesendclient{[StructLayoutAttribute (layoutkind.sequential, CharSet= CharSet.Ansi, Pack =1)] structStructdemo { Public byteA; Public byteC; [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)] Public byte[] b; Public byteD; Public inte; } unsafe classProgram {Static voidMain (string[] args) {Structdemo sd; SD.A=0; Sd.d=0; SD.C=0; SD.B=New byte[3] {0,0,1 }; SD.E=5; intSize =0; //use non-secure code here to get the value to Structdemo unsafe{size=marshal.sizeof (SD); } byte[] B =structtobytes (sd,size); Bytetostruct (b,typeof(Structdemo)); } //convert byte to struct type Public Static byte[] Structtobytes (ObjectStructobj,intsize) {Structdemo sd; intnum =2; byte[] bytes =New byte[size]; INTPTR structptr=Marshal.allochglobal (size); //copy the structure to the allocated memory spacemarshal.structuretoptr (Structobj, Structptr,false); //Copy from memory space to byte arraymarshal.copy (structptr, Bytes,0, size); //free up memory spaceMarshal.freehglobal (STRUCTPTR); returnbytes; } //convert byte to struct type Public Static ObjectBytetostruct (byte[] bytes, type type) { intSize =marshal.sizeof (type); if(Size >bytes. Length) {return NULL; } //allocating structure body storage spaceIntPtr structptr=Marshal.allochglobal (size); //copies a byte array to the allocated memory spacemarshal.copy (Bytes,0, structptr, size); //Convert the memory space to the target structure Objectobj =marshal.ptrtostructure (structptr, type); //free up memory spaceMarshal.freehglobal (STRUCTPTR); returnobj; } }}
about struct and byte array conversions in C #