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 checks 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:
Additional (implementation specific) options may also be supported.
Option Name Description SO_SNDBUFThe size of the socket send buffer SO_RCVBUFThe size of the socket receive buffer SO_REUSEADDRRe-use address SO_BROADCASTAllow transmission of broadcast datagrams IP_TOSThe Type of Service (ToS) octet in the Internet Protocol (IP) header IP_MULTICAST_IFThe network interface for Internet Protocol (IP) multicast datagrams IP_MULTICAST_TTLThe time-to-live for Internet Protocol (IP) multicast datagrams IP_MULTICAST_LOOPLoopback for Internet Protocol (IP) multicast datagrams
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.
| Access | Constructor and Description |
|---|---|
| protected | DatagramChannel(SelectorProvider
The provider that created this channel provider)Initializes a new instance of this class. |
| Modifier and Type | Method and Description |
|---|---|
| public abstract DatagramChannel | bind(SocketAddress local)
Redeclares java. Binds the channel's socket to a local address. |
| public abstract DatagramChannel | Returns: This datagram channelThe remote address to which this channel is to be connected remote)Connects this channel's socket. |
| public abstract DatagramChannel | |
| public abstract SocketAddress | Returns: TheSocketAddress that the socket is bound to; null
if the channel's socket is not boundRedeclares java. Returns the socket address that this channel's socket is bound to. |
| public abstract SocketAddress | Returns: The remote address;null if the channel's socket is not
connectedReturns the remote address to which this channel's socket is connected. |
| public abstract boolean | |
| public static DatagramChannel | |
| public static DatagramChannel | Returns: A new datagram channelThe protocol family family)Opens a datagram channel. |
| public abstract int | read(ByteBuffer dst)
Redeclares java. Reads a datagram from this channel. |
| public abstract long | read(ByteBuffer[] dsts, int offset, int length)
Redeclares java. Reads a datagram from this channel. |
| public final long | read(ByteBuffer[] dsts)
Implements java. Reads a datagram from this channel. |
| public abstract SocketAddress | Returns: The datagram's source address, ornull if this channel is in non-blocking mode
and no datagram was immediately availableThe buffer into which the datagram is to be transferred dst)Receives a datagram via this channel. |
| public abstract int | Returns: The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output bufferThe buffer containing the datagram to be sent src, SocketAddress The address to which the datagram is to be sent target)Sends a datagram via this channel. |
| public abstract <T> DatagramChannel | setOption(SocketOption<T> name, T value)
Redeclares java. Sets the value of a socket option. |
| public abstract DatagramSocket | Returns: A datagram socket associated with this channelRetrieves a datagram socket associated with this channel. |
| public final int | Returns: The valid-operation setImplements abstract java. Returns an operation set identifying this channel's supported operations. |
| public abstract int | write(ByteBuffer src)
Redeclares java. Writes a datagram to this channel. |
| public abstract long | Returns: The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output bufferRedeclares java. Writes a datagram to this channel. |
| public final long | Returns: The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output bufferImplements java. Writes a datagram to this channel. |
| DatagramChannel | back to summary |
|---|---|
| protected DatagramChannel(SelectorProvider provider) Initializes a new instance of this class.
| |
| bind | back to summary |
|---|---|
| public abstract DatagramChannel bind(SocketAddress local) throws IOException Redeclares java. Doc from java. Binds the channel's socket to a local address. This method is used to establish an association between the socket and
a local address. Once an association is established then the socket remains
bound until the channel is closed. If the
| |
| connect | back to summary |
|---|---|
| public abstract DatagramChannel connect(SocketAddress remote) throws IOException Connects this channel's socket. The channel's socket is configured so that it only receives datagrams from, and sends datagrams to, the given remote peer address. Once connected, datagrams may not be received from or sent to any other address. Datagrams in the channel's socket receive buffer, which have not been received before invoking this method, may be discarded. The channel's socket remains connected until it is explicitly disconnected or until it is closed. This method may be invoked at any time. If another thread has
already initiated a read or write operation upon this channel, then an
invocation of this method will block until any such operation is
complete. If this channel's socket is not bound then this method will
first cause the socket to be bound to an address that is assigned
automatically, as if invoking the
| |
| disconnect | back to summary |
|---|---|
| public abstract DatagramChannel disconnect() throws IOException Disconnects this channel's socket. The channel's socket is configured so that it can receive datagrams from, and sends datagrams to, any remote address. This method may be invoked at any time. If another thread has already initiated a read or write operation upon this channel, then an invocation of this method will block until any such operation is complete. If this channel's socket is not connected, or if the channel is closed, then invoking this method has no effect. API Note If this method throws an IOException, the channel's socket may be left in an unspecified state. It is strongly recommended that the channel be closed when disconnect fails.
| |
| getLocalAddress | back to summary |
|---|---|
| public abstract SocketAddress getLocalAddress() throws IOException Redeclares java. Doc from java. Returns the socket address that this channel's socket is bound to. Where the channel is
If the channel's socket was initially bound to the wildcard address and
is now
| |
| getRemoteAddress | back to summary |
|---|---|
| public abstract SocketAddress getRemoteAddress() throws IOException Returns the remote address to which this channel's socket is connected.
| |
| isConnected | back to summary |
|---|---|
| public abstract boolean isConnected() Tells whether or not this channel's socket is connected.
| |
| open | back to summary |
|---|---|
| public static DatagramChannel open() throws IOException Opens a datagram channel. The new channel is created by invoking the The
| |
| open | back to summary |
|---|---|
| public static DatagramChannel open(ProtocolFamily family) throws IOException Opens a datagram channel. The The new channel is created by invoking the API Note Unix domain sockets are not supported.
| |
| read | back to summary |
|---|---|
| public abstract int read(ByteBuffer dst) throws IOException Redeclares java. Reads a datagram from this channel. This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffer
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the
| |
| read | back to summary |
|---|---|
| public abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException Redeclares java. Reads a datagram from this channel. This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the
| |
| read | back to summary |
|---|---|
| public final long read(ByteBuffer[] dsts) throws IOException Implements java. Reads a datagram from this channel. This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the
| |
| receive | back to summary |
|---|---|
| public abstract SocketAddress receive(ByteBuffer dst) throws IOException Receives a datagram via this channel. If a datagram is immediately available, or if this channel is in
blocking mode and one eventually becomes available, then the datagram is
copied into the given byte buffer and its source address is returned.
If this channel is in non-blocking mode and a datagram is not
immediately available then this method immediately returns
The datagram is transferred into the given byte buffer starting at
its current position, as if by a regular This method may be invoked at any time. If another thread has
already initiated a read operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete. If this channel's socket is not bound then this method will
first cause the socket to be bound to an address that is assigned
automatically, as if invoking the
| |
| send | back to summary |
|---|---|
| public abstract int send(ByteBuffer src, SocketAddress target) throws IOException Sends a datagram via this channel. If this channel is in non-blocking mode and there is sufficient room in the underlying output buffer, or if this channel is in blocking mode and sufficient room becomes available, then the remaining bytes in the given buffer are transmitted as a single datagram to the given target address. The datagram is transferred from the byte buffer as if by a regular
This method may be invoked at any time. If another thread has
already initiated a write operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete. If this channel's socket is not bound then this method will
first cause the socket to be bound to an address that is assigned
automatically, as if by invoking the
| |
| setOption | back to summary |
|---|---|
| public abstract <T> DatagramChannel setOption(SocketOption<T> name, T value) throws IOException Redeclares java. Doc from java. Sets the value of a socket option.
| |
| socket | back to summary |
|---|---|
| public abstract DatagramSocket socket() Retrieves a datagram socket associated with this channel.
| |
| validOps | back to summary |
|---|---|
| public final int validOps() Implements abstract java. Returns an operation set identifying this channel's supported operations. Datagram channels support reading and writing, so this method
returns
| |
| write | back to summary |
|---|---|
| public abstract int write(ByteBuffer src) throws IOException Redeclares java. Writes a datagram to this channel. This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the
| |
| write | back to summary |
|---|---|
| public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException Redeclares java. Writes a datagram to this channel. This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the
| |
| write | back to summary |
|---|---|
| public final long write(ByteBuffer[] srcs) throws IOException Implements java. Writes a datagram to this channel. This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the
| |