VB.Net套接字通信,接收到异常数据

发布于 2024-10-19 03:56:00 字数 438 浏览 1 评论 0原文

我正在尝试通过套接字与我的 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 技术交流群。

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

发布评论

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

评论(2

剑心龙吟 2024-10-26 03:56:00

Socket.Send 的文档中:

也不保证您发送的数据会立即出现在网络上。为了提高网络效率,底层系统可能会延迟传输,直到收集到大量传出数据。 Send 方法的成功完成意味着底层系统有空间来缓冲网络发送的数据。


也不能保证您会在接收端收到完整的消息 - 这就是套接字的工作原理。我建议添加一个“消息结束”标记,并在接收代码中循环,直到收到完整的消息 - 但您可能会收到一个跨越两条消息的缓冲区,因此当您有“消息结束”标记时,您需要保留该点之后收到的所有数据,以用于重建下一条消息。

即假设您有两条消息要接收 - “AAAAAA”和“BBBBBBBB”。您可能会从三个单独的 Receive 调用中收到以下内容(添加 $ 作为消息结束标记后):

AAAA
AAAA$BB
BBBBBB$

From the documentation for Socket.Send:

There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the Send method means that the underlying system has had room to buffer your data for a network 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):

AAAA
AAAA$BB
BBBBBB$
再见回来 2024-10-26 03:56:00

您可以设置 NoDelay 在套接字上设置为 True?

Nagle 算法旨在通过使套接字缓冲小数据包,然后在某些情况下将它们合并在一个数据包中发送来减少网络流量。 TCP 数据包由 40 字节的标头和正在发送的数据组成。当使用 TCP 发送小数据包时,TCP 标头产生的开销可能成为网络流量的重要组成部分。在负载较重的网络上,这种开销造成的拥塞可能会导致数据报丢失和重传,以及拥塞导致的传播时间过长。当用户有新的传出数据到达时,如果连接上任何先前传输的数据仍未得到确认,Nagle 算法将禁止发送新的 TCP 段。

Can you set NoDelay to True on the socket?

The Nagle algorithm is designed to reduce network traffic by causing the socket to buffer small packets and then combine and send them in one packet under certain circumstances. A TCP packet consists of 40 bytes of header plus the data being sent. When small packets of data are sent with TCP, the overhead resulting from the TCP header can become a significant part of the network traffic. On heavily loaded networks, the congestion resulting from this overhead can result in lost datagrams and retransmissions, as well as excessive propagation time caused by congestion. The Nagle algorithm inhibits the sending of new TCP segments when new outgoing data arrives from the user if any previouslytransmitted data on the connection remains unacknowledged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文