接收UDP数据包
假设我的程序通过网络 (UDP) 发送 1000 字节。它是否保证接收方将“一批”接收 1000 个字节?或者他可能需要执行多次“读取”,直到收到完整的消息?如果后者为真,我如何确保同一消息的数据包顺序不会“混合”(按顺序),或者协议可能保证它?
编辑:也就是说,我的消息是否有可能被分割成多个数据包? (如果我尝试发送 10000mb 的消息,会发生什么?)
Let's say my program sends a 1000 bytes over the network (UDP). Does it guaranteed that the receiver will receive the 1000 bytes in one "batch"? Or perhaps he will need to perform sevral "reads" until he'll receive the entire message? if the later is true, how can i ensure that the order of the packets for the same message don't get "mixed up" (in order), or perhaps the protocol guarantees it?
Edit: that is, does it possible that my message will be split to sevral packets? (what if i try to send a 10000mb message, what happens then?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你要么得到全部,要么什么都得不到。
但并不能特别保证您将按照数据包传输的顺序恰好收到一次数据包;数据包丢失、重新排序和(不太常见)重复都是可能的。
有最大帧大小(65,507 字节),send()ing 较大大小的数据包将返回错误。
您必须提供足够的缓冲区才能在一次调用中接收整个帧。
UDP 数据包可以分段为多个 IP 片段,但操作系统会丢弃不完整的数据包。因此,这对于应用程序来说是透明的。
You will get it all or nothing.
But there is no particular guarantee that you will receive packets exactly once in the order they were transmitted; packet loss, reordering and (less often) duplication are all possible.
There is a maximum frame size (of 65,507 bytes), send()ing packets of larger sizes will return an error.
You must provide enough buffer to receive the entire frame in one call.
UDP packets CAN be fragmented into multiple IP fragments, but the OS will drop an incomplete packet. This is therefore transparent to the application.
接收方将在一次调用中获取整个数据包。即使在理论中,数据包长度也是有限的:
然而,真正的限制要低得多,通常可以安全地假设 512 字节。请参阅 最大的安全 UDP 数据包大小是多少互联网。
The receiver will get the entire packet in one call. The packet length is limited, even in theory:
However the real limit is much much lower, usually is safe to assume 512 bytes. See What is the largest Safe UDP Packet Size on the Internet.
与 TCP 不同,UDP 不是可靠的协议。它没有提供内置机制来确保数据包按正确的顺序到达,甚至完全到达。也就是说,您可以以锁步方式编写发送/接收例程,每次发送数据包时,发送方必须等待接收 ACK,然后才能再次发送。如果在指定的超时后未收到 ACK,则必须重新发送数据包。这样您就可以确保按正确的顺序接收数据包。 (有关详细信息,请查看 TFTP 协议的 RFC,它使用了此策略.)
最后,如果可能的话,您可能要考虑使用 TCP。
UDP, unlike TCP, is not a reliable protocol. It provides no built in mechanism to ensure that the packets arrive in the proper order, or even arrive at all. That said, you can write your send/recv routines in a lock step fashion, where every time a packet is sent, the sender must wait to receive an ACK before sending again. If an ACK is not received after some specified timeout, the packet must be resent. This way you ensure that packets are received in the proper order. (For more information, check out the RFC for the TFTP protocol, which uses this strategy.)
Finally, if possible, you may want to consider using TCP instead.
使用 UDP 发送的数据被分组在 数据包 中,因此如果您发送 x 字节数那么如果接收者收到数据包,他将收到 x 字节数。
但是,您的数据包甚至可能不会到达,或者到达的顺序可能不正确。
Data sent using UDP is grouped in packets, so if you send x amount of bytes then IF the receiver receives the packet he'll receive x amount of bytes.
However, your packets might not even arrive, or they may arrive out of order.
使用UDP Lite,您可以请求接收部分损坏的数据包。这对于视频和 VoIP 服务非常有用。
With UDP Lite you can request to receive partially corrupted packets. This can be useful for video and VoIP services.