Java Reference
In-Depth Information
Figure 12-1. The structure of a UDP datagram
Although the theoretical maximum amount of data in a UDP datagram is 65,507 bytes,
in practice there is almost always much less. On many platforms, the actual limit is more
likely to be 8,192 bytes (8K). And implementations are not required to accept datagrams
with more than 576 total bytes, including data and headers. Consequently, you should
be extremely wary of any program that depends on sending or receiving UDP packets
with more than 8K of data. Most of the time, larger packets are simply truncated to 8K
of data. For maximum safety, the data portion of a UDP packet should be kept to 512
bytes or less, although this limit can negatively affect performance compared to larger
packet sizes. (This is a problem for TCP datagrams too, but the stream-based API pro‐
vided by Socket and ServerSocket completely shields programmers from these de‐
tails.)
In Java, a UDP datagram is represented by an instance of the DatagramPacket class:
public final class DatagramPacket extends Object
This class provides methods to get and set the source or destination address from the
IP header, to get and set the source or destination port, to get and set the data, and to
get and set the length of the data. The remaining header fields are inaccessible from
pure Java code.
 
Search WWH ::




Custom Search