.NET的NetworkStream如何在同一个数据包中分隔多个消息?

发布于 2024-08-20 02:18:57 字数 731 浏览 6 评论 0原文

因此,我的任务是为我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2024-08-27 02:18:57

流不知道任何事情的结束——它应该是应用程序协议的一部分。

NetworkStream 没有内置任何东西来将数据转换为对象。是什么让你认为它确实如此?设法使用 NetworkStream 的代码是什么样的?您是否可能执行某种形式的 XML 反序列化,并且读取代码在到达结束标记时自动停止?

基本上:

  • 如果您的协议没有任何消息分隔符或长度前缀,它可能应该有
  • NetworkStream 本身不太可能做任何聪明的事情 - 但如果您可以告诉我们您正在观察什么,我们也许可以弄清楚发生了什么事。

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:

  • If your protocol doesn't have any message delimiter or length prefix, it probably should have
  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文