DatagramPacket - Java 是否首先对其进行分段和重新排列?

DatagramPacket - does Java frag and rearrange it first?

我是整个 UDP 的新手(因为每个人都喜欢 TCP),需要问几个关于 Java 实现的问题。

我需要有人告诉我是否:

  1. Java发送的DatagramPackets由于以下原因自动分片 网络配置和数据大小。
  2. 由于网络配置和数据大小而自动分段后,DatagramPackets 被 Java 重新排列为正确的分段顺序...在 receive() 调用之前 returns 结果.
  3. 如果不完整的碎片化 DatagramPackets 被丢弃或在丢弃时产生异常。 (部分片段收到,其他片段丢失)

我担心 Java 会静静地丢弃它,或者数据排列不正确...这意味着我必须实现伪 TCP 之类的东西才能同时拥有 UDP 的优点,以及TCP的检查。

UDP 主要在 OS 中实现,Java 在这方面几乎没有发言权。

  • 长度超过 576 字节的数据包可以分片;
  • 数据包可能会丢失;
  • 数据包可能会乱序到达。

没有办法Java,或者你来判断这些是否发生过。

你可以做的是实施一个协议来检测这个。例如在每个数据包的开头添加序列号、长度和校验和。

which would mean that I have to implement a pseudo TCP kind of thing to have both the benefits of UDP, as well as the checking of TCP.

现在您开始理解为什么 "everyone loves TCP" 或大多数人会这样做。 UDP 有其用途,但对于大多数应用程序,TCP 是最简单的。

The DatagramPackets sent by Java are fragmented automatically due to network configurations and data size.

是的,但是Java与它无关。

The DatagramPackets are rearranged to be in the correct frag sequential order by Java after being fragmented automatically due to network configurations and data size... before the receive() call returns the result.

是的,但不是 Java,并且只有当所有片段都到达时。这发生在 IP 层。

If fragmented DatagramPackets that're incomplete are dropped or generate Exceptions when dropped. (Some fragments received, others lost)

它们被无声地丢弃。没有例外。同样 Java 与它无关。这一切都发生在 IP 层。当且仅当所有片段都到达时,数据报才重新组装并向上传递到 UDP 层。 Java 没有任何参与。

I'm concerned that Java drops it silently

Java 什么都不做。 IP 静静地丢弃它。

or data is not arranged correctly

数据报要么完整完整地接收,要么根本没有接收到。同样 Java 与它无关。

which would mean that I have to implement a pseudo TCP kind of thing to have both the benefits of UDP, as well as the checking of TCP.

正确。你会的。