Java Reference
In-Depth Information
from a different sender. The datagram itself contains the sender's (client's) source
address and port.
Send echo reply: line 21
packet already contains the echo string and echo reply destination address and port,
so the send() method of DatagramSocket can simply transmit the datagram previously
received. Note that when we receive the datagram, we interpret the datagram address
and port as the source address and port, and when we send a datagram, we interpret
the datagram's address and port as the destination address and port.
Reset buffer size: line 22
The internal length of packet was set to the length of the message just processed,
which may have been smaller than the original buffer size. If we do not reset the
internal length before receiving again, the next message will be truncated if it is longer
than the one just received.
2.3.4 Sending and Receiving with UDP Sockets
A subtle but important difference between TCP and UDP is that UDP preserves message
boundaries. Each call to receive() returns data from at most one call to send() Moreover,
different calls to receive() will never return data from the same call to send() .
When a call to write() on a TCP socket's output stream returns, all the caller knows is
that the data has been copied into a buffer for transmission; the data may or may not have
actually been transmitted yet. (This is covered in more detail in Chapter 5.) UDP, however, does
not provide recovery from network errors and, therefore, does not buffer data for possible
retransmission. This means that by the time a call to send() returns, the message has been
passed to the underlying channel for transmission and is (or soon will be) on its way out the
door.
Between the time a message arrives from the network and the time its data is returned via
read() or receive() , the data is stored in a first-in, first-out (FIFO) queue of received data. With
a connected TCP socket, all received-but-not-yet-delivered bytes are treated as one continuous
sequence of bytes (see Chapter 5). For a UDP socket, however, the received data may have come
from different senders. A UDP socket's received data is kept in a queue of messages, each with
associated information identifying its source. A call to receive() will never return more than
one message. However, if receive() is called with a DatagramPacket containing a buffer of size
n , and the size of the first message in the receive queue exceeds n , only the first n bytes of
the message are returned. The remaining bytes are quietly discarded, with no indication to the
receiving program that information has been lost!
For this reason, a receiver should always supply a DatagramPacket with a buffer big enough
to hold the largest message allowed by its application protocol at the time it calls receive() .
This technique will guarantee that no data will be lost. The maximum amount of data that can
be transmitted in a DatagramPacket is 65,507 bytes—the largest payload that can be carried in
a UDP datagram. It is important to remember here that each instance of DatagramPacket has an
internal notion of message length that may be changed whenever a message is received into
Search WWH ::




Custom Search