DatagramChannel

public abstract class DatagramChannel extends AbstractSelectableChannel
implements ByteChannel ScatteringByteChannel GatheringByteChannel NetworkChannel

A selectable channel for datagram-oriented sockets.

A datagram channel is created by invoking one of the open methods of this class. It is not possible to create a channel for an arbitrary, pre-existing datagram socket. A newly-created datagram channel is open but not connected. A datagram channel need not be connected in order for the send and receive methods to be used. A datagram channel may be connected, by invoking its connect method, in order to avoid the overhead of the security checks are otherwise performed as part of every send and receive operation. A datagram channel must be connected in order to use the read and write methods, since those methods do not accept or return socket addresses.

Once connected, a datagram channel remains connected until it is disconnected or closed. Whether or not a datagram channel is connected may be determined by invoking its isConnected method.

Socket options are configured using the setOption method. A datagram channel to an Internet Protocol socket supports the following options:

Option Name Description
SO_SNDBUF The size of the socket send buffer
SO_RCVBUF The size of the socket receive buffer
SO_REUSEADDR Re-use address
SO_BROADCAST Allow transmission of broadcast datagrams
IP_TOS The Type of Service (ToS) octet in the Internet Protocol (IP) header
IP_MULTICAST_IF The network interface for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_TTL The time-to-live for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_LOOP Loopback for Internet Protocol (IP) multicast datagrams
Additional (implementation specific) options may also be supported.

Datagram channels are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time.

Protected Constructor Summary

DatagramChannel(SelectorProvider provider)
Initializes a new instance of this class.

Public Method Summary

abstract DatagramChannel
bind(SocketAddress local)
Binds the channel's socket to a local address.
abstract DatagramChannel
connect(SocketAddress remote)
Connects this channel's socket.
abstract DatagramChannel
disconnect()
Disconnects this channel's socket.
abstract SocketAddress
getRemoteAddress()
Returns the remote address to which this channel's socket is connected.
abstract boolean
isConnected()
Tells whether or not this channel's socket is connected.
static DatagramChannel
open()
Opens a datagram channel.
static DatagramChannel
open(ProtocolFamily family)
Opens a datagram channel.
abstract long
read(ByteBuffer[] dsts, int offset, int length)
Reads a datagram from this channel.
abstract int
read(ByteBuffer dst)
Reads a datagram from this channel.
final long
read(ByteBuffer[] dsts)
Reads a datagram from this channel.
abstract SocketAddress
receive(ByteBuffer dst)
Receives a datagram via this channel.
abstract int
send(ByteBuffer src, SocketAddress target)
Sends a datagram via this channel.
abstract <T> DatagramChannel
setOption(SocketOption<T> name, T value)
Sets the value of a socket option.
abstract DatagramSocket
socket()
Retrieves a datagram socket associated with this channel.
final int
validOps()
Returns an operation set identifying this channel's supported operations.
abstract int
write(ByteBuffer src)
Writes a datagram to this channel.
final long
write(ByteBuffer[] srcs)
Writes a datagram to this channel.
abstract long
write(ByteBuffer[] srcs, int offset, int length)
Writes a datagram to this channel.

Inherited Method Summary