解析 C# bootp 服务器的二进制数据?
我需要我的 C# .NET 应用程序之一充当 bootp 服务器。该协议非常简单,但我不知道构建/解析二进制数据的简单方法。
任何想法:
(来源:tcpipguide.com)
I need one of my C# .NET applications to act as a bootp server. The protocol is pretty simple but I dont know an easy way to build/parse the binary data.
Any ideas:
(source: tcpipguide.com)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个简单的结构,例如:
并使用 中的代码这篇博文用于序列化/反序列化数据包。但由于编码差异,ServerName 和 BootFilename 可能存在问题,并且您可能需要为每个字段指定准确的 FieldOffset(请参阅 msdn 上的此主题了解详细信息)。
希望这会有所帮助:)
You can create a simple structure like:
and use the code from this blogpost to serialize/deserialize packets. But there may be a problem with ServerName and BootFilename because of differences in encoding and probably you need to specify exact FieldOffset for each of the fields (see this topic on msdn for details).
Hope this will help :)
有几种方法可以做到这一点。您也许可以使用封送属性 例如 StructLayout 将结构打包到字节数组中,但这可能很棘手并且不值得付出努力。
您可以使用专门的框架(例如 Protobuf)以这种方式为类添加属性它将被序列化以匹配您需要的结构。
但根据我的经验,创建这样的二进制结构的最简单、最快和最灵活的方法是使用 MemoryStream 类来保存字节缓冲区,然后使用 BinaryWriter 围绕它来实际将二进制数据写入流中。
无论如何,有一个可用的服务器可供参考是有帮助的。使用 Wireshark 或 Microsoft Network Monitor 等工具来捕获有线流量,以便您可以将您的有线格式与已知有效的示例进行比较。
There's a couple ways to do this. You might be able to play around with the marshaling attributes such as StructLayout to pack a structure into a byte array but this is probably tricky and not worth the effort.
You could use a specialized framework such as Protobuf to attribute a class in such a way that it will be serialized to match the structure you need.
But in my experience, the easiest, fastest, and most flexible method of creating a binary structure like this is to use a MemoryStream class to hold a buffer of byes, then use a BinaryWriter around it to actually write the binary data into the stream.
In any case, it helps to have a working server to reference. Use a tool like Wireshark or Microsoft Network Monitor to capture the wire traffic so you can compare your wire format to an example that is known to work.