APIdia Launch Partner
Top Description Constructors Methods
java.nio.channels

public abstract Class DatagramChannel

extends AbstractSelectableChannel
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, MulticastChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.MulticastChannel, java.nio.channels.NetworkChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.GatheringByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.ScatteringByteChannel, java.nio.channels.ReadableByteChannel, java.nio.channels.ByteChannel
Known Direct Subclasses
sun.nio.ch.DatagramChannelImpl
Imports
java.io.IOException, java.net.ProtocolFamily, .DatagramSocket, .SocketOption, .SocketAddress, java.nio.ByteBuffer, java.nio.channels.spi.AbstractSelectableChannel, .SelectorProvider

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 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:

Socket 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.

Authors
Mark Reinhold, JSR-51 Expert Group
Since
1.4

Constructor Summary

AccessConstructor and Description
protected
DatagramChannel(SelectorProvider
The provider that created this channel
provider
)

Initializes a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
public abstract DatagramChannel
bind(SocketAddress local)

Redeclares java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address.

public abstract DatagramChannel

Returns:

This datagram channel
connect
(SocketAddress
The remote address to which this channel is to be connected
remote
)

Connects this channel's socket.

public abstract DatagramChannel

Returns:

This datagram channel
disconnect
()

Disconnects this channel's socket.

public abstract SocketAddress

Returns:

The SocketAddress that the socket is bound to; null if the channel's socket is not bound
getLocalAddress
()

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

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 connected
getRemoteAddress
()

Returns the remote address to which this channel's socket is connected.

public abstract boolean

Returns:

true if, and only if, this channel's socket is open and connected
isConnected
()

Tells whether or not this channel's socket is connected.

public static DatagramChannel

Returns:

A new datagram channel
open
()

Opens a datagram channel.

public static DatagramChannel

Returns:

A new datagram channel
open
(ProtocolFamily
The protocol family
family
)

Opens a datagram channel.

public abstract int
read(ByteBuffer dst)

Redeclares java.nio.channels.ReadableByteChannel.read.

Reads a datagram from this channel.

public abstract long
read(ByteBuffer[] dsts, int offset, int length)

Redeclares java.nio.channels.ScatteringByteChannel.read.

Reads a datagram from this channel.

public final long
read(ByteBuffer[] dsts)

Implements java.nio.channels.ScatteringByteChannel.read.

Reads a datagram from this channel.

public abstract SocketAddress

Returns:

The datagram's source address, or null if this channel is in non-blocking mode and no datagram was immediately available
receive
(ByteBuffer
The 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 buffer
send
(ByteBuffer
The 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.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

public abstract DatagramSocket

Returns:

A datagram socket associated with this channel
socket
()

Retrieves a datagram socket associated with this channel.

public final int

Returns:

The valid-operation set
validOps
()

Implements abstract java.nio.channels.SelectableChannel.validOps.

Returns an operation set identifying this channel's supported operations.

public abstract int
write(ByteBuffer src)

Redeclares java.nio.channels.WritableByteChannel.write.

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 buffer
write
(ByteBuffer[] srcs, int offset, int length)

Redeclares java.nio.channels.GatheringByteChannel.write.

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 buffer
write
(ByteBuffer[] srcs)

Implements java.nio.channels.GatheringByteChannel.write.

Writes a datagram to this channel.

Inherited from java.nio.channels.spi.AbstractSelectableChannel:
blockingLockconfigureBlockingimplCloseChannelimplCloseSelectableChannelimplConfigureBlockingisBlockingisRegisteredkeyForproviderregister

Constructor Detail

DatagramChannelback to summary
protected DatagramChannel(SelectorProvider provider)

Initializes a new instance of this class.

Parameters
provider:SelectorProvider

The provider that created this channel

Method Detail

bindback to summary
public abstract DatagramChannel bind(SocketAddress local) throws IOException

Redeclares java.nio.channels.NetworkChannel.bind.

Doc from java.nio.channels.NetworkChannel.bind.

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 local parameter has the value null then the socket will be bound to an address that is assigned automatically.

Exceptions
IOException:
If some other I/O error occurs
AlreadyBoundException:
If the socket is already bound
UnsupportedAddressTypeException:
If the type of the given address is not supported
ClosedChannelException:
If the channel is closed
Since
1.7
connectback 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 bind method with a parameter of null.

Parameters
remote:SocketAddress

The remote address to which this channel is to be connected

Returns:DatagramChannel

This datagram channel

Exceptions
IOException:
If some other I/O error occurs
AlreadyConnectedException:
If this channel is already connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the connect operation is in progress, thereby closing the channel and setting the current thread's interrupted status
UnresolvedAddressException:
If the given remote address is not fully resolved
UnsupportedAddressTypeException:
If the type of the given remote address is not supported
disconnectback 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.

Returns:DatagramChannel

This datagram channel

Exceptions
IOException:
If some other I/O error occurs
getLocalAddressback to summary
public abstract SocketAddress getLocalAddress() throws IOException

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

Doc from java.nio.channels.NetworkChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.

Where the channel is bound to an Internet Protocol socket address then the return value from this method is of type java.net.InetSocketAddress.

If the channel's socket was initially bound to the wildcard address and is now connected, then the address returned may be the local address selected as the source address for datagrams sent via this channel instead of the wildcard address. When disconnect is called, the bound address reverts to the wildcard address.

Returns:SocketAddress

The SocketAddress that the socket is bound to; null if the channel's socket is not bound

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
ClosedChannelException:
If the channel is closed
getRemoteAddressback to summary
public abstract SocketAddress getRemoteAddress() throws IOException

Returns the remote address to which this channel's socket is connected.

Returns:SocketAddress

The remote address; null if the channel's socket is not connected

Exceptions
IOException:
If an I/O error occurs
ClosedChannelException:
If the channel is closed
Since
1.7
isConnectedback to summary
public abstract boolean isConnected()

Tells whether or not this channel's socket is connected.

Returns:boolean

true if, and only if, this channel's socket is open and connected

openback to summary
public static DatagramChannel open() throws IOException

Opens a datagram channel.

The new channel is created by invoking the openDatagramChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object. The channel will not be connected.

The ProtocolFamily of the channel's socket is platform (and possibly configuration) dependent and therefore unspecified. The open allows the protocol family to be selected when opening a datagram channel, and should be used to open datagram channels that are intended for Internet Protocol multicasting.

Returns:DatagramChannel

A new datagram channel

Exceptions
IOException:
If an I/O error occurs
See Also
java.net.preferIPv4Stack system property
openback to summary
public static DatagramChannel open(ProtocolFamily family) throws IOException

Opens a datagram channel.

The family parameter is used to specify the ProtocolFamily. If the datagram channel is to be used for IP multicasting then this should correspond to the address type of the multicast groups that this channel will join.

The new channel is created by invoking the openDatagramChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object. The channel will not be connected.

API Note

Unix domain sockets are not supported.

Parameters
family:ProtocolFamily

The protocol family

Returns:DatagramChannel

A new datagram channel

Exceptions
IOException:
If an I/O error occurs
UnsupportedOperationException:
If the specified protocol family is not supported. For example, suppose the parameter is specified as StandardProtocolFamily.INET6 but IPv6 is not enabled on the platform.
Since
1.7
See Also
java.net.preferIPv4Stack system property
readback to summary
public abstract int read(ByteBuffer dst) throws IOException

Redeclares java.nio.channels.ReadableByteChannel.read.

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 ReadableByteChannel interface.

Exceptions
IOException:

Doc from java.nio.channels.ReadableByteChannel.read.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupted status
readback to summary
public abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException

Redeclares java.nio.channels.ScatteringByteChannel.read.

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 ScatteringByteChannel interface.

Exceptions
IOException:

Doc from java.nio.channels.ScatteringByteChannel.read.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupted status
readback to summary
public final long read(ByteBuffer[] dsts) throws IOException

Implements java.nio.channels.ScatteringByteChannel.read.

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 ScatteringByteChannel interface.

Exceptions
IOException:

Doc from java.nio.channels.ScatteringByteChannel.read.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupted status
receiveback 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 null.

The datagram is transferred into the given byte buffer starting at its current position, as if by a regular read operation. If there are fewer bytes remaining in the buffer than are required to hold the datagram then the remainder of the datagram is silently discarded.

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 bind method with a parameter of null.

Parameters
dst:ByteBuffer

The buffer into which the datagram is to be transferred

Returns:SocketAddress

The datagram's source address, or null if this channel is in non-blocking mode and no datagram was immediately available

Exceptions
IOException:
If some other I/O error occurs
IllegalArgumentException:
If the buffer is read-only
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupted status
sendback 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 write operation.

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 bind method with a parameter of null.

Parameters
src:ByteBuffer

The buffer containing the datagram to be sent

target:SocketAddress

The address to which the datagram is to be sent

Returns:int

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 buffer

Exceptions
IOException:
If some other I/O error occurs
AlreadyConnectedException:
If this channel is connected to a different address from that specified by target
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the read operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupted status
UnresolvedAddressException:
If the given remote address is not fully resolved
UnsupportedAddressTypeException:
If the type of the given remote address is not supported
setOptionback to summary
public abstract <T> DatagramChannel setOption(SocketOption<T> name, T value) throws IOException

Redeclares java.nio.channels.NetworkChannel.setOption.

Doc from java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

Exceptions
IOException:
If an I/O error occurs
UnsupportedOperationException:
If the socket option is not supported by this channel
IllegalArgumentException:
If the value is not a valid value for this socket option
ClosedChannelException:
If this channel is closed
Since
1.7
socketback to summary
public abstract DatagramSocket socket()

Retrieves a datagram socket associated with this channel.

Returns:DatagramSocket

A datagram socket associated with this channel

validOpsback to summary
public final int validOps()

Implements abstract java.nio.channels.SelectableChannel.validOps.

Returns an operation set identifying this channel's supported operations.

Datagram channels support reading and writing, so this method returns (SelectionKey#OP_READ | SelectionKey#OP_WRITE).

Returns:int

The valid-operation set

writeback to summary
public abstract int write(ByteBuffer src) throws IOException

Redeclares java.nio.channels.WritableByteChannel.write.

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 WritableByteChannel interface.

Exceptions
IOException:

Doc from java.nio.channels.WritableByteChannel.write.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupted status
writeback to summary
public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Redeclares java.nio.channels.GatheringByteChannel.write.

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 GatheringByteChannel interface.

Returns:long

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 buffer

Exceptions
IOException:

Doc from java.nio.channels.GatheringByteChannel.write.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupted status
writeback to summary
public final long write(ByteBuffer[] srcs) throws IOException

Implements java.nio.channels.GatheringByteChannel.write.

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 GatheringByteChannel interface.

Returns:long

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 buffer

Exceptions
IOException:

Doc from java.nio.channels.GatheringByteChannel.write.

If some other I/O error occurs

NotYetConnectedException:
If this channel's socket is not connected
ClosedChannelException:
If this channel is closed
AsynchronousCloseException:
If another thread closes this channel while the write operation is in progress
ClosedByInterruptException:
If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupted status