Added in API level 1

DatagramChannel

abstract class DatagramChannel : AbstractSelectableChannel, ByteChannel, GatheringByteChannel, MulticastChannel, ScatteringByteChannel
kotlin.Any
   ↳ java.nio.channels.spi.AbstractInterruptibleChannel
   ↳ java.nio.channels.SelectableChannel
   ↳ java.nio.channels.spi.AbstractSelectableChannel
   ↳ java.nio.channels.DatagramChannel

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:

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.

Summary

Protected constructors

Initializes a new instance of this class.

Public methods
abstract DatagramChannel!

abstract DatagramChannel!

Connects this channel's socket.

abstract DatagramChannel!

Disconnects this channel's socket.

abstract SocketAddress!

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

abstract SocketAddress!

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

abstract Boolean

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

open static DatagramChannel!

Opens a datagram channel.

open static DatagramChannel!
open(family: ProtocolFamily!)

Opens a datagram channel.

abstract Int

Reads a datagram from this channel.

Long
read(dsts: Array<ByteBuffer!>!)

Reads a datagram from this channel.

abstract Long
read(dsts: Array<ByteBuffer!>!, offset: Int, length: Int)

Reads a datagram from this channel.

abstract SocketAddress!

Receives a datagram via this channel.

abstract Int
send(src: ByteBuffer!, target: SocketAddress!)

Sends a datagram via this channel.

abstract DatagramChannel!
setOption(name: SocketOption<T>!, value: T)

abstract DatagramSocket!

Retrieves a datagram socket associated with this channel.

Int

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

abstract Int

Writes a datagram to this channel.

Long

Writes a datagram to this channel.

abstract Long
write(srcs: Array<ByteBuffer!>!, offset: Int, length: Int)

Writes a datagram to this channel.

Inherited functions

Protected constructors

DatagramChannel

Added in API level 1
protected DatagramChannel(provider: SelectorProvider!)

Initializes a new instance of this class.

Parameters
provider SelectorProvider!: The provider that created this channel

Public methods

bind

Added in API level 24
abstract fun bind(local: SocketAddress!): DatagramChannel!
Parameters
local SocketAddress!: The address to bind the socket, or null to bind the socket to an automatically assigned socket address
Return
DatagramChannel! This channel
Exceptions
java.nio.channels.AlreadyBoundException If the socket is already bound
java.nio.channels.UnsupportedAddressTypeException If the type of the given address is not supported
java.nio.channels.ClosedChannelException If the channel is closed
java.io.IOException If some other I/O error occurs
java.lang.SecurityException If a security manager has been installed and its checkListen method denies the operation

connect

Added in API level 1
abstract fun connect(remote: SocketAddress!): DatagramChannel!

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. A datagram socket remains connected until it is explicitly disconnected or until it is closed.

This method performs exactly the same security checks as the java.net.DatagramSocket#connect method of the class. That is, if a security manager has been installed then this method verifies that its checkAccept and java.lang.SecurityManager#checkConnect methods permit datagrams to be received from and sent to, respectively, the given remote address.

This method may be invoked at any time. It will not have any effect on read or write operations that are already in progress at the moment that it is invoked. 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
Return
DatagramChannel! This datagram channel
Exceptions
java.nio.channels.AlreadyConnectedException If this channel is already connected
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the connect operation is in progress
java.nio.channels.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 interrupt status
java.nio.channels.UnresolvedAddressException If the given remote address is not fully resolved
java.nio.channels.UnsupportedAddressTypeException If the type of the given remote address is not supported
java.lang.SecurityException If a security manager has been installed and it does not permit access to the given remote address
java.io.IOException If some other I/O error occurs

disconnect

Added in API level 1
abstract fun disconnect(): DatagramChannel!

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 so long as the security manager, if installed, permits it.

This method may be invoked at any time. It will not have any effect on read or write operations that are already in progress at the moment that it is invoked.

If this channel's socket is not connected, or if the channel is closed, then invoking this method has no effect.

Return
DatagramChannel! This datagram channel
Exceptions
java.io.IOException If some other I/O error occurs

getLocalAddress

Added in API level 24
abstract fun getLocalAddress(): SocketAddress!

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 .

If there is a security manager set, its checkConnect method is called with the local address and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, a SocketAddress representing the loopback address and the local port of the channel's socket is returned.

Return
SocketAddress! The SocketAddress that the socket is bound to, or the SocketAddress representing the loopback address if denied by the security manager, or null if the channel's socket is not bound
Exceptions
java.nio.channels.ClosedChannelException If the channel is closed
java.io.IOException If an I/O error occurs

getRemoteAddress

Added in API level 24
abstract fun getRemoteAddress(): SocketAddress!

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

Return
SocketAddress! The remote address; null if the channel's socket is not connected
Exceptions
java.nio.channels.ClosedChannelException If the channel is closed
java.io.IOException If an I/O error occurs

isConnected

Added in API level 1
abstract fun isConnected(): Boolean

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

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

open

Added in API level 1
open static fun open(): DatagramChannel!

Opens a datagram channel.

The new channel is created by invoking the openDatagramChannel method of the system-wide default 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.

Return
DatagramChannel! A new datagram channel
Exceptions
java.io.IOException If an I/O error occurs

open

Added in API level 24
open static fun open(family: ProtocolFamily!): DatagramChannel!

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 object. The channel will not be connected.

Parameters
family ProtocolFamily!: The protocol family
Return
DatagramChannel! A new datagram channel
Exceptions
java.lang.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.
java.io.IOException If an I/O error occurs

read

Added in API level 1
abstract fun read(dst: ByteBuffer!): Int

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.

Parameters
dst ByteBuffer!: The buffer into which bytes are to be transferred
Return
Int The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Exceptions
java.nio.channels.NonReadableChannelException If this channel was not opened for reading
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the read operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected

read

Added in API level 1
fun read(dsts: Array<ByteBuffer!>!): Long

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.

Parameters
dsts Array<ByteBuffer!>!: The buffers into which bytes are to be transferred
Return
Long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Exceptions
java.nio.channels.NonReadableChannelException If this channel was not opened for reading
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the read operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected

read

Added in API level 1
abstract fun read(
    dsts: Array<ByteBuffer!>!,
    offset: Int,
    length: Int
): Long

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.

Parameters
dsts Array<ByteBuffer!>!: The buffers into which bytes are to be transferred
offset Int: The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length Int: The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Return
Long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Exceptions
java.lang.IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold
java.nio.channels.NonReadableChannelException If this channel was not opened for reading
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the read operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected

receive

Added in API level 1
abstract fun receive(dst: ByteBuffer!): SocketAddress!

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 performs exactly the same security checks as the receive method of the class. That is, if the socket is not connected to a specific remote address and a security manager has been installed then for each datagram received this method verifies that the source's address and port number are permitted by the security manager's checkAccept method. The overhead of this security check can be avoided by first connecting the socket via the connect method.

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
Return
SocketAddress! The datagram's source address, or null if this channel is in non-blocking mode and no datagram was immediately available
Exceptions
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the read operation is in progress
java.nio.channels.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 interrupt status
java.lang.SecurityException If a security manager has been installed and it does not permit datagrams to be accepted from the datagram's sender
java.io.IOException If some other I/O error occurs

send

Added in API level 1
abstract fun send(
    src: ByteBuffer!,
    target: SocketAddress!
): Int

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 performs exactly the same security checks as the send method of the class. That is, if the socket is not connected to a specific remote address and a security manager has been installed then for each datagram sent this method verifies that the target address and port number are permitted by the security manager's java.lang.SecurityManager#checkConnect method. The overhead of this security check can be avoided by first connecting the socket via the connect method.

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
Return
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
java.nio.channels.AlreadyConnectedException If this channel is connected to a different address from that specified by target
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the read operation is in progress
java.nio.channels.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 interrupt status
java.nio.channels.UnresolvedAddressException If the given remote address is not fully resolved
java.nio.channels.UnsupportedAddressTypeException If the type of the given remote address is not supported
java.lang.SecurityException If a security manager has been installed and it does not permit datagrams to be sent to the given address
java.io.IOException If some other I/O error occurs

setOption

Added in API level 24
abstract fun <T : Any!> setOption(
    name: SocketOption<T>!,
    value: T
): DatagramChannel!
Parameters
<T> The type of the socket option value
name SocketOption<T>!: The socket option
value T: The value of the socket option. A value of null may be a valid value for some socket options.
Return
DatagramChannel! This channel
Exceptions
java.lang.UnsupportedOperationException If the socket option is not supported by this channel
java.lang.IllegalArgumentException If the value is not a valid value for this socket option
java.nio.channels.ClosedChannelException If this channel is closed
java.io.IOException If an I/O error occurs

socket

Added in API level 1
abstract fun socket(): DatagramSocket!

Retrieves a datagram socket associated with this channel.

The returned object will not declare any public methods that are not declared in the java.net.DatagramSocket class.

Return
DatagramSocket! A datagram socket associated with this channel

validOps

Added in API level 1
fun validOps(): Int

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

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

Return
Int The valid-operation set

write

Added in API level 1
abstract fun write(src: ByteBuffer!): Int

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.

Parameters
src ByteBuffer!: The buffer from which bytes are to be retrieved
Return
Int The number of bytes written, possibly zero
Exceptions
java.nio.channels.NonWritableChannelException If this channel was not opened for writing
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the write operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected

write

Added in API level 1
fun write(srcs: Array<ByteBuffer!>!): Long

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.

Parameters
srcs Array<ByteBuffer!>!: The buffers from which bytes are to be retrieved
Return
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
java.nio.channels.NonWritableChannelException If this channel was not opened for writing
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the write operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected

write

Added in API level 1
abstract fun write(
    srcs: Array<ByteBuffer!>!,
    offset: Int,
    length: Int
): Long

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.

Parameters
srcs Array<ByteBuffer!>!: The buffers from which bytes are to be retrieved
offset Int: The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
length Int: The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Return
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
java.lang.IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold
java.nio.channels.NonWritableChannelException If this channel was not opened for writing
java.nio.channels.ClosedChannelException If this channel is closed
java.nio.channels.AsynchronousCloseException If another thread closes this channel while the write operation is in progress
java.nio.channels.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 interrupt status
java.io.IOException If some other I/O error occurs
java.nio.channels.NotYetConnectedException If this channel's socket is not connected