私は次の構造体を持っています
[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++で記述された他のアプリケーションで同じメモリ形式で受信するための構造体のシーケンス配列が必要です。