简体   繁体   中英

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. 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. 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. The Access point is exclusively used for this Application and there is no Internet connection to it.

UDP is inherently unreliable in the sense that:

  • UDP packets may be lost, and
  • the UDP protocol provides no mechanism to tell if packets have been lost, or to resend them.

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). If any of the fragments gets lost, then the receiver cannot reassemble the packet, and the entire UDP packet will be lost. (There is no mechanism for retransmitting the lost fragments.)

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

Hence, the probability of a large UDP packet getting lost is greater.

Note that fragmentation occurs when the IP packet size exceeds the MTU of a link. For an Ethernet link, the MTU is typically ~1500 octets or more. But the IPv4 spec allows the MTU to be as low as 576 octets. 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.

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.
  • 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.
  • Avoid sending too many packets in too short a time.

But nothing will make UDP reliable in the sense above. The protocol is inherently unreliable. If you want reliability, either use TCP or implement your own reliability mechanisms at the application protocol level. The former is probably the better approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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