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

# NetworkChannel

` public interface NetworkChannel `   
` implements https://developer.android.com/reference/java/nio/channels/Channel `

|---|
| java.nio.channels.NetworkChannel |

Known indirect subclasses [AsynchronousServerSocketChannel](https://developer.android.com/reference/java/nio/channels/AsynchronousServerSocketChannel), [AsynchronousSocketChannel](https://developer.android.com/reference/java/nio/channels/AsynchronousSocketChannel), [DatagramChannel](https://developer.android.com/reference/java/nio/channels/DatagramChannel), [MulticastChannel](https://developer.android.com/reference/java/nio/channels/MulticastChannel), [ServerSocketChannel](https://developer.android.com/reference/java/nio/channels/ServerSocketChannel), [SocketChannel](https://developer.android.com/reference/java/nio/channels/SocketChannel) [AsynchronousServerSocketChannel](https://developer.android.com/reference/java/nio/channels/AsynchronousServerSocketChannel) An asynchronous channel for stream-oriented listening sockets. [AsynchronousSocketChannel](https://developer.android.com/reference/java/nio/channels/AsynchronousSocketChannel) An asynchronous channel for stream-oriented connecting sockets. [DatagramChannel](https://developer.android.com/reference/java/nio/channels/DatagramChannel) A selectable channel for datagram-oriented sockets. [MulticastChannel](https://developer.android.com/reference/java/nio/channels/MulticastChannel) A network channel that supports Internet Protocol (IP) multicasting. [ServerSocketChannel](https://developer.android.com/reference/java/nio/channels/ServerSocketChannel) A selectable channel for stream-oriented listening sockets. [SocketChannel](https://developer.android.com/reference/java/nio/channels/SocketChannel) A selectable channel for stream-oriented connecting sockets.   

*** ** * ** ***

A channel to a network socket.

A channel that implements this interface is a channel to a network socket. The `https://developer.android.com/reference/java/nio/channels/NetworkChannel#bind(java.net.SocketAddress)` method is used to bind the socket to a local `https://developer.android.com/reference/java/net/SocketAddress`, the `https://developer.android.com/reference/java/nio/channels/NetworkChannel#getLocalAddress()` method returns the address that the socket is bound to, and the `https://developer.android.com/reference/java/nio/channels/NetworkChannel#setOption(java.net.SocketOption<T>,%20T)` and `https://developer.android.com/reference/java/nio/channels/NetworkChannel#getOption(java.net.SocketOption<T>)` methods are used to set and query socket options. An implementation of this interface should specify the socket options that it supports.

The `https://developer.android.com/reference/java/nio/channels/NetworkChannel#bind(java.net.SocketAddress)` and `https://developer.android.com/reference/java/nio/channels/NetworkChannel#setOption(java.net.SocketOption<T>,%20T)` methods that do not otherwise have a value to return are specified to return the network channel upon which they are invoked. This allows method invocations to be chained. Implementations of this interface should specialize the return type so that method invocations on the implementation class can be chained.

## Summary

| ### Public methods ||
|---|---|
| ` 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. |

### Inherited methods

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/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/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Public methods

### bind

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

```
public abstract NetworkChannel 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/NetworkChannel` | This 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 is installed and it denies an unspecified permission. An implementation of this interface should specify any required permissions. |
| `https://developer.android.com/reference/java/nio/channels/AlreadyBoundException` | If the socket is already bound |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If the channel is closed |
| `https://developer.android.com/reference/java/nio/channels/UnsupportedAddressTypeException` | If the type of the given address is not supported |

**See also:**

- `https://developer.android.com/reference/java/nio/channels/NetworkChannel#getLocalAddress()`

### 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`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/SocketAddress` | The socket address that the socket is bound to, or `null` if the channel's socket is not bound <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 |

### getOption

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

```
public abstract T getOption (SocketOption<T> name)
```

Returns the value of a socket option.

<br />

| Parameters ||
|---|---|
| `name` | `SocketOption`: The socket option <br /> |

| Returns ||
|---|---|
| `T` | The value of the socket option. A value of `null` may be a valid value for some socket options. <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 socket option is not supported by this channel |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |

**See also:**

- `https://developer.android.com/reference/java/net/StandardSocketOptions`

### setOption

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

```
public abstract NetworkChannel 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/NetworkChannel` | This channel <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | If the value is not a valid value for this socket option |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the socket option is not supported by this channel |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |

**See also:**

- `https://developer.android.com/reference/java/net/StandardSocketOptions`

### supportedOptions

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

```
public abstract Set<SocketOption<?>> supportedOptions ()
```

Returns a set of the socket options supported by this channel.

This method will continue to return the set of options even after the channel has been closed.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Set<https://developer.android.com/reference/java/net/SocketOption<?>>` | A set of the socket options supported by this channel <br /> |