The message is too long for a UDP socket after setting sendBufferSize ()

I am trying to send a UDP datagram (containing a protocol buffer message) and receiving too long messages:

java.io.IOException: Message too long at java.net.PlainDatagramSocketImpl.send(Native Method) at java.net.DatagramSocket.send(DatagramSocket.java:625) 

I set the size of the send buffer and checked the return value from getBufferSize (), and it is 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 he refused to install a large buffer, but it seems that he tunes everything that I ask, and then does not comply with it.

This is on OSX; I tried both with 1.6 and 1.5

+4
source share
3 answers

UDP datagrams cannot exceed 64K

+2
source

(a) The payload limit of the UDP datagram in IPv4 is 65535-28 = 65507 bytes, and the practical limitation is the path MTU, which is more like 1460 bytes if you are lucky.

(b) When UDP is fragmented, it loses the datagram if the fragment is lost because there is no retransmission.

Use TCP.

+9
source

UDP has a maximum limit of just over 64K. Your message has exceeded this limit.

In addition, you should not use UDP at all for such a large message. When UDP is fragmented, it must retransmit it all if one segment is lost. Use TCP.

+1
source

All Articles