2

I have following struct

[Serializable()]
public struct Transfer_packet 
  {
     public int  _packet_type; // 0 is action 1 is data
     public int _packet_len; // length of data
     public byte[] _data;//Content of data it's Length depends on objects types 

        public byte[] serialize()
        {
            byte[] arr;
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            arr = ms.ToArray();
           return arr;
        }
    }

some where in my code i do this

            Transfer_packet sndpkt;  
            string cmd = "Some Commands in text or binary bytes";
            byte[] data = ASCIIEncoding.ASCII.GetBytes(cmd);
            sndpkt._packet_type = 0; // Action Packet 
            sndpkt._packet_len = data.Length;  // Length of command
            sndpkt._data = data;
            byte[] SendData = sndpkt.serialize();
            LanAdapter.Send(SendData, System.Net.Sockets.SocketFlags.None); // LanAdapter ->TcpSocket

the serialize function inside struct does not work fine I want a sequence byte array of structure for sending it over net and receive it in same memory format in other application written in c++.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
saeed
  • 2,477
  • 2
  • 23
  • 40

3 Answers3

1

You may implement ISerializable interface
MSDN sample may serve

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
  • i have try marshaling as well but it does not provide me a correct memory map for sending over net and handle it in c++ application thanks for your answer i will check i am looking for a particular method ;) – saeed Mar 10 '13 at 07:28
  • 1
    http://stackoverflow.com/questions/5209297/about-c-sharp-struct-memory-serialization-overhead and http://stackoverflow.com/questions/628843/byte-for-byte-serialization-of-a-struct-in-c-sharp and http://stackoverflow.com/questions/9944994/fast-serialization-deserialization-of-structs may be usful. – Mohsen Heydari Mar 10 '13 at 08:05
  • before i ask this question i googled it and found some methods but none of those converts my struct as i want i change serial code to this – saeed Mar 10 '13 at 08:18
1

You want to checkout protobuf-net for high performance, platform agnostic, serialisation of objects. It is IMHO hands down the best.

Modifiy your object with the necessary attributes:

[ProtoBuf.ProtoContract]
public struct Transfer_packet
{
    [ProtoBuf.ProtoMember(1)]
    public int _packet_type; // 0 is action 1 is data
    [ProtoBuf.ProtoMember(2)]
    public int _packet_len; // length of data
    [ProtoBuf.ProtoMember(3)]
    public byte[] _data;//Content of data it's Length depends on objects types 

    /// <summary>
    /// Private constructor required by protobuf
    /// </summary>
    private Transfer_packet() { }
}

Usage is a piece of CAKE:

// write to a file
Serializer.Serialize(outputStream, Transfer_packet);

// read from a file
var person = Serializer.Deserialize<Transfer_packet>(inputStream);
MarcF
  • 3,169
  • 2
  • 30
  • 57
  • I am searching for a wise and standard method to send and receive data in native c++ network program which targets windows platform. By the way thanks for your attentions. – saeed Nov 05 '13 at 06:15
0

I change the serialize function inside struct Transfer_packet to this and it works fine for me butt i want a smart solution not this one, marshalling and Iserialize adds some headers or change the actual byte array to some thing that i don't know.

 public struct Transfer_packet 
        {
            public short  _packet_type; // 0 is action 1 is data
            public int _packet_len; // length of data
            public byte[] _data;//Content of data it's Length depends on objects types 

            public byte[] serialize()
            {
                byte[] arr;
                MemoryStream ms = new MemoryStream();
                arr = BitConverter.GetBytes(this._packet_type);
               // Array.Reverse(arr);
                ms.Write(arr, 0, arr.Length);
                arr = BitConverter.GetBytes(this._packet_len);
               // Array.Reverse(arr);
                ms.Write(arr,0,arr.Length);
                ms.Write(this._data, 0, this._data.Length);
                arr = ms.ToArray();
               return arr;
            }
        }
saeed
  • 2,477
  • 2
  • 23
  • 40