Java DatagramPacket(UDP)最大发送/ recv缓冲区大小 [英] Java DatagramPacket (UDP) maximum send/recv buffer size

查看:2094
本文介绍了Java DatagramPacket(UDP)最大发送/ recv缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 时使用Java DatagramPacket 假设您有一个字节[1024 * 1024] 缓冲区。如果你只是在发送/接收时传递给DatagramPacket,那么Java接收对DatagramPacket块的调用,直到它读取整个兆字节为止?

In Java when using DatagramPacket suppose you have a byte[1024*1024] buffer. If you just pass that for the DatagramPacket when sending/receiving will a Java receive call for the DatagramPacket block until it has read the entire megabyte?

我问是否Java将它拆分或者只是尝试发送丢弃的整个东西。

I'm asking if Java will split it up or just try to send the entire thing which gets dropped.

正常情况下,UDP数据包的大小限制大约是64KB,但我想知道,因为Java的API允许对于字节数组,如果这是一个限制,超级巨大的东西被删除或拆分并重新组装给你。

Normally the size limit is around 64KB for a UDP packet, but I wondered since Java's API allow for byte arrays if that is a limit and something super huge is dropped or split up and reassembled for you.

如果它被删除,API调用会告诉我最大数据我可以在Java调用中使用的有效负载?我听说IPv6也有巨型帧,但是DatagramPacket(或 DatagramSocket )支持,因为UDP定义了标题规范?

If it is dropped what API call would tell me the maximum data payload I can use in the Java call? I've heard that IPv6 also has jumbo frames, but does DatagramPacket (or DatagramSocket) support that since UDP defines the header spec?

推荐答案

DatagramPacket 只是基于UDP的套接字的包装器,因此适用通常的UDP规则。

DatagramPacket is just a wrapper on a UDP based socket, so the usual UDP rules apply.

64千字节是完整IP数据报的理论最大大小,但保证仅路由576个字节。在任何给定的网络路径上,具有最小最大传输单元的链路将确定实际限制。 (1500字节,更少的标头是常见的最大值,但是不可能预测将有多少标头,因此最安全地将消息限制在大约1400字节。)

64 kilobytes is the theoretical maximum size of a complete IP datagram, but only 576 bytes are guaranteed to be routed. On any given network path, the link with the smallest Maximum Transmit Unit will determine the actual limit. (1500 bytes, less headers is the common maximum, but it is impossible to predict how many headers there will be so its safest to limit messages to around 1400 bytes.)

如果超过MTU限制,IPv4将自动将数据报分解为片段并在最后重组它们,但最多只能达到64千字节,并且只有当所有片段都通过时才会重新组装。如果任何片段丢失,或者任何设备决定它不喜欢片段,那么整个数据包都会丢失。

If you go over the MTU limit, IPv4 will automatically break the datagram up into fragments and reassemble them at the end, but only up to 64 kilobytes and only if all fragments make it through. If any fragment is lost, or if any device decides it doesn't like fragments, then the entire packet is lost.

如上所述,不可能知道推进路径的MTU将是什么。有各种算法可供试验以找出,但许多设备没有正确实施(或故意忽略)必要的标准,所以这一切都归结为试验和错误。或者你可以猜测每条消息1400字节。

As noted above, it is impossible to know in advance what the MTU of path will be. There are various algorithms for experimenting to find out, but many devices do not properly implement (or deliberately ignore) the necessary standards so it all comes down to trial and error. Or you can just guess 1400 bytes per message.

至于错误,如果你尝试发送的字节数多于操作系统配置允许的字节数,你应该得到一个 EMSGSIZE 错误或其等价物。如果您发送的数量少于网络允许的数量,那么数据包将会消失。

As for errors, if you try to send more bytes than the OS is configured to allow, you should get an EMSGSIZE error or its equivalent. If you send less than that but more than the network allows, the packet will just disappear.

这篇关于Java DatagramPacket(UDP)最大发送/ recv缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆