我有以下结构
[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;
}
}
在我的代码中的一些地方我这样做
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
struct 中的serialize
函数不能正常工作我想要一个byte
结构的序列数组,用于通过网络发送它并在其他用 c++ 编写的应用程序中以相同的内存格式接收它。