A socket channel is created by invoking one of the open
methods of
this class. The no-arg open
method opens a socket channel
for an Internet protocol socket. The open(ProtocolFamily)
method is used to open a socket channel for a socket of a specified protocol
family. 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 for Internet protocol sockets support
following options:
Option Name Description SO_SNDBUF
The size of the socket send buffer SO_RCVBUF
The size of the socket receive buffer SO_KEEPALIVE
Keep connection alive SO_REUSEADDR
Re-use address SO_LINGER
Linger on close if data is present (when configured in blocking mode only) TCP_NODELAY
Disable the Nagle algorithm
Socket channels for Unix domain sockets support:
Option Name Description SO_SNDBUF
The size of the socket send buffer SO_RCVBUF
The size of the socket receive buffer SO_LINGER
Linger on close if data is present (when configured in blocking mode only)
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.
Access | Constructor and Description |
---|---|
protected | SocketChannel(SelectorProvider
The provider that created this channel provider)Initializes a new instance of this class. |
Modifier and Type | Method and Description |
---|---|
public abstract SocketChannel | Returns: This channelThe address to bind the socket, or local)null to bind
the socket to an automatically assigned socket addressRedeclares java. Binds the channel's socket to a local address. |
public abstract boolean | Returns: true if a connection was established,
false if this channel is in non-blocking mode
and the connection operation is in progressThe remote address to which this channel is to be connected remote)Connects this channel's socket. |
public abstract boolean | Returns: true if, and only if, this channel's socket is now
connectedFinishes the process of connecting a socket channel. |
public abstract SocketAddress | Returns: TheSocketAddress that the socket is bound to, or the
SocketAddress representing the loopback address or empty
path if denied by the security manager, or null if the
channel's socket is not boundRedeclares java. Returns the socket address that this channel's socket is bound to. |
public abstract SocketAddress | Returns: The remote address;null if the channel's socket is not
connectedReturns the remote address to which this channel's socket is connected. |
public abstract boolean | |
public abstract boolean | Returns: true if, and only if, a connection operation has been
initiated on this channel but not yet completed by invoking the
finishConnect methodTells whether or not a connection operation is in progress on this channel. |
public static SocketChannel | |
public static SocketChannel | |
public static SocketChannel | Returns: A new, and connected, socket channelThe remote address to which the new channel is to be connected remote)Opens a socket channel and connects it to a remote address. |
public abstract int | read(ByteBuffer
The buffer into which bytes are to be transferred dst)Redeclares java. Reads a sequence of bytes from this channel into the given buffer. |
public abstract long | read(ByteBuffer[]
The buffers into which bytes are to be transferred dsts, 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 offset, int dsts.length The maximum number of buffers to be accessed; must be
non-negative and no larger than
length)dsts.length - offset Redeclares java. Reads a sequence of bytes from this channel into a subsequence of the given buffers. |
public final long | read(ByteBuffer[]
The buffers into which bytes are to be transferred dsts)Implements java. Reads a sequence of bytes from this channel into the given buffers. |
public abstract <T> SocketChannel | setOption(SocketOption<T>
The socket option name, T The value of the socket option. A value of value)null may be
a valid value for some socket options.Redeclares java. Sets the value of a socket option. |
public abstract SocketChannel | |
public abstract SocketChannel | Returns: The channelShutdown the connection for writing without closing the channel. |
public abstract Socket | Returns: A socket associated with this channelRetrieves a socket associated with this channel. |
public final int | Returns: The valid-operation setImplements abstract java. Returns an operation set identifying this channel's supported operations. |
public abstract int | write(ByteBuffer
The buffer from which bytes are to be retrieved src)Redeclares java. Writes a sequence of bytes to this channel from the given buffer. |
public abstract long | write(ByteBuffer[]
The buffers from which bytes are to be retrieved srcs, 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 offset, int srcs.length The maximum number of buffers to be accessed; must be
non-negative and no larger than
length)srcs.length - offset Redeclares java. Writes a sequence of bytes to this channel from a subsequence of the given buffers. |
public final long | write(ByteBuffer[]
The buffers from which bytes are to be retrieved srcs)Implements java. Writes a sequence of bytes to this channel from the given buffers. |
SocketChannel | back to summary |
---|---|
protected SocketChannel(SelectorProvider provider) Initializes a new instance of this class.
|
bind | back to summary |
---|---|
public abstract SocketChannel bind(SocketAddress local) throws IOException Redeclares java. Binds the channel's socket to a local address. This method is used to establish an association between the socket
and a local address. For Internet Protocol sockets, once an
association is established then the socket remains bound until the
channel is closed. If the API Note Binding a socket channel to a Unix Domain socket creates a file
corresponding to the file path in the Implementation Note Each platform enforces an implementation specific maximum length for the name of a Unix Domain socket. This limitation is enforced when a channel is bound. The maximum length is typically close to and generally not less than 100 bytes.
|
connect | back to summary |
---|---|
public abstract boolean connect(SocketAddress remote) throws IOException 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 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. For channels to Internet protocol sockets, this method performs
exactly the same security checks as the For channels to Unix Domain sockets, this method checks
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.
|
finishConnect | back to summary |
---|---|
public abstract boolean finishConnect() throws IOException 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 If this channel is already connected then this method will not block
and will immediately return 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.
|
getLocalAddress | back to summary |
---|---|
public abstract SocketAddress getLocalAddress() throws IOException Redeclares java. Doc from java. Returns the socket address that this channel's socket is bound to. Where the channel is Where the channel is bound to a Unix Domain socket address, the socket
address is a
|
getRemoteAddress | back to summary |
---|---|
public abstract SocketAddress getRemoteAddress() throws IOException Returns the remote address to which this channel's socket is connected. Where the channel's socket is bound and connected to an Internet
Protocol socket address then the return value is of type
Where the channel's socket is bound and connected to a Unix Domain
socket address, the returned address is a
|
isConnected | back to summary |
---|---|
public abstract boolean isConnected() Tells whether or not this channel's network socket is connected.
|
isConnectionPending | back to summary |
---|---|
public abstract boolean isConnectionPending() Tells whether or not a connection operation is in progress on this channel.
|
open | back to summary |
---|---|
public static SocketChannel open() throws IOException Opens a socket channel for an Internet protocol socket. The new channel is created by invoking the
|
open | back to summary |
---|---|
public static SocketChannel open(ProtocolFamily family) throws IOException Opens a socket channel. The The new channel is created by invoking the
|
open | back to summary |
---|---|
public static SocketChannel open(SocketAddress remote) throws IOException Opens a socket channel and connects it to a remote address. If the remote address is an If the remote address is a
|
read | back to summary |
---|---|
public abstract int read(ByteBuffer dst) throws IOException Redeclares java. Doc from java. 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,
Suppose that a byte sequence of length n is read, where
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.
|
read | back to summary |
---|---|
public abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException Redeclares java. Doc from java. 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 in the specified subsequence of the given buffer array, that is, dsts[offset].remaining() + dsts[offset+1].remaining() + ... + dsts[offset+length-1].remaining() Suppose that a byte sequence of length n is read, where
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.
|
read | back to summary |
---|---|
public final long read(ByteBuffer[] dsts) throws IOException Implements java. Doc from java. Reads a sequence of bytes from this channel into the given buffers. An invocation of this method of the form c.read(dsts, 0, dsts.length);
|
setOption | back to summary |
---|---|
public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException Redeclares java. Doc from java. Sets the value of a socket option.
|
shutdownInput | back to summary |
---|---|
public abstract SocketChannel shutdownInput() throws IOException Shutdown the connection for reading without closing the channel. Once shutdown for reading then further reads on the channel will
return
|
shutdownOutput | back to summary |
---|---|
public abstract SocketChannel shutdownOutput() throws IOException Shutdown the connection for writing without closing the channel. Once shutdown for writing then further attempts to write to the
channel will throw
|
socket | back to summary |
---|---|
public abstract Socket socket() Retrieves a socket associated with this channel.
|
validOps | back to summary |
---|---|
public final int validOps() Implements abstract java. Returns an operation set identifying this channel's supported operations. Socket channels support connecting, reading, and writing, so this
method returns
|
write | back to summary |
---|---|
public abstract int write(ByteBuffer src) throws IOException Redeclares java. Doc from java. 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,
Suppose that a byte sequence of length n is written, where
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.
|
write | back to summary |
---|---|
public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException Redeclares java. Doc from java. 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() Suppose that a byte sequence of length n is written, where
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.
|
write | back to summary |
---|---|
public final long write(ByteBuffer[] srcs) throws IOException Implements java. Doc from java. Writes a sequence of bytes to this channel from the given buffers. An invocation of this method of the form c.write(srcs, 0, srcs.length);
|