Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Protected Ctors](https://developer.android.com/reference/java/nio/channels/DatagramChannel#proctors) \| [Methods](https://developer.android.com/reference/java/nio/channels/DatagramChannel#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/nio/channels/DatagramChannel#inhmethods)

# DatagramChannel

` public abstract class DatagramChannel `   
` ` ` ` ` ` ` extends https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel ` ` implements https://developer.android.com/reference/java/nio/channels/ByteChannel, https://developer.android.com/reference/java/nio/channels/GatheringByteChannel, https://developer.android.com/reference/java/nio/channels/MulticastChannel, https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel `

|---|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||||
| ↳ | [java.nio.channels.spi.AbstractInterruptibleChannel](https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel) ||||
|   | ↳ | [java.nio.channels.SelectableChannel](https://developer.android.com/reference/java/nio/channels/SelectableChannel) |||
|   |   | ↳ | [java.nio.channels.spi.AbstractSelectableChannel](https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel) ||
|   |   |   | ↳ | java.nio.channels.DatagramChannel |

<br />

*** ** * ** ***

A selectable channel for datagram-oriented sockets.

A datagram channel is created by invoking one of the `https://developer.android.com/reference/java/nio/channels/DatagramChannel#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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#send(java.nio.ByteBuffer,%20java.net.SocketAddress)` and `https://developer.android.com/reference/java/nio/channels/DatagramChannel#receive(java.nio.ByteBuffer)` methods to be used. A datagram channel may be connected, by invoking its `https://developer.android.com/reference/java/nio/channels/DatagramChannel#connect(java.net.SocketAddress)` 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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#read(java.nio.ByteBuffer)` and `https://developer.android.com/reference/java/nio/channels/DatagramChannel#write(java.nio.ByteBuffer)` 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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#isConnected()` method.

Socket options are configured using the `https://developer.android.com/reference/java/nio/channels/DatagramChannel#setOption(java.net.SocketOption<T>,%20T)` method. A datagram channel to an Internet Protocol socket supports the following options:
>
> | Option Name | Description |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_SNDBUF` | The size of the socket send buffer |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_RCVBUF` | The size of the socket receive buffer |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_REUSEADDR` | Re-use address |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_BROADCAST` | Allow transmission of broadcast datagrams |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#IP_TOS` | The Type of Service (ToS) octet in the Internet Protocol (IP) header |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#IP_MULTICAST_IF` | The network interface for Internet Protocol (IP) multicast datagrams |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#IP_MULTICAST_TTL` | The *time-to-live* for Internet Protocol (IP) multicast datagrams |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#IP_MULTICAST_LOOP` | Loopback for Internet Protocol (IP) multicast datagrams |
> |---|---|
> [Socket options]

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.

<br />

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#DatagramChannel(java.nio.channels.spi.SelectorProvider)(https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider provider) ` Initializes a new instance of this class. |

| ### Public methods ||
|---|---|
| ` abstract https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#bind(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress local) ` Binds the channel's socket to a local address. |
| ` abstract https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#connect(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress remote) ` Connects this channel's socket. |
| ` abstract https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#disconnect()() ` Disconnects this channel's socket. |
| ` abstract https://developer.android.com/reference/java/net/SocketAddress` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#getLocalAddress()() ` Returns the socket address that this channel's socket is bound to. 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. |
| ` abstract https://developer.android.com/reference/java/net/SocketAddress` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#getRemoteAddress()() ` Returns the remote address to which this channel's socket is connected. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#isConnected()() ` Tells whether or not this channel's socket is connected. |
| ` static https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#open()() ` Opens a datagram channel. |
| ` static https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#open(java.net.ProtocolFamily)(https://developer.android.com/reference/java/net/ProtocolFamily family) ` Opens a datagram channel. |
| ` abstract long` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#read(java.nio.ByteBuffer[],%20int,%20int)(https://developer.android.com/reference/java/nio/ByteBuffer dsts, int offset, int length) ` Reads a datagram from this channel. |
| ` abstract int` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#read(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer dst) ` Reads a datagram from this channel. |
| ` final long` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#read(java.nio.ByteBuffer[])(https://developer.android.com/reference/java/nio/ByteBuffer dsts) ` Reads a datagram from this channel. |
| ` abstract https://developer.android.com/reference/java/net/SocketAddress` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#receive(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer dst) ` Receives a datagram via this channel. |
| ` abstract int` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#send(java.nio.ByteBuffer,%20java.net.SocketAddress)(https://developer.android.com/reference/java/nio/ByteBuffer src, https://developer.android.com/reference/java/net/SocketAddress target) ` Sends a datagram via this channel. |
| ` abstract <T> https://developer.android.com/reference/java/nio/channels/DatagramChannel` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#setOption(java.net.SocketOption<T>,%20T)(https://developer.android.com/reference/java/net/SocketOption<T> name, T value) ` Sets the value of a socket option. |
| ` abstract https://developer.android.com/reference/java/net/DatagramSocket` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#socket()() ` Retrieves a datagram socket associated with this channel. |
| ` final int` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#validOps()() ` Returns an operation set identifying this channel's supported operations. |
| ` abstract int` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#write(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer src) ` Writes a datagram to this channel. |
| ` final long` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#write(java.nio.ByteBuffer[])(https://developer.android.com/reference/java/nio/ByteBuffer srcs) ` Writes a datagram to this channel. |
| ` abstract long` | ` https://developer.android.com/reference/java/nio/channels/DatagramChannel#write(java.nio.ByteBuffer[],%20int,%20int)(https://developer.android.com/reference/java/nio/ByteBuffer srcs, int offset, int length) ` Writes a datagram to this channel. |

### Inherited methods

From class ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel ` ` final https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#blockingLock()() `

Retrieves the object upon which the `https://developer.android.com/reference/java/nio/channels/SelectableChannel#configureBlocking(boolean)` and `https://developer.android.com/reference/java/nio/channels/SelectableChannel#register(java.nio.channels.Selector,%20int)` methods synchronize.
` final https://developer.android.com/reference/java/nio/channels/SelectableChannel` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#configureBlocking(boolean)(boolean block) `

Adjusts this channel's blocking mode.
` final void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#implCloseChannel()() `

Closes this channel.
` abstract void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#implCloseSelectableChannel()() `

Closes this selectable channel.
` abstract void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#implConfigureBlocking(boolean)(boolean block) `

Adjusts this channel's blocking mode.
` final boolean` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#isBlocking()() `

Tells whether or not every I/O operation on this channel will block until it completes.
` final boolean` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#isRegistered()() `

Tells whether or not this channel is currently registered with any selectors.
` final https://developer.android.com/reference/java/nio/channels/SelectionKey` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#keyFor(java.nio.channels.Selector)(https://developer.android.com/reference/java/nio/channels/Selector sel) `

Retrieves the key representing the channel's registration with the given selector.
` final https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#provider()() `

Returns the provider that created this channel.
` final https://developer.android.com/reference/java/nio/channels/SelectionKey` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractSelectableChannel#register(java.nio.channels.Selector,%20int,%20java.lang.Object)(https://developer.android.com/reference/java/nio/channels/Selector sel, int ops, https://developer.android.com/reference/java/lang/Object att) `

Registers this channel with the given selector, returning a selection key.
From class ` https://developer.android.com/reference/java/nio/channels/SelectableChannel ` ` abstract https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#blockingLock()() `

Retrieves the object upon which the `https://developer.android.com/reference/java/nio/channels/SelectableChannel#configureBlocking(boolean)` and `https://developer.android.com/reference/java/nio/channels/SelectableChannel#register(java.nio.channels.Selector,%20int)` methods synchronize.
` abstract https://developer.android.com/reference/java/nio/channels/SelectableChannel` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#configureBlocking(boolean)(boolean block) `

Adjusts this channel's blocking mode.
` abstract boolean` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#isBlocking()() `

Tells whether or not every I/O operation on this channel will block until it completes.
` abstract boolean` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#isRegistered()() `

Tells whether or not this channel is currently registered with any selectors.
` abstract https://developer.android.com/reference/java/nio/channels/SelectionKey` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#keyFor(java.nio.channels.Selector)(https://developer.android.com/reference/java/nio/channels/Selector sel) `

Retrieves the key representing the channel's registration with the given selector.
` abstract https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#provider()() `

Returns the provider that created this channel.
` abstract https://developer.android.com/reference/java/nio/channels/SelectionKey` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#register(java.nio.channels.Selector,%20int,%20java.lang.Object)(https://developer.android.com/reference/java/nio/channels/Selector sel, int ops, https://developer.android.com/reference/java/lang/Object att) `

Registers this channel with the given selector, returning a selection key.
` final https://developer.android.com/reference/java/nio/channels/SelectionKey` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#register(java.nio.channels.Selector,%20int)(https://developer.android.com/reference/java/nio/channels/Selector sel, int ops) `

Registers this channel with the given selector, returning a selection key.
` abstract int` ` https://developer.android.com/reference/java/nio/channels/SelectableChannel#validOps()() `

Returns an [operation set](https://developer.android.com/reference/java/nio/channels/SelectionKey#opsets) identifying this channel's supported operations.
From class ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel ` ` final void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#begin()() `

Marks the beginning of an I/O operation that might block indefinitely.
` final void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#close()() `

Closes this channel.
` final void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#end(boolean)(boolean completed) `

Marks the end of an I/O operation that might block indefinitely.
` abstract void` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#implCloseChannel()() `

Closes this channel.
` final boolean` ` https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#isOpen()() `

Tells whether or not this channel is open.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/nio/channels/Channel ` ` abstract void` ` https://developer.android.com/reference/java/nio/channels/Channel#close()() `

Closes this channel.
` abstract boolean` ` https://developer.android.com/reference/java/nio/channels/Channel#isOpen()() `

Tells whether or not this channel is open.
From interface ` https://developer.android.com/reference/java/nio/channels/InterruptibleChannel ` ` abstract void` ` https://developer.android.com/reference/java/nio/channels/InterruptibleChannel#close()() `

Closes this channel.
From interface ` https://developer.android.com/reference/java/nio/channels/GatheringByteChannel ` ` abstract long` ` https://developer.android.com/reference/java/nio/channels/GatheringByteChannel#write(java.nio.ByteBuffer[])(https://developer.android.com/reference/java/nio/ByteBuffer srcs) `

Writes a sequence of bytes to this channel from the given buffers.
` abstract long` ` https://developer.android.com/reference/java/nio/channels/GatheringByteChannel#write(java.nio.ByteBuffer[],%20int,%20int)(https://developer.android.com/reference/java/nio/ByteBuffer srcs, int offset, int length) `

Writes a sequence of bytes to this channel from a subsequence of the given buffers.
From interface ` https://developer.android.com/reference/java/nio/channels/MulticastChannel ` ` abstract void` ` https://developer.android.com/reference/java/nio/channels/MulticastChannel#close()() `

Closes this channel.
` abstract https://developer.android.com/reference/java/nio/channels/MembershipKey` ` https://developer.android.com/reference/java/nio/channels/MulticastChannel#join(java.net.InetAddress,%20java.net.NetworkInterface)(https://developer.android.com/reference/java/net/InetAddress group, https://developer.android.com/reference/java/net/NetworkInterface interf) `

Joins a multicast group to begin receiving all datagrams sent to the group, returning a membership key.
` abstract https://developer.android.com/reference/java/nio/channels/MembershipKey` ` https://developer.android.com/reference/java/nio/channels/MulticastChannel#join(java.net.InetAddress,%20java.net.NetworkInterface,%20java.net.InetAddress)(https://developer.android.com/reference/java/net/InetAddress group, https://developer.android.com/reference/java/net/NetworkInterface interf, https://developer.android.com/reference/java/net/InetAddress source) `

Joins a multicast group to begin receiving datagrams sent to the group from a given source address.
From interface ` https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel ` ` abstract long` ` https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel#read(java.nio.ByteBuffer[],%20int,%20int)(https://developer.android.com/reference/java/nio/ByteBuffer dsts, int offset, int length) `

Reads a sequence of bytes from this channel into a subsequence of the given buffers.
` abstract long` ` https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel#read(java.nio.ByteBuffer[])(https://developer.android.com/reference/java/nio/ByteBuffer dsts) `

Reads a sequence of bytes from this channel into the given buffers.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/java/nio/channels/ReadableByteChannel ` ` abstract int` ` https://developer.android.com/reference/java/nio/channels/ReadableByteChannel#read(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer dst) `

Reads a sequence of bytes from this channel into the given buffer.
From interface ` https://developer.android.com/reference/java/nio/channels/WritableByteChannel ` ` abstract int` ` https://developer.android.com/reference/java/nio/channels/WritableByteChannel#write(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer src) `

Writes a sequence of bytes to this channel from the given buffer.
From interface ` https://developer.android.com/reference/java/nio/channels/NetworkChannel ` ` abstract https://developer.android.com/reference/java/nio/channels/NetworkChannel` ` https://developer.android.com/reference/java/nio/channels/NetworkChannel#bind(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress local) `

Binds the channel's socket to a local address.
` abstract https://developer.android.com/reference/java/net/SocketAddress` ` https://developer.android.com/reference/java/nio/channels/NetworkChannel#getLocalAddress()() `

Returns the socket address that this channel's socket is bound to.
` abstract <T> T` ` https://developer.android.com/reference/java/nio/channels/NetworkChannel#getOption(java.net.SocketOption<T>)(https://developer.android.com/reference/java/net/SocketOption<T> name) `

Returns the value of a socket option.
` abstract <T> https://developer.android.com/reference/java/nio/channels/NetworkChannel` ` https://developer.android.com/reference/java/nio/channels/NetworkChannel#setOption(java.net.SocketOption<T>,%20T)(https://developer.android.com/reference/java/net/SocketOption<T> name, T value) `

Sets the value of a socket option.
` abstract https://developer.android.com/reference/java/util/Set<https://developer.android.com/reference/java/net/SocketOption<?>>` ` https://developer.android.com/reference/java/nio/channels/NetworkChannel#supportedOptions()() `

Returns a set of the socket options supported by this channel.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Protected constructors

### DatagramChannel

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected DatagramChannel (SelectorProvider provider)
```

Initializes a new instance of this class.

<br />

| Parameters ||
|---|---|
| `provider` | `SelectorProvider`: The provider that created this channel <br /> |

## Public methods

### bind

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract DatagramChannel bind (SocketAddress local)
```

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.

<br />

| Parameters ||
|---|---|
| `local` | `SocketAddress`: The address to bind the socket, or `null` to bind the socket to an automatically assigned socket address <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | This channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/java/lang/SecurityException` | If a security manager has been installed and its `https://developer.android.com/reference/java/lang/SecurityManager#checkListen(int)` method denies the operation |
| `https://developer.android.com/reference/java/nio/channels/AlreadyBoundException` |   |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` |   |
| `https://developer.android.com/reference/java/nio/channels/UnsupportedAddressTypeException` |   |

### connect

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract DatagramChannel connect (SocketAddress remote)
```

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 `https://developer.android.com/reference/java/net/DatagramSocket#connect(java.net.InetAddress,%20int)` method of the `https://developer.android.com/reference/java/net/DatagramSocket` class. That is, if a security manager has been installed then this method verifies that its `https://developer.android.com/reference/java/lang/SecurityManager#checkAccept(java.lang.String,%20int)` and `https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int)` 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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#bind(java.net.SocketAddress)` method with a parameter of `null`.

<br />

<br />

| Parameters ||
|---|---|
| `remote` | `SocketAddress`: The remote address to which this channel is to be connected <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | This datagram channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |
| `https://developer.android.com/reference/java/lang/SecurityException` | If a security manager has been installed and it does not permit access to the given remote address |
| `https://developer.android.com/reference/java/nio/channels/AlreadyConnectedException` | If this channel is already connected |
| `https://developer.android.com/reference/java/nio/channels/AsynchronousCloseException` | If another thread closes this channel while the connect operation is in progress |
| `https://developer.android.com/reference/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 |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |
| `https://developer.android.com/reference/java/nio/channels/UnresolvedAddressException` | If the given remote address is not fully resolved |
| `https://developer.android.com/reference/java/nio/channels/UnsupportedAddressTypeException` | If the type of the given remote address is not supported |

### disconnect

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract DatagramChannel disconnect ()
```

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.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | This datagram channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |

### getLocalAddress

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract SocketAddress getLocalAddress ()
```

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

Where the channel is `https://developer.android.com/reference/java/nio/channels/NetworkChannel#bind(java.net.SocketAddress)` to an Internet Protocol socket address then the return value from this method is of type `https://developer.android.com/reference/java/net/InetSocketAddress`.

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 `https://developer.android.com/reference/java/net/InetAddress#getLoopbackAddress()` address and the local port of the channel's socket is returned.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/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 <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` |   |

### getRemoteAddress

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract SocketAddress getRemoteAddress ()
```

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

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/SocketAddress` | The remote address; `null` if the channel's socket is not connected <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If the channel is closed |

### isConnected

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean isConnected ()
```

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

<br />

| Returns ||
|---|---|
| `boolean` | `true` if, and only if, this channel's socket is `https://developer.android.com/reference/java/nio/channels/spi/AbstractInterruptibleChannel#isOpen()` and connected <br /> |

### open

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static DatagramChannel open ()
```

Opens a datagram channel.

The new channel is created by invoking the `https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider#openDatagramChannel()` method of the system-wide default `https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider` object. The channel will not be connected.

The `https://developer.android.com/reference/java/net/ProtocolFamily` of the channel's socket is platform (and possibly configuration) dependent and therefore unspecified. The `https://developer.android.com/reference/java/nio/channels/DatagramChannel#open(java.net.ProtocolFamily)` 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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | A new datagram channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |

### open

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static DatagramChannel open (ProtocolFamily family)
```

Opens a datagram channel.

The `family` parameter is used to specify the `https://developer.android.com/reference/java/net/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 `https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider#openDatagramChannel(java.net.ProtocolFamily)` method of the system-wide default `https://developer.android.com/reference/java/nio/channels/spi/SelectorProvider` object. The channel will not be connected.

<br />

| Parameters ||
|---|---|
| `family` | `ProtocolFamily`: The protocol family <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | A new datagram channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the specified protocol family is not supported. For example, suppose the parameter is specified as `https://developer.android.com/reference/java/net/StandardProtocolFamily#INET6` but IPv6 is not enabled on the platform. |

### read

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

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 `https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `dsts` | `ByteBuffer`: The buffers into which bytes are to be transferred <br /> |
| `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` <br /> |
| `length` | `int`: The maximum number of buffers to be accessed; must be non-negative and no larger than `dsts.length` - `offset` <br /> |

| Returns ||
|---|---|
| `long` | The number of bytes read, possibly zero, or `-1` if the channel has reached end-of-stream <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### read

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int read (ByteBuffer dst)
```

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 `https://developer.android.com/reference/java/nio/channels/ReadableByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `dst` | `ByteBuffer`: The buffer into which bytes are to be transferred <br /> |

| Returns ||
|---|---|
| `int` | The number of bytes read, possibly zero, or `-1` if the channel has reached end-of-stream <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### read

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final long read (ByteBuffer[] dsts)
```

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 `https://developer.android.com/reference/java/nio/channels/ScatteringByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `dsts` | `ByteBuffer`: The buffers into which bytes are to be transferred <br /> |

| Returns ||
|---|---|
| `long` | The number of bytes read, possibly zero, or `-1` if the channel has reached end-of-stream <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### receive

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract SocketAddress receive (ByteBuffer dst)
```

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 `https://developer.android.com/reference/java/nio/channels/ReadableByteChannel#read(java.nio.ByteBuffer)` 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 `https://developer.android.com/reference/java/net/DatagramSocket#receive(java.net.DatagramPacket)` method of the `https://developer.android.com/reference/java/net/DatagramSocket` 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 `https://developer.android.com/reference/java/lang/SecurityManager#checkAccept(java.lang.String,%20int)` method. The overhead of this security check can be avoided by first connecting the socket via the `https://developer.android.com/reference/java/nio/channels/DatagramChannel#connect(java.net.SocketAddress)` 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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#bind(java.net.SocketAddress)` method with a parameter of `null`.

<br />

<br />

| Parameters ||
|---|---|
| `dst` | `ByteBuffer`: The buffer into which the datagram is to be transferred <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/SocketAddress` | The datagram's source address, or `null` if this channel is in non-blocking mode and no datagram was immediately available <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |
| `https://developer.android.com/reference/java/lang/SecurityException` | If a security manager has been installed and it does not permit datagrams to be accepted from the datagram's sender |
| `https://developer.android.com/reference/java/nio/channels/AsynchronousCloseException` | If another thread closes this channel while the read operation is in progress |
| `https://developer.android.com/reference/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 |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |

### send

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int send (ByteBuffer src, 
                SocketAddress target)
```

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 `https://developer.android.com/reference/java/nio/channels/WritableByteChannel#write(java.nio.ByteBuffer)` operation.

This method performs exactly the same security checks as the `https://developer.android.com/reference/java/net/DatagramSocket#send(java.net.DatagramPacket)` method of the `https://developer.android.com/reference/java/net/DatagramSocket` 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 `https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int)` method. The overhead of this security check can be avoided by first connecting the socket via the `https://developer.android.com/reference/java/nio/channels/DatagramChannel#connect(java.net.SocketAddress)` 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 `https://developer.android.com/reference/java/nio/channels/DatagramChannel#bind(java.net.SocketAddress)` method with a parameter of `null`.

<br />

<br />

| Parameters ||
|---|---|
| `src` | `ByteBuffer`: The buffer containing the datagram to be sent <br /> |
| `target` | `SocketAddress`: The address to which the datagram is to be sent <br /> |

| 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 <br /> |

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

### setOption

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract DatagramChannel setOption (SocketOption<T> name, 
                T value)
```

Sets the value of a socket option.

<br />

| Parameters ||
|---|---|
| `name` | `SocketOption`: The socket option <br /> |
| `value` | `T`: The value of the socket option. A value of `null` may be a valid value for some socket options. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/DatagramChannel` | This channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` |   |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` |   |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` |   |

### socket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract DatagramSocket socket ()
```

Retrieves a datagram socket associated with this channel.

The returned object will not declare any public methods that are not declared in the `https://developer.android.com/reference/java/net/DatagramSocket` class.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/DatagramSocket` | A datagram socket associated with this channel <br /> |

### validOps

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final int validOps ()
```

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

Datagram channels support reading and writing, so this method returns `(``https://developer.android.com/reference/java/nio/channels/SelectionKey#OP_READ` `|` `https://developer.android.com/reference/java/nio/channels/SelectionKey#OP_WRITE``)`.

<br />

| Returns ||
|---|---|
| `int` | The valid-operation set <br /> |

### write

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int write (ByteBuffer src)
```

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 `https://developer.android.com/reference/java/nio/channels/WritableByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `src` | `ByteBuffer`: The buffer from which bytes are to be retrieved <br /> |

| Returns ||
|---|---|
| `int` | The number of bytes written, possibly zero <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### write

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final long write (ByteBuffer[] srcs)
```

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 `https://developer.android.com/reference/java/nio/channels/GatheringByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `srcs` | `ByteBuffer`: The buffers from which bytes are to be retrieved <br /> |

| 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 <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### write

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract long write (ByteBuffer[] srcs, 
                int offset, 
                int length)
```

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 `https://developer.android.com/reference/java/nio/channels/GatheringByteChannel` interface.

<br />

<br />

| Parameters ||
|---|---|
| `srcs` | `ByteBuffer`: The buffers from which bytes are to be retrieved <br /> |
| `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` <br /> |
| `length` | `int`: The maximum number of buffers to be accessed; must be non-negative and no larger than `srcs.length` - `offset` <br /> |

| 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 <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel's socket is not connected |
| `https://developer.android.com/reference/java/io/IOException` |   |