我有以下结构
[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
结构内的serialize
函数无法正常工作我想要一个序列byte
结构数组,用于通过网络发送它并在编写的其他应用程序中以相同的内存格式接收它在 C++ 中。
最佳答案
您可以实现ISerialized
接口(interface)
MSDN sample可以服务
关于c# - 如何序列化复杂的结构以通过 tcp/ip 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319696/