简体   繁体   English

Java DatagramChannel数据丢失

[英]java datagramchannel data loss

I've read some conflicting things about how UDP/Java datagram channels operate. 我已经阅读了有关UDP / Java数据报通道如何工作的一些矛盾之处。 I need to know a few things: 我需要知道几件事:

  • Does UDP have an inherit way to tell if the packet that is received whole, and in order, before .read(ByteBuffer b) is called? 在调用.read(ByteBuffer b)之前,UDP是否具有继承方式来判断接收到的数据包是否完整? I've read in at least one article saying that UDP inherit'ly discards incomplete or out of order data. 我至少读过一篇文章,说UDP会继承丢弃不完整或乱序的数据。

  • Does datagramchannel treat one send(buffer.. ) as one datagram packet? 数据报通道是否将一个send(buffer ..)视为一个数据报包? what if its a partial send? 如果部分发送怎么办?

  • Can a .read(.. ) read more than one packet of data, resulting in data being discarded if the buffer being given as the commands argument was only designed to handle one packet of data? .read(..)是否可以读取多个数据包,如果将缓冲区作为commands参数指定仅用于处理一个数据包,从而导致数据被丢弃?

Does UDP have an [inherent] way to tell if the packet that is received whole, and in order, before .read(ByteBuffer b) is called? 在调用.read(ByteBuffer b)之前,UDP是否具有[固有的]方式来判断接收到的数据包是否按顺序完整接收? I've read in at least one article saying that UDP inherit'ly discards incomplete or out of order data. 我至少读过一篇文章,说UDP会继承丢弃不完整或乱序的数据。

Neither statement is correct. 两种说法都不正确。 It would be more accurate to say that IP has a way to tell if a datagram's fragments have all arrived, and then and only then does it even present it to UDP. 准确地说, IP可以判断数据报的片段是否全部到达,然后才将其呈现给UDP。 Reassembly is the responsibility of the IP layer, not UDP. 重组是IP层而不是UDP的责任。 If the fragments don't arrive, UDP never even sees it. 如果片段没有到达,UDP甚至都不会看到它。 If they expire before reassembly is complete, IP throws them away. 如果它们在重新组装完成之前过期,IP会将它们丢弃。

Before/after read() is called is irrelevant. read()之前/之后的调用无关紧要。

Does datagramchannel treat one send(buffer.. ) as one datagram packet? 数据报通道是否将一个send(buffer.. )视为一个数据报包?

Yes. 是。

what if it's a partial send? 如果是部分发送怎么办?

There is no such thing in UDP. UDP中没有这样的东西。

Can a read(.. ) read more than one packet of data read(.. )读取多个数据包吗

A UDP read will return exactly and only one datagram, or fail. UDP读取将仅返回一个数据报,否则将失败。

resulting in data being discarded if the buffer being given as the commands argument was only designed to handle one packet of data? 如果将缓冲区作为命令参数给定仅用于处理一个数据包,则会导致数据被丢弃?

Can't happen. 不会发生

Re your comment below, which is about a completely different question, the usual technique for detecting truncation is to use a buffer one larger than the largest expected datagram. 在下面的评论中,这是一个完全不同的问题,用于检测截断的常用技术是使用比最大预期数据报大一个的缓冲区。 Then if you ever get a datagram that size, (i) it's an application protocol error, and (ii) it may have been truncated too. 然后,如果您收到的数据报大小如此大,(i)这是应用程序协议错误,并且(ii)也可能已被截断。

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

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