Java Reference
In-Depth Information
ware is less forgiving, and most native UDP implementations don't support more than
8,192 bytes of data per datagram. The theoretical limit for an IPv4 datagram is 65,507
bytes of data, and a DatagramPacket with a 65,507-byte buffer can receive any possible
IPv4 datagram without losing data. IPv6 datagrams raise the theoretical limit to 65,536
bytes. In practice, however, many UDP-based protocols such as DNS and TFTP use
packets with 512 bytes of data per datagram or fewer. The largest data size in common
usage is 8,192 bytes for NFS. Almost all UDP datagrams you're likely to encounter will
have 8K of data or fewer. In fact, many operating systems don't support UDP datagrams
with more than 8K of data and either truncate, split, or discard larger datagrams. If a
large datagram is too big and as a result the network truncates or drops it, your Java
program won't be notified of the problem. Consequently, you shouldn't create Data
gramPacket objects with more than 8,192 bytes of data.
Constructors for sending datagrams
These four constructors create new DatagramPacket objects used to send data across
the network:
public DatagramPacket ( byte [] data , int length ,
InetAddress destination , int port )
public DatagramPacket ( byte [] data , int offset , int length ,
InetAddress destination , int port )
public DatagramPacket ( byte [] data , int length ,
SocketAddress destination )
public DatagramPacket ( byte [] data , int offset , int length ,
SocketAddress destination )
Each constructor creates a new DatagramPacket to be sent to another host. The packet
is filled with length bytes of the data array starting at offset or 0 if offset is not used.
If you try to construct a DatagramPacket with a length that is greater than data.length
(or greater than data.length - offset ), the constructor throws an IllegalArgumen
tException . It's OK to construct a DatagramPacket object with an offset and a length
that will leave extra, unused space at the end of the data array. In this case, only length
bytes of data will be sent over the network. The InetAddress or SocketAddress object
destination points to the host you want the packet delivered to; the int argument port
is the port on that host.
Choosing a Datagram Size
The correct amount of data to stuff into one packet depends on the situation. Some
protocols dictate the size of the packet. For example, rlogin transmits each character to
the remote system almost as soon as the user types it. Therefore, packets tend to be short:
a single byte of data, plus a few bytes of headers. Other applications aren't so picky. For
example, file transfer is more efficient with large buffers; the only requirement is that
you split files into packets no larger than the maximum allowable packet size.
 
Search WWH ::




Custom Search