Message too long for UDP socket after setting sendBufferSize()
I'm trying to send a UDP datagram (containing a protocol buffers message) and getting message too long exceptions:
java.io.IOException: Message too long
at java.net.PlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(DatagramSocket.java:625)
I've set the send buffer size, and checked the return value from getBufferSize(), and it's larger than the message:
byte[] b = msg.toByteArray();
System.out.println( "Serialised message in " + b.length + " bytes (max length: " + network.getSendBufferSize() + ")");
DatagramPacket p = new DatagramPacket( b, b.length, host, port );
开发者_如何转开发 network.send( p );
Outputs:
VM version: 16.3-b01-279
Runtime version: 1.6.0_20-b02-279-9M3165
Vendor: Apple Inc.
Serialised message in 69424 bytes (max length: 531075)
Problem sending packet: java.io.IOException: Message too long
I could understand if it was refusing to set a large sized buffer, but it seems to be setting whatever I ask, and then not honoring it.
This is on OSX; I've tried both with 1.6 and 1.5
The limit on a UDP datagram payload in IPv4 is 65535-28=65507 bytes, and the practical limit is the MTU of the path which is more like 1460 bytes if you're lucky.
When UDP is fragmented, it loses the datagram if a fragment is lost, because there is no retransmission.
Use TCP.
UDP datagrams can't be larger than 64K
UDP has a maximum limit a little bit short of 64K. Your message is over that limit.
Besides, you shouldn't use UDP at all for such large message. When UDP is fragmented, it has to retransmit the whole thing if one segment is lost. Use TCP.
精彩评论