在Java中,如何处理大于最大UDP数据负载的UDP消息?
在某些情况下,我的数据消息超过 1,400,000 字节,并且要求使用 UDP 协议。我无法更改协议(我更愿意使用 TCP 或基于 UDP 的可靠协议)。相反,我必须找到一种方法,通过 UDP 将大型有效负载从测试应用程序传输到我正在构建的系统中,并在我正在构建的系统中读取这些大型有效负载进行处理。我也不必担心丢失的数据包 - 如果我没有收到数据报,我也不在乎 - 只需等待下一个有效负载到达即可。如果它不完整或丢失,请将其全部扔掉并继续等待。我也不提前知道数据报的大小(范围从几百字节到 1,400,000+ 字节)。
我已经将发送和接收缓冲区大小设置得足够大,但这还不够。我还能做什么?
I read this question about the error that I'm getting and I learned that UDP data payloads can't be more than 64k. The suggestions that I've read are to use TCP, but that is not an option in this particular case. I am interfacing with an external system that is transmitting data over UDP, but I don't have access to that external system at this time, so I'm simulating it.
I have data messages that are upwards of 1,400,000 bytes in some instances and it's a requirement that the UDP protocol is used. I am not able to change protocols (I would much rather use TCP or a reliable protocol build on UDP). Instead, I have to find a way to transmit large payloads over UDP from a test application into the system that I am building and to read those large payloads in the system that I'm building for processing. I don't have to worry about dropped packets, either - if I don't get the datagram, I don't care - just wait for the next payload to arrive. If it's incomplete or missing, just throw it all away and continue waiting. I also don't know the size of the datagram in advance (they range of a few hundred bytes to 1,400,000+ bytes.
I've already set my send and receive buffer sizes large enough, but that's not sufficient. What else can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,该要求也应该决定打包技术。您需要有关外部系统及其协议的更多信息。请注意,最大 IPv4 UDP 有效负载为 65535-28 字节,最大实际有效负载一旦路由器介入,则为 1500 字节。
The requirement should also therefore dictate the packetization technique. You need more information about the external system and its protocol. Note that the maximum IPv4 UDP payload Is 65535-28 bytes, and the maximum practical payload is < 1500 bytes once a router gets involved.
UDP 数据包有一个 16 位长度的字段。与Java无关。它们不能更大,就这样。如果您正在交谈的服务器是不可变的,那么您就只能局限于可以装入数据包的内容。
如果您可以更改服务器并因此更改协议,您或多或少可以为自己重新实现 TCP。由于 UDP 被定义为不可靠,因此您需要完整的重传机制来应对网络中某处丢失的数据包。因此,您必须将“消息”拆分为块,发送块,并制定一个用于请求重传丢失块的协议。
UDP packets have a 16 bit length field. It's nothing to do with Java. They cannot be bigger, period. If the server you are talking to is immutable, you are stuck with what you can fit into a packet.
If you can change the server and thus the protocol, you can more or less reimplement TCP for yourself. Since UDP is defined to be unreliable, you need the full retransmission mechanism to cope with packets that are dropped in the network somewhere. So, you have to split the 'message' into chunks, send the chunks, and have a protocol for requesting retransmission of lost chunks.