问题描述
假设我的程序通过网络 (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?)
推荐答案
你会得到它,或者什么都没有.
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.
存在最大帧大小(65,507 字节),发送()更大尺寸的数据包将返回错误.
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 数据包可以分成多个 IP 片段,但操作系统会丢弃一个不完整的数据包.因此,这对应用程序是透明的.
UDP packets CAN be fragmented into multiple IP fragments, but the OS will drop an incomplete packet. This is therefore transparent to the application.
这篇关于以 UDP 接收数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!