GeezerVison is showing docs.oracle.com — SocketChannel (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.nio.channels
Class SocketChannel
-
- All Implemented Interfaces:
- Closeable, AutoCloseable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, NetworkChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
public abstract class SocketChannel
extends AbstractSelectableChannel
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
A selectable channel for stream-oriented connecting sockets.
A socket channel is created by invoking one of the open
methods of this class. It is not possible to create a channel for an arbitrary,
pre-existing 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 NotYetConnectedException to be thrown. A
socket channel can be connected by invoking its connect
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 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 connect method for
later completion by the finishConnect method.
Whether or not a connection operation is in progress may be determined by
invoking the isConnectionPending method.
Socket channels support asynchronous shutdown, which is similar
to the asynchronous close operation specified in the 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 AsynchronousCloseException.
Socket options are configured using the setOption method. Socket channels support the following 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 connect and 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.
- Since:
- 1.4
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
| Modifier and Type |
Method |
Description |
abstract SocketChannel |
bind(SocketAddress local) |
Binds the channel's socket to a local address.
|
abstract boolean |
connect(SocketAddress remote) |
Connects this channel's socket.
|
abstract boolean |
finishConnect() |
Finishes the process of connecting a socket channel.
|
abstract SocketAddress |
getLocalAddress() |
Returns the socket address that this channel's socket is bound to.
|
abstract SocketAddress |
getRemoteAddress() |
Returns the remote address to which this channel's socket is connected.
|
abstract boolean |
isConnected() |
Tells whether or not this channel's network socket is connected.
|
abstract boolean |
isConnectionPending() |
Tells whether or not a connection operation is in progress on this
channel.
|
static SocketChannel |
open() |
Opens a socket channel.
|
static SocketChannel |
open(SocketAddress remote) |
Opens a socket channel and connects it to a remote address.
|
abstract int |
read(ByteBuffer dst) |
Reads a sequence of bytes from this channel into the given buffer.
|
long |
read(ByteBuffer[] dsts) |
Reads a sequence of bytes from this channel into the given buffers.
|
abstract long |
read(ByteBuffer[] dsts,
int offset,
int length) |
Reads a sequence of bytes from this channel into a subsequence of the
given buffers.
|
abstract <T> SocketChannel |
setOption(SocketOption<T> name,
T value) |
Sets the value of a socket option.
|
abstract SocketChannel |
shutdownInput() |
Shutdown the connection for reading without closing the channel.
|
abstract SocketChannel |
shutdownOutput() |
Shutdown the connection for writing without closing the channel.
|
abstract Socket |
socket() |
Retrieves a socket associated with this channel.
|
int |
validOps() |
Returns an operation set identifying this channel's supported
operations.
|
abstract int |
write(ByteBuffer src) |
Writes a sequence of bytes to this channel from the given buffer.
|
long |
write(ByteBuffer[] srcs) |
Writes a sequence of bytes to this channel from the given buffers.
|
abstract long |
write(ByteBuffer[] srcs,
int offset,
int length) |
Writes a sequence of bytes to this channel from a subsequence of the
given buffers.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|