The documentation for the receive() method of DatagramSocket says that the received message is truncated if the datagram packet buffer is not big enough to hold it.

Tim Rohaly

The receive() method will silently discard any extra data if the supplied byte array is not large enough to hold it. This discarding behavior is the same as in BSD sockets. However, at the Java API level, there is no way to tell that this data has been discarded because the original packet length is only available in native code, and not propagated up to the Java implementation classes.

The easiest way of dealing with this issue is to avoid it entirely. A UDP packet can contain at most 65507 bytes (this is the 65535-byte IP packet size minus the minimum IP header of 20 bytes and minus the 8-byte UDP header). If you receive your datagrams into an array of at least this length, truncation will never happen.

A more efficient way to do this would be to use a protocol-specific buffer size based on the maximum packet size for that protocol. For instance, Dynamic Host Configuration Protocol messages are at most 548 bytes long, so your DHCP packet receiving code can safely limit the byte array to that size.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact