VB.Net套接字通信,接收到异常数据
我正在尝试通过套接字与我的 PC 上的本地 AS3 Air 应用程序进行通信。 在本例中,VB.Net 应用程序充当服务器,而 Air 应用程序则充当客户端。
.Net 应用程序每 25 毫秒向 Air 应用程序发送一次数据。我还没有发送任何终止消息。 我只是在Air应用程序上接收数据并进行处理。
在接收端,当我调试数据时,我发现一些转换已被连接。
就像如果我连续发送 "ABCDEF"
在大多数情况下我确实收到 "ABCDEF"
但在某些情况下我收到 "ABCDEFABCDEF"
是这样吗底层 TCP 网络可能正在组合数据,然后将其作为一个数据包发送。 我是否真的需要发送一些消息终止符,例如像这样的 "ABCDEF$"
,然后在接收端拆分我的消息?
我是不是搞砸了?
I am trying to communicate to a local AS3 Air app on my PC via sockets.
The VB.Net app is acting as a server in this case while the Air app is a client.
The .Net app sends data every 25ms to the Air app. I have not send any message termination.
I just receive the data on Air app and process it.
On the receiving side when I debug the data I see that that some transitions have been concated.
Something like if I send "ABCDEF"
successively for most cases I do receive "ABCDEF"
however in some cases I receive "ABCDEFABCDEF"
Is it possible that the underlying TCP network is combining the data and then sending it as one packet.
Do I really need to send some message terminator for instance like this "ABCDEF$"
and then split my messages at the receiving end?
Am I goofing up big time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Socket.Send 的文档中:
也不能保证您会在接收端收到完整的消息 - 这就是套接字的工作原理。我建议添加一个“消息结束”标记,并在接收代码中循环,直到收到完整的消息 - 但您可能会收到一个跨越两条消息的缓冲区,因此当您有“消息结束”标记时,您需要保留该点之后收到的所有数据,以用于重建下一条消息。
即假设您有两条消息要接收 - “AAAAAA”和“BBBBBBBB”。您可能会从三个单独的 Receive 调用中收到以下内容(添加 $ 作为消息结束标记后):
From the documentation for Socket.Send:
There's also no guarantee that you'll receive a complete message at the receiving end either - this is just how sockets work. I'd suggest adding an "End of message" marker, and looping in your receive code until you receive a complete message - but you may receive a buffer that straddles two messages, so when you've got an "End of message" marker, you need to keep hold of any data received after that point, for use in reconstructing the next message.
I.e. imagine that you have two messages to receive - "AAAAAA" and "BBBBBBBB". You might receive the following from three separate calls to Receive (after adding $ as an end of message marker):
您可以设置
NoDelay
在套接字上设置为 True?Can you set
NoDelay
to True on the socket?