[MonoDevelop] Using struct with socket C#

Tom Spink tspink at gmail.com
Mon Mar 28 16:05:31 EDT 2011


Hello,

There are a couple of problems with this approach.

Firstly, you don't take into account the endianness of the two systems
- for example, Machine A could be a little-endian machine, while
Machine B might be a big-endian machine.

This means that when you convert an integer to it's raw bytes on one
machine, it'll be backwards when you convert it back on the other
machine.  This is of course a major problem when you're trying to send
information across the network, when you don't know what type of hosts
are communicating.  The way to solve this is to agree on the
byte-order for the transfer over the network, and then convert each
field one-at-a-time to the correct byte order when building the
packet.  Traditionally, network byte order or big-endian is used.

You should use a BinaryReader, and read/write each element one-by-one
into the structure, to make sure you're getting the byte ordering
correct.

Secondly, what's the type of the last element in the structure?  What
is 'signals'?

-- Tom

On 27 March 2011 21:04, Leonel Florin Selles
<leonel06013 at cfg.jovenclub.cu> wrote:
> Hi friend:
>
> Well I have a problem here, I'm creating a socket application that have a
> server and a client, and they share information in form of struct, I mean,
> the server send an struct to the client and the client work with that
> information and forward this information in the same struct to the server,
>
> I'm using the Marshal class to convert the struct into a byte[] and I
> prove this between to different struct in the same program, but, when I
> send the struct transform it in to byte[] to the client, it get the byte[]
> but when i try to use the marshal class to restore the byte[] in to struct
> the program closes without explanation, an example of what I'm doing
>
> the struct I'm use
>
> public struct cliente
>        {
>                public string clientName;
>                public int clientPid;
>                public string host;
>                public string bd;
>                public string user;
>                public string pass;
>                public string usersCheck;
>                public bool systemTryIcon;
>                public string adminpass;
>                public signals orden;
>        }
>
> and the two method to convert struct to byte[], and byte[] to struct
>
> public class dataManager
> {
>    public static byte[] structToByte (Object structura)
>        {
>                int size = Marshal.SizeOf (structura);
>                IntPtr apuntador = Marshal.AllocHGlobal (size);
>                byte[] datos = new byte[size];
>
>                Marshal.StructureToPtr (structura, apuntador, false);
>                Marshal.Copy (apuntador, datos, 0, size);
>                Marshal.FreeHGlobal (apuntador);
>
>                return datos;
>        }
>     public static System.Object ByteToStruct (byte[] listaByte, Type
> tipoStruct)
>        {
>                int size = Marshal.SizeOf (tipoStruct);
>                if (size > listaByte.Length)
>                        return null;
>                IntPtr apuntador = Marshal.AllocHGlobal (size);
>                System.Object objeto = new Object ();
>
>                Marshal.Copy (listaByte, 0, apuntador, size);
>                objeto = Marshal.PtrToStructure (apuntador, tipoStruct);
>                Marshal.FreeHGlobal (apuntador);
>
>                return objeto;
>        }
> }
>
> and using Socket to create the server and to conect the client
>
> the server
>
> Socket socketServer = new Socket (AddressFamily.InterNetwork,
> SocketType.Stream, ProtocolType.IP);
> socketServer.Bind (new IPEndPoint (IPAddress.Any, puerto));
> socketServer.Listen (100);
>
> the client
>
> ocket cliente = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
> ProtocolType.IP);
> cliente.Connect ("localhost", 4069);
>
> //converting the struct to byte[]......
>
> cliente.Send (sendData, 0, sendData.Length, SocketFlags.None);
>
>
> what can you tell my about?
>
> _______________________________________________
> Monodevelop-list mailing list
> Monodevelop-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/monodevelop-list
>



-- 
Tom Spink


More information about the Monodevelop-list mailing list