.NET的NetworkStream如何在同一个数据包中分隔多个消息?
因此,我的任务是为我们的 QA 部门创建一个工具,该工具可以从网络上读取数据包并正确地重新组装消息(他们不信任我们的日志......说来话长)。
我尝试侦听其通信的应用程序正在使用 .NET 的 TcpListener 和 TcpClient 类进行通信。拦截数据包不是问题(我正在使用 SharpPcap)。然而,事实证明,正确将数据包重新组装成应用程序级消息有点困难。
有些数据包中包含一条消息的结尾和下一条消息的开头,我无法弄清楚 .NET 中的 NetworkStream 对象如何能够判断一个应用程序级消息的结束位置和另一个应用程序级消息的开始位置。
我已经发现,任何包含应用程序级消息结尾的数据包都将打开 TCP 标头“PSH”(推送)标志。但我不明白 .NET 如何知道消息结尾在数据包内的确切位置。
一个数据包的数据可能如下所示:
/></Message><Message><Header fromSystem=http://blah
流如何知道仅将 末尾发送到应用程序并存储其余部分,直到消息的其余部分完成?
没有为分段设置 IP 级别标志,并且 .NET 套接字不了解应用程序级别协议。所以我觉得这非常令人烦恼。任何见解将不胜感激。
So I've been tasked with creating a tool for our QA department that can read packets off the wire and reassemble the messages correctly (they don't trust our logs... long story).
The application whose communication I'm attempting to listen in on is using .NET's TcpListener and TcpClient classes to communicate. Intercepting the packets isn't a problem (I'm using SharpPcap). However, correctly reassembling the packets into application level messages is proving slightly difficult.
Some packets have the end of one message and the beginning of the next message in them and I can't figure out how the NetworkStream object in .NET is able to tell where one application level message ends and the other begins.
I have been able to figure out that any packet that contains the end of an application level message will have the TCP header "PSH" (Push) flag turned on. But I can't figure out how .NET knows where exactly the end of the message is inside that packet.
The data of one packet might look like:
/></Message><Message><Header fromSystem=http://blah
How does the stream know to only send up to the end of </Message>
to the application and store the rest until the rest of the message is complete?
There are no IP level flags set for fragmentation, and the .NET sockets have no knowledge of the application level protocol. So I find this incredibly vexing. Any insight would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
流不知道任何事情的结束——它应该是应用程序协议的一部分。
NetworkStream 没有内置任何东西来将数据转换为对象。是什么让你认为它确实如此?设法使用 NetworkStream 的代码是什么样的?您是否可能执行某种形式的 XML 反序列化,并且读取代码在到达结束标记时自动停止?
基本上:
The stream doesn't know the end of anything - it should be part of the application protocol.
NetworkStream
doesn't have anything built into it to convert the data into an object. What makes you think it does? What does the code which manages to use the NetworkStream look like? Are you perhaps doing some form of XML deserialization, and the reading code automatically stops when it reaches the closing tag?Basically:
NetworkStream
itself is highly unlikely to be doing anything clever - but if you could tell us what you're observing, we can maybe work out what's going on.