最佳实践:在C#中已知的协议生成和分析网络数据包? [英] Best Practice: generating and parsing network packets for a known protocol in C#?
问题描述
我试着问了的最近然而经过几个答案,我意识到这是不是要问正确的事情类似的问题。
I tried asking a similar question recently however after several answers I realized it wasn't the correct thing to ask.
是否有引导或普遍接受的做法生成和分析基于像SMPP现有协议的网络数据包(短消息对等网络协议[用于短信])或各种聊天协议,如热线或IRC与C#。
Is there a guide or a generally accepted practice for generating and parsing network packets based on an existing protocol like SMPP (Short Message Peer-to-Peer Protocol [Used for SMS Messages]) or various chat protocols like HOTLINE or IRC with C#.
一提到谷歌 Protocol Buffers的<答案/ A>和 .NET版本。看,我才意识到序列化,如果你有超过两端(客户端和服务器)的控制是非常有用的经过。在那里我会是协议规范下。
One of the answers mentioned Google Protocol Buffers and a .NET version. After looking at that I realized that serialization is extremely useful if you have control over both ends (client and server). Where I would be following the protocol spec.
生成
。在过去,我已经做了一个类或结构与那些性质网络数据包/协议的一部分:
Generating In the past I have made a class or structure with properties that are part of the network packet / protocol:
// PSEUDO CODE
class NetworkObject
{
private int length;
private string data;
// ...
public byte[] GetBytes()
{
// Convert string and int to byte array
return byte array;
}
}
的
类有一个的字节[] getBytes()构造方法我称之为一旦我设置相应的属性。我采取的字节数组,并把它在网络上。在我前面的问题的答案的一个建议存储这似乎是一个更好的主意内存流或字节流中的数据。
The class has a byte[] GetBytes() method which I call once I've set the appropriate properties. I take that byte array and send it over the network. In my previous question one of the answers suggested storing the data in a memory stream or bytestream which seems like a better idea.
这似乎是我的字节数组方法可能更慢不是通过一个网络流,根据直接类属性到流写入分组数据的方法。这是我可能会做,除非我失去了一些东西真的很大这里。
It seems like my byte array method is probably slower than passing a network stream to a method that writes the packet data according to class properties directly to the stream. Which is what I will probably do unless I'm missing something really big here.
解析
解析时我使用各种情况声明逐句通过各种PDU和产生NetworkObject与填充相应的属性,并用它做什么,因为我认为合适的。
Parsing When parsing I use various case statements to step through the various PDUs and generate a NetworkObject with the appropriate properties populated and do with it as I see fit.
推荐答案
你为什么不尝试对象的序列化?
Why don't you try Serialization of objects?
这篇关于最佳实践:在C#中已知的协议生成和分析网络数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!