Name
DatagramSocket
Synopsis
This class defines a socket that can
receive and send unreliable datagram packets over the network using
the UDP protocol. A
datagram
is a very low-level networking interface: it is simply an array of
bytes sent over the network. A datagram does not implement any kind
of stream-based communication protocol, and there is no connection
established between the sender and the receiver. Datagram packets are
called unreliable because the protocol does not make any attempt to
ensure they arrive or to resend them if they don’t.
Thus, packets sent through a DatagramSocket
are
not guaranteed to arrive in the order sent or even to arrive at all.
On the other hand, this low-overhead protocol makes datagram
transmission very fast. See Socket
and
URL
for higher-level interfaces to networking.
This class was introduced in Java 1.0, and was enhanced in Java 1.4
to allow local and remote addresses to be specified using the
protocol-independent SocketAddress
class.
send( )
sends a
DatagramPacket
through the socket. The packet must
contain the destination address to which it should be sent.
receive( )
waits for data to arrive at the socket
and stores it, along with the address of the sender, in the specified
DatagramPacket
. close( )
closes
the socket and frees the local port for reuse. Once close(
)
has been called, the DatagramSocket
should not be used again, except to call the isClosed(
)
method which returns true
if the
socket has been closed.
Each time a packet ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.