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/SocketChannel#proctors) \| [Methods](https://developer.android.com/reference/java/nio/channels/SocketChannel#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/nio/channels/SocketChannel#inhmethods)

# SocketChannel


`
public


abstract
class
SocketChannel
`


`


`

`


`

`


`

`

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/NetworkChannel,

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

<br />

*** ** * ** ***

A selectable channel for stream-oriented connecting sockets.

A socket channel is created by invoking one of the `https://developer.android.com/reference/java/nio/channels/SocketChannel#open()`
methods of this class. It is not possible to create a channel for an arbitrary,
pre-existing socket. A newly-created socket channel is open but not yet
connected. An attempt to invoke an I/O operation upon an unconnected
channel will cause a `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` to be thrown. A
socket channel can be connected by invoking its `https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)`
method; once connected, a socket channel remains connected until it is
closed. Whether or not a socket channel is connected may be determined by
invoking its `https://developer.android.com/reference/java/nio/channels/SocketChannel#isConnected()` method.

Socket channels support *non-blocking connection:* A socket
channel may be created and the process of establishing the link to the
remote socket may be initiated via the `https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)` method for
later completion by the `https://developer.android.com/reference/java/nio/channels/SocketChannel#finishConnect()` method.
Whether or not a connection operation is in progress may be determined by
invoking the `https://developer.android.com/reference/java/nio/channels/SocketChannel#isConnectionPending()` method.

Socket channels support *asynchronous shutdown,* which is similar
to the asynchronous close operation specified in the `https://developer.android.com/reference/java/nio/channels/Channel` class.
If the input side of a socket is shut down by one thread while another
thread is blocked in a read operation on the socket's channel, then the read
operation in the blocked thread will complete without reading any bytes and
will return `-1`. If the output side of a socket is shut down by one
thread while another thread is blocked in a write operation on the socket's
channel, then the blocked thread will receive an `https://developer.android.com/reference/java/nio/channels/AsynchronousCloseException`.

Socket options are configured using the `https://developer.android.com/reference/java/nio/channels/SocketChannel#setOption(java.net.SocketOption<T>,%20T)` method. Socket channels support 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_KEEPALIVE` | Keep connection alive |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_REUSEADDR` | Re-use address |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#SO_LINGER` | Linger on close if data is present (when configured in blocking mode only) |
> | `https://developer.android.com/reference/java/net/StandardSocketOptions#TCP_NODELAY` | Disable the Nagle algorithm |
> |---|---|
> [Socket options]

Additional (implementation specific) options may also be supported.

Socket 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. The `https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)` and `https://developer.android.com/reference/java/nio/channels/SocketChannel#finishConnect()` methods are
mutually synchronized against each other, and an attempt to initiate a read
or write operation while an invocation of one of these methods is in
progress will block until that invocation is complete.

<br />

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/nio/channels/SocketChannel#SocketChannel(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/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#bind(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress local) ` Binds the channel's socket to a local address. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress remote) ` Connects this channel's socket. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#finishConnect()() ` Finishes the process of connecting a socket channel. |
| ` abstract https://developer.android.com/reference/java/net/SocketAddress` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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/SocketChannel#getRemoteAddress()() ` Returns the remote address to which this channel's socket is connected. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#isConnected()() ` Tells whether or not this channel's network socket is connected. |
| ` abstract boolean` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#isConnectionPending()() ` Tells whether or not a connection operation is in progress on this channel. |
| ` static https://developer.android.com/reference/java/nio/channels/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#open()() ` Opens a socket channel. |
| ` static https://developer.android.com/reference/java/nio/channels/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#open(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress remote) ` Opens a socket channel and connects it to a remote address. |
| ` abstract long` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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 int` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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. |
| ` final long` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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. |
| ` abstract <T> https://developer.android.com/reference/java/nio/channels/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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/nio/channels/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#shutdownInput()() ` Shutdown the connection for reading without closing the channel. |
| ` abstract https://developer.android.com/reference/java/nio/channels/SocketChannel` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#shutdownOutput()() ` Shutdown the connection for writing without closing the channel. |
| ` abstract https://developer.android.com/reference/java/net/Socket` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#socket()() ` Retrieves a socket associated with this channel. |
| ` final int` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#validOps()() ` Returns an operation set identifying this channel's supported operations. |
| ` abstract int` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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. |
| ` final long` | ` https://developer.android.com/reference/java/nio/channels/SocketChannel#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/SocketChannel#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. |

| ### 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/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/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/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Protected constructors

### SocketChannel

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

```
protected SocketChannel (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 SocketChannel 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/SocketChannel` | 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/ConnectionPendingException` | If a non-blocking connect operation is already in progress on this channel |
| `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 boolean connect (SocketAddress remote)
```

Connects this channel's socket.

If this channel is in non-blocking mode then an invocation of this
method initiates a non-blocking connection operation. If the connection
is established immediately, as can happen with a local connection, then
this method returns `true`. Otherwise this method returns
`false` and the connection operation must later be completed by
invoking the `https://developer.android.com/reference/java/nio/channels/SocketChannel#finishConnect()` method.

If this channel is in blocking mode then an invocation of this
method will block until the connection is established or an I/O error
occurs.

This method performs exactly the same security checks as the `https://developer.android.com/reference/java/net/Socket` class. That is, if a security manager has been
installed then this method verifies that its `https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int)` method permits
connecting to the address and port number of the given remote endpoint.

This method may be invoked at any time. If a read or write
operation upon this channel is invoked while an invocation of this
method is in progress then that operation will first block until this
invocation is complete. If a connection attempt is initiated but fails,
that is, if an invocation of this method throws a checked exception,
then the channel will be closed.

<br />

<br />

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

| Returns ||
|---|---|
| `boolean` | `true` if a connection was established, `false` if this channel is in non-blocking mode and the connection operation is in progress <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 endpoint |
| `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/ConnectionPendingException` | If a non-blocking connection operation is already in progress on this channel |
| `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 |

### finishConnect

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

```
public abstract boolean finishConnect ()
```

Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket
channel in non-blocking mode and then invoking its `https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)` method. Once the connection is established, or the attempt has
failed, the socket channel will become connectable and this method may
be invoked to complete the connection sequence. If the connection
operation failed then invoking this method will cause an appropriate
`https://developer.android.com/reference/java/io/IOException` to be thrown.

If this channel is already connected then this method will not block
and will immediately return `true`. If this channel is in
non-blocking mode then this method will return `false` if the
connection process is not yet complete. If this channel is in blocking
mode then this method will block until the connection either completes
or fails, and will always either return `true` or throw a checked
exception describing the failure.

This method may be invoked at any time. If a read or write
operation upon this channel is invoked while an invocation of this
method is in progress then that operation will first block until this
invocation is complete. If a connection attempt fails, that is, if an
invocation of this method throws a checked exception, then the channel
will be closed.

<br />

<br />

| Returns ||
|---|---|
| `boolean` | `true` if, and only if, this channel's socket is now connected <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |
| `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/NoConnectionPendingException` | If this channel is not connected and a connection operation has not been initiated |

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

Where the channel is bound and connected 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 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 network socket is connected.

<br />

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

### isConnectionPending

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

```
public abstract boolean isConnectionPending ()
```

Tells whether or not a connection operation is in progress on this
channel.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the `https://developer.android.com/reference/java/nio/channels/SocketChannel#finishConnect()` method <br /> |

### open

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

```
public static SocketChannel open ()
```

Opens a socket channel.

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

<br />

<br />

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

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

### open

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

```
public static SocketChannel open (SocketAddress remote)
```

Opens a socket channel and connects it to a remote address.

This convenience method works as if by invoking the `https://developer.android.com/reference/java/nio/channels/SocketChannel#open()`
method, invoking the `https://developer.android.com/reference/java/nio/channels/SocketChannel#connect(java.net.SocketAddress)` method upon
the resulting socket channel, passing it `remote`, and then
returning that channel.

<br />

<br />

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

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/SocketChannel` | A new, and connected, socket 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 endpoint |
| `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/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 |

### 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 sequence of bytes from this channel into a subsequence of the
given buffers.

An invocation of this method attempts to read up to *r* bytes
from this channel, where *r* is the total number of bytes remaining
the specified subsequence of the given buffer array, that is,

> ```
> dsts[offset].remaining()
>     + dsts[offset+1].remaining()
>     + ... + dsts[offset+length-1].remaining()
> ```
at the moment that this method is invoked.

Suppose that a byte sequence of length *n* is read, where
`0` `<=` *n* `<=` *r*.
Up to the first `dsts[offset].remaining()` bytes of this sequence
are transferred into buffer `dsts[offset]`, up to the next
`dsts[offset+1].remaining()` bytes are transferred into buffer
`dsts[offset+1]`, and so forth, until the entire byte sequence
is transferred into the given buffers. As many bytes as possible are
transferred into each buffer, hence the final position of each updated
buffer, except the last updated buffer, is guaranteed to be equal to
that buffer's limit.

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.

<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 is not yet 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 sequence of bytes from this channel into the given buffer.

An attempt is made to read up to *r* bytes from the channel,
where *r* is the number of bytes remaining in the buffer, that is,
`dst.remaining()`, at the moment this method is invoked.

Suppose that a byte sequence of length *n* is read, where
`0` `<=` *n* `<=` *r*.
This byte sequence will be transferred into the buffer so that the first
byte in the sequence is at index *p* and the last byte is at index
*p* `+` *n* `-` `1`,
where *p* is the buffer's position at the moment this method is
invoked. Upon return the buffer's position will be equal to
*p* `+` *n*; its limit will not have changed.

A read operation might not fill the buffer, and in fact it might not
read any bytes at all. Whether or not it does so depends upon the
nature and state of the channel. A socket channel in non-blocking mode,
for example, cannot read any more bytes than are immediately available
from the socket's input buffer; similarly, a file channel cannot read
any more bytes than remain in the file. It is guaranteed, however, that
if a channel is in blocking mode and there is at least one byte
remaining in the buffer then this method will block until at least one
byte is read.

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.

<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 is not yet 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 sequence of bytes from this channel into the given buffers.

An invocation of this method of the form `c.read(dsts)`
behaves in exactly the same manner as the invocation

> ```
> c.read(dsts, 0, dsts.length);
> ```

<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 is not yet connected |
| `https://developer.android.com/reference/java/io/IOException` |   |

### setOption

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

```
public abstract SocketChannel 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/SocketChannel` | 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` |   |

### shutdownInput

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

```
public abstract SocketChannel shutdownInput ()
```

Shutdown the connection for reading without closing the channel.

Once shutdown for reading then further reads on the channel will
return `-1`, the end-of-stream indication. If the input side of the
connection is already shutdown then invoking this method has no effect.

<br />

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

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel is not yet connected |

### shutdownOutput

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

```
public abstract SocketChannel shutdownOutput ()
```

Shutdown the connection for writing without closing the channel.

Once shutdown for writing then further attempts to write to the
channel will throw `https://developer.android.com/reference/java/nio/channels/ClosedChannelException`. If the output side of
the connection is already shutdown then invoking this method has no
effect.

<br />

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

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If some other I/O error occurs |
| `https://developer.android.com/reference/java/nio/channels/ClosedChannelException` | If this channel is closed |
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel is not yet connected |

### socket

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

```
public abstract Socket socket ()
```

Retrieves a 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/Socket` class.

<br />

<br />

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

Socket channels support connecting, reading, and writing, so this
method returns `(``https://developer.android.com/reference/java/nio/channels/SelectionKey#OP_CONNECT`
`|` `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 sequence of bytes to this channel from the given buffer.

An attempt is made to write up to *r* bytes to the channel,
where *r* is the number of bytes remaining in the buffer, that is,
`src.remaining()`, at the moment this method is invoked.

Suppose that a byte sequence of length *n* is written, where
`0` `<=` *n* `<=` *r*.
This byte sequence will be transferred from the buffer starting at index
*p*, where *p* is the buffer's position at the moment this
method is invoked; the index of the last byte written will be
*p* `+` *n* `-` `1`.
Upon return the buffer's position will be equal to
*p* `+` *n*; its limit will not have changed.

Unless otherwise specified, a write operation will return only after
writing all of the *r* requested bytes. Some types of channels,
depending upon their state, may write only some of the bytes or possibly
none at all. A socket channel in non-blocking mode, for example, cannot
write any more bytes than are free in the socket's output buffer.

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.

<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 is not yet 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 sequence of bytes to this channel from the given buffers.

An invocation of this method of the form `c.write(srcs)`
behaves in exactly the same manner as the invocation

> ```
> c.write(srcs, 0, srcs.length);
> ```

<br />

<br />

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

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

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/NotYetConnectedException` | If this channel is not yet 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 sequence of bytes to this channel from a subsequence of the
given buffers.

An attempt is made to write up to *r* bytes to this channel,
where *r* is the total number of bytes remaining in the specified
subsequence of the given buffer array, that is,

> ```
> srcs[offset].remaining()
>     + srcs[offset+1].remaining()
>     + ... + srcs[offset+length-1].remaining()
> ```
at the moment that this method is invoked.

Suppose that a byte sequence of length *n* is written, where
`0` `<=` *n* `<=` *r*.
Up to the first `srcs[offset].remaining()` bytes of this sequence
are written from buffer `srcs[offset]`, up to the next
`srcs[offset+1].remaining()` bytes are written from buffer
`srcs[offset+1]`, and so forth, until the entire byte sequence is
written. As many bytes as possible are written from each buffer, hence
the final position of each updated buffer, except the last updated
buffer, is guaranteed to be equal to that buffer's limit.

Unless otherwise specified, a write operation will return only after
writing all of the *r* requested bytes. Some types of channels,
depending upon their state, may write only some of the bytes or possibly
none at all. A socket channel in non-blocking mode, for example, cannot
write any more bytes than are free in the socket's output buffer.

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.

<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 written, possibly zero <br /> |

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