在 C# 中创建网络数据包结构/类的最佳方法是什么?
我想知道是否有任何好的指南或书籍可以解释在 C# 中处理网络数据包通信的最佳方法?
现在我正在使用一个结构和一个根据该结构的值生成字节数组的方法。
有没有更简单的方法来做到这一点? 或者甚至是更好的方法?
public struct hotline_transaction
{
private int transaction_id;
private short task_number;
private int error_code;
private int data_length;
private int data_length2;
...
public int Transaction_id
{
get
{
return IPAddress.HostToNetworkOrder(transaction_id);
}
set
{
transaction_id = value;
}
}
...
public byte[] GetBytes()
{
List<byte> buffer = new List<byte>();
buffer.Add(0); // reserved
buffer.Add(0); // request = 0
buffer.AddRange(BitConverter.GetBytes(Task_number));
buffer.AddRange(BitConverter.GetBytes(Transaction_id));
buffer.AddRange(BitConverter.GetBytes(error_code));
buffer.AddRange(BitConverter.GetBytes(Data_length));
buffer.AddRange(subBuffer.ToArray());
return buffer.ToArray(); // return byte array for network sending
}
}
除此之外,是否有关于将网络数据解析为可用结构/类的最佳实践的良好指南或文章?
I'm wondering if there are any good guides or books that explain the best way to handle network packet communication in C#?
Right now I'm using a structure and a method that generates a byte array based on values of the structure.
Is there a simpler way to do this? Or even a better way?
public struct hotline_transaction
{
private int transaction_id;
private short task_number;
private int error_code;
private int data_length;
private int data_length2;
...
public int Transaction_id
{
get
{
return IPAddress.HostToNetworkOrder(transaction_id);
}
set
{
transaction_id = value;
}
}
...
public byte[] GetBytes()
{
List<byte> buffer = new List<byte>();
buffer.Add(0); // reserved
buffer.Add(0); // request = 0
buffer.AddRange(BitConverter.GetBytes(Task_number));
buffer.AddRange(BitConverter.GetBytes(Transaction_id));
buffer.AddRange(BitConverter.GetBytes(error_code));
buffer.AddRange(BitConverter.GetBytes(Data_length));
buffer.AddRange(subBuffer.ToArray());
return buffer.ToArray(); // return byte array for network sending
}
}
Beyond that is there a good guide or article on the best practice of parsing network data into usable structures / classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您听说过 Google 协议缓冲区吗?
Have you heard of google protocol buffers?
好吧,我会倾向于使用
Write(Stream)
,而不是GetBytes()
,以防它很大……但在一般情况下,有用于此的序列化 API...我会提到我自己的,但我认为人们会厌倦听到它。顺便说一句,在我看来,
hotline_transaction
看起来更像是一个class
(而不是struct
)。Well, rather than
GetBytes()
, I'd be tempted to use aWrite(Stream)
, in case it is big... but in the general case there are serialization APIs for this... I'd mention my own, but I think people get bored of hearing it.IMO,
hotline_transaction
looks more like aclass
(than astruct
) to me, btw.您可能应该为此使用
BinaryWriter
,而不是返回byte[]
,您应该将BinaryWriter
传递给序列化代码,即,您可以轻松地使用
BinaryWriter
包装现有的Stream
。 如果您确实需要以某种方式获取byte[]
,您可以使用MemoryStream
作为后备流,并在需要时调用ToArray
完毕。You should probably use a
BinaryWriter
for this, and rather than returningbyte[]
, you should pass aBinaryWriter
to the serialization code, i.e.,You can easily wrap an existing
Stream
with aBinaryWriter
. If you really need to get abyte[]
somehow, you can use aMemoryStream
as a backing stream, and callToArray
when you're done.