简体   繁体   English

UDP根本不可靠吗?

[英]UDP is not reliable at all?

I have designed a system in which i used UDP to send broadcast messages to some clients connected to the same Access point. 我设计了一个系统,在该系统中,我使用UDP向与同一访问点连接的某些客户端发送广播消息。 I am using an Access point to connect the server and all other clients. 我正在使用访问点连接服务器和所有其他客户端。 The problem is with the broadcasting. 问题出在广播上。 When i broadcast say a message of 800 bytes to the clients, reception is totally random. 当我向客户端广播一条800字节的消息时,接收完全是随机的。 Sometimes clients are able to get the messages and sometimes not. 有时客户可以获取消息,有时则不能。 I tried to broadcast it multiple times so that at-least one will go through and reach all the clients. 我尝试过多次广播,以便至少通过一次广播并覆盖所有客户。 But even this is not working some times. 但是,即使这样有时也不起作用。 Why are packets getting dropped? 为什么丢包? Any problem with the size of packets ? 数据包大小有问题吗? How should i go about to make it reliable ? 我应该如何使其可靠? What factors may lead to this? 哪些因素可能导致这种情况? I have 40-50 clients connected to the AP. 我有40-50个客户端连接到AP。 The Access point is exclusively used for this Application and there is no Internet connection to it. 接入点专用于此应用程序,并且没有Internet连接。

UDP is inherently unreliable in the sense that: 在以下方面,UDP本质上是不可靠的:

  • UDP packets may be lost, and UDP数据包可能会丢失,并且
  • the UDP protocol provides no mechanism to tell if packets have been lost, or to resend them. UDP协议没有提供机制来判断数据包是否丢失或重新发送。

Why are packets getting dropped? 为什么丢包?

In general, there are a number of possible reasons: 通常,有多种可能的原因:

  • packets may be misrouted, 数据包可能路由错误,
  • packets may be "eaten" by a firewall, 数据包可能被防火墙“吞噬”,
  • packets may be dropped due to congestion in a gateway, 数据包可能由于网关拥塞而被丢弃,
  • packets may be dropped due to congestion at the end-point, or 数据包可能由于端点拥塞而被丢弃,或者
  • packets may be lost due to a network-level problem; 数据包可能由于网络级问题而丢失; eg a collision or a transmission error. 例如碰撞或传输错误。

Any problem with the size of packets? 数据包大小有问题吗?

Yes. 是。 If a UDP packet is too large, it may need to be fragmented (at the IP packet level). 如果UDP数据包太大,则可能需要分段(在IP数据包级别)。 If any of the fragments gets lost, then the receiver cannot reassemble the packet, and the entire UDP packet will be lost. 如果任何碎片丢失,则接收方无法重组数据包,并且整个UDP数据包都会丢失。 (There is no mechanism for retransmitting the lost fragments.) (没有重新传输丢失的碎片的机制。)

Reference: http://pcvr.nl/tcpip/udp_user.htm#11 . 参考: http : //pcvr.nl/tcpip/udp_user.htm#11

Hence, the probability of a large UDP packet getting lost is greater. 因此,大UDP数据包丢失的可能性更大。

Note that fragmentation occurs when the IP packet size exceeds the MTU of a link. 请注意,当IP数据包大小超过链接的MTU时,就会发生分段。 For an Ethernet link, the MTU is typically ~1500 octets or more. 对于以太网链路,MTU通常约为1500个八位位组或更多。 But the IPv4 spec allows the MTU to be as low as 576 octets. 但是IPv4规范允许MTU低至576个八位位组。 If you subtract the size of the IP and UDP headers, that gives a minimum UDP packet payload size of 534 octets before fragmentation is possible. 如果减去IP和UDP标头的大小,则在可能进行分段之前,UDP数据包的最小有效载荷大小为534个八位位组。

How should i go about to make it reliable ? 我应该如何使其可靠?

These things may help: 这些事情可能会有所帮助:

  • Pick a maximum UDP packet size that won't lead to fragmentation. 选择不会导致碎片的最大UDP数据包大小。
  • Implement your software to read the packets as quickly as possible ... to avoid the packets being dropped by the receiving OS. 实施您的软件以尽快读取数据包...避免接收操作系统丢弃数据包。
  • Avoid sending UDP packets through a (potentially) congested network link. 避免通过(可能)拥塞的网络链路发送UDP数据包。
  • Avoid sending too many packets in too short a time. 避免在太短的时间内发送太多的数据包。

But nothing will make UDP reliable in the sense above. 但是,从上述意义上讲,没有什么能使UDP可靠。 The protocol is inherently unreliable. 该协议本质上是不可靠的。 If you want reliability, either use TCP or implement your own reliability mechanisms at the application protocol level. 如果需要可靠性,请使用TCP或在应用程序协议级别实现自己的可靠性机制。 The former is probably the better approach. 前者可能是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM