windows UDP 数据包在套接字上停留多长时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7843644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How long does a UDP packet stay at a socket?
提问by T.T.T.
If data is sent to the client but the client is busy executing something else, how long will the data be available to read using recvfrom()?
如果数据已发送到客户端,但客户端正忙于执行其他操作,那么使用 recvfrom() 可以读取多长时间的数据?
Also, what happens if a second packet is sent before the first one is read, is the first one lost and the next one sitting there wating to be read?
此外,如果在读取第一个数据包之前发送第二个数据包,会发生什么情况,第一个数据包丢失,而下一个数据包坐在那里等待读取?
(windows - udp)
(windows - udp)
采纳答案by Jerry Coffin
Normally, the data will be buffered until it's read. I suppose if you wait long enough that the driver completely runs out of space, it'll have to do something, but assuming your code works halfway reasonably, that shouldn't be a problem.
通常,数据将被缓冲直到被读取。我想如果您等待的时间足够长以至于驱动程序完全耗尽空间,它就必须做一些事情,但假设您的代码正常工作到一半,那应该不是问题。
A typical network driver will be able to buffer a number of packets without losing any.
典型的网络驱动程序将能够缓冲大量数据包而不会丢失任何数据包。
回答by Damon
If data is sent to the client but the client is busy executing something else, how long will the data be available to read using
recvfrom()
?
如果数据已发送到客户端,但客户端正忙于执行其他操作,那么使用 可以读取数据多长时间
recvfrom()
?
Forever, or not at all, or until you close the socket or read as much as a single byte.
永远,或者根本不,或者直到您关闭套接字或读取多达单个字节。
The reason for that is:
UDP delivers datagrams, or it doesn't. This sounds like nonsense, but it is exactly what it is.
原因是:
UDP 传送数据报,或者不传送。这听起来像是无稽之谈,但事实就是如此。
A single UDP datagram relates to either exactly one or several "fragments", which are IP packets (further encapsulated in some "on the wire" protocol, but that doesn't matter). The network stack collects all fragments for a datagram. If the checksum on any of the fragments is not good, or any other thing that makes the network stack unhappy, the complete datagram is discarded, and you get nothing, not even an error. You simply don't know anything happened.
单个 UDP 数据报与一个或多个“片段”相关,这些“片段”是 IP 数据包(进一步封装在某些“在线”协议中,但这无关紧要)。网络堆栈收集数据报的所有片段。如果任何片段的校验和不好,或任何其他使网络堆栈不满意的事情,则整个数据报将被丢弃,您将一无所获,甚至不会出现错误。你根本不知道发生了什么。
If all goes well, a complete datagramis placed into the receive buffer. Never anything less, and never anything more. If you try to recvfrom
later, that is what you'll get.
如果一切顺利,一个完整的数据报就会被放入接收缓冲区。绝不会少,也绝不会多。如果您稍后尝试recvfrom
,这就是您将得到的。
The receive buffer is obviously necessarily large enough to hold at least one max-size datagram (65535 bytes), but since usually datagrams will not be maximum size, but rather something below 1280 bytes (or 1500 if you will), it can usually hold quite a few of them (on most platforms, the buffer defaults to something around 128-256k, and is configurable).
If there is not enough room left in the buffer, the datagram is discarded, and you get nothing(well, you dostill get the ones that are already in the buffer). Again, you don't even know something happened.
接收缓冲区显然必须足够大以容纳至少一个最大大小的数据报(65535 字节),但由于通常数据报不会是最大大小,而是小于 1280 字节(或 1500,如果你愿意),它通常可以容纳其中不少(在大多数平台上,缓冲区默认为 128-256k 左右,并且是可配置的)。
如果没有足够的空间留在缓冲区,数据包将被丢弃,你会得到什么(当然,你也仍然可以说是已经在缓冲区中的)。再说一次,你甚至不知道发生了什么事。
Each time you call recvfrom
, a complete datagramis removed from the buffer (important detail!), and you get up to the number of bytes that you requested. Which means if you naively try read a few bytes and then a few bytes again, it just won't work. The first read will discard the rest of the datagram, and the subsequent ones read the first bytes of some future datagrams (and possibly block)!
每次调用 时recvfrom
,都会从缓冲区中删除一个完整的数据报(重要的细节!),并且您会得到所需的字节数。这意味着如果您天真地尝试读取几个字节然后再次读取几个字节,它就行不通。第一次读取将丢弃数据报的其余部分,随后的读取将读取一些未来数据报的第一个字节(可能会阻塞)!
This is very differentfrom how TCP works. Here you can actually read a few bytes and a few bytes again, and it will just work, because the network layer simulates a data stream. You give a crap howit works, because the network stack makes sure it works.
这与 TCP 的工作方式非常不同。在这里你实际上可以读取几个字节,然后再读取几个字节,它就会起作用,因为网络层模拟了一个数据流。你给一个废话如何它的工作原理,因为网络堆栈可以确保它的工作原理。
Also, what happens if a second packet is sent before the first one is read, is the first one lost and the next one sitting there waiting to be read?
另外,如果在读取第一个数据包之前发送第二个数据包,会发生什么情况,第一个数据包丢失并且下一个等待读取?
You probably meant to say "received" rather than "sent". Send and receive have different buffers, so that would not matter at all. About receiving another packet while one is still in the buffer, see the above explanation. If the buffer can hold the second datagram, it will store it, otherwise it silently goes * poof *.
This does not affect any datagrams already in the buffer.
您可能想说“收到”而不是“发送”。发送和接收有不同的缓冲区,所以这根本无关紧要。关于在缓冲区中接收另一个数据包时,请参阅上面的说明。如果缓冲区可以容纳第二个数据报,它将存储它,否则它会默默地 * poof *。
这不会影响缓冲区中已有的任何数据报。
回答by 8bitwide
If data is sent to the client but the client is busy executing something else, how long will the data be available to read using recvfrom()?
如果数据已发送到客户端,但客户端正忙于执行其他操作,那么使用 recvfrom() 可以读取多长时间的数据?
This depends on the OS, in windows, I believe the default for each UDP socket is 8012, this can be raised with setsockopt() Winsock DocumentationSo, as long as the buffer isn't full, the data will stay there until the socket is closed or it is read.
这取决于操作系统,在 Windows 中,我相信每个 UDP 套接字的默认值是 8012,这可以通过 setsockopt() 提高Winsock 文档因此,只要缓冲区未满,数据就会保留在那里直到套接字已关闭或已读取。
Also, what happens if a second packet is sent before the first one is read, is the first one lost and the next one sitting there wating to be read?
此外,如果在读取第一个数据包之前发送第二个数据包,会发生什么情况,第一个数据包丢失,而下一个数据包坐在那里等待读取?
If the buffer has room, they are both stored, if not, one of them gets discarded. I believe its the newest one but I'm not 100% Sure.
如果缓冲区有空间,则它们都被存储,如果没有,则其中一个被丢弃。我相信它是最新的,但我不是 100% 确定。