structure sending thru socket

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello
I have structure:
public struct buf
{
public int type;
public int len;
public byte [] data;
};
and in my program:
String d="something";
send_buf.data=Encoding.ASCII.GetBytes(d);

and after that i want to send whole struct throught my socket function:
System.Net.Sockets.Socket.Send().
How can i convert my buf structure to byte [] ?
I need to be assured that for example:
field type (int) will be 32 bits (even when type will use only first few
bits), and so on...

How can i do it?
 
Look at the Marshal.StructToPtr. If the struct is that simple, you may also
want to convert each int to any array using the Convert class, create a new
array of size 4 + 4 + data.length and copy all three arrays into it. If
..net on both sides, you should not have to worry about converting the int's
into network byte order as they both sides will use the same ordering. You
could also use unsafe code and get a ptr to the first byte of each
respective type and copy the bytes to a new array, etc.
 
Back
Top