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++.