java.nio.channels
public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel
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.
Option Name Description SO_SNDBUFThe size of the socket send buffer SO_RCVBUFThe size of the socket receive buffer SO_KEEPALIVEKeep connection alive SO_REUSEADDRRe-use address SO_LINGERLinger on close if data is present (when configured in blocking mode only) TCP_NODELAYDisable the Nagle algorithm
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.
| Modifier | Constructor and Description |
|---|---|
protected |
SocketChannel(SelectorProvider provider)
Initializes a new instance of this class.
|
| Modifier and Type | Method and 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 |
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) |
long |
read(ByteBuffer[] dsts) |
abstract long |
read(ByteBuffer[] dsts,
int offset,
int length) |
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) |
long |
write(ByteBuffer[] srcs) |
abstract long |
write(ByteBuffer[] srcs,
int offset,
int length) |
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, registerregisterbegin, close, end, isOpenclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetLocalAddress, getOption, supportedOptionsprotected SocketChannel(SelectorProvider provider)
public static SocketChannel open() throws IOException
The new channel is created by invoking the openSocketChannel method of the system-wide default SelectorProvider object.
IOException - If an I/O error occurspublic static SocketChannel open(SocketAddress remote) throws IOException
This convenience method works as if by invoking the open()
method, invoking the connect method upon
the resulting socket channel, passing it remote, and then
returning that channel.
remote - The remote address to which the new channel is to be connectedAsynchronousCloseException - If another thread closes this channel
while the connect operation is in progressClosedByInterruptException - 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 statusUnresolvedAddressException - If the given remote address is not fully resolvedUnsupportedAddressTypeException - If the type of the given remote address is not supportedSecurityException - If a security manager has been installed
and it does not permit access to the given remote endpointIOException - If some other I/O error occurspublic final int validOps()
Socket channels support connecting, reading, and writing, so this
method returns (SelectionKey.OP_CONNECT
| SelectionKey.OP_READ | SelectionKey.OP_WRITE).
validOps in class SelectableChannelpublic abstract SocketChannel bind(SocketAddress local) throws IOException
NetworkChannel 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.
bind in interface NetworkChannellocal - The address to bind the socket, or null to bind the socket
to an automatically assigned socket addressConnectionPendingException - If a non-blocking connect operation is already in progress on
this channelAlreadyBoundException - If the socket is already boundUnsupportedAddressTypeException - If the type of the given address is not supportedClosedChannelException - If the channel is closedIOException - If some other I/O error occursNetworkChannel.getLocalAddress()public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException
NetworkChannelsetOption in interface NetworkChannelname - The socket optionvalue - The value of the socket option. A value of null may be
a valid value for some socket options.UnsupportedOperationException - If the socket option is not supported by this channelIllegalArgumentException - If the value is not a valid value for this socket optionClosedChannelException - If this channel is closedIOException - If an I/O error occursStandardSocketOptionpublic abstract SocketChannel shutdownInput() throws IOException
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.
NotYetConnectedException - If this channel is not yet connectedClosedChannelException - If this channel is closedIOException - If some other I/O error occurspublic abstract SocketChannel shutdownOutput() throws IOException
Once shutdown for writing then further attempts to write to the
channel will throw ClosedChannelException. If the output side of
the connection is already shutdown then invoking this method has no
effect.
NotYetConnectedException - If this channel is not yet connectedClosedChannelException - If this channel is closedIOException - If some other I/O error occurspublic abstract Socket socket()
The returned object will not declare any public methods that are not
declared in the Socket class.
public abstract boolean isConnected()
open and connectedpublic abstract boolean isConnectionPending()
finishConnect methodpublic abstract boolean connect(SocketAddress remote) throws IOException
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 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 Socket class. That is, if a security manager has been
installed then this method verifies that its checkConnect 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.
remote - The remote address to which this channel is to be connectedAlreadyConnectedException - If this channel is already connectedConnectionPendingException - If a non-blocking connection operation is already in progress
on this channelClosedChannelException - If this channel is closedAsynchronousCloseException - If another thread closes this channel
while the connect operation is in progressClosedByInterruptException - 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 statusUnresolvedAddressException - If the given remote address is not fully resolvedUnsupportedAddressTypeException - If the type of the given remote address is not supportedSecurityException - If a security manager has been installed
and it does not permit access to the given remote endpointIOException - If some other I/O error occurspublic abstract boolean finishConnect()
throws IOException
A non-blocking connection operation is initiated by placing a socket
channel in non-blocking mode and then invoking its connect 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
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.
NoConnectionPendingException - If this channel is not connected and a connection operation
has not been initiatedClosedChannelException - If this channel is closedAsynchronousCloseException - If another thread closes this channel
while the connect operation is in progressClosedByInterruptException - 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 statusIOException - If some other I/O error occurspublic abstract SocketAddress getRemoteAddress() throws IOException
Where the channel is bound and connected to an Internet Protocol
socket address then the return value from this method is of type InetSocketAddress.
null if the channel's socket is not
connectedClosedChannelException - If the channel is closedIOException - If an I/O error occurspublic abstract int read(ByteBuffer dst) throws IOException
read in interface ReadableByteChannelNotYetConnectedException - If this channel is not yet connectedIOExceptionpublic abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException
read in interface ScatteringByteChannelNotYetConnectedException - If this channel is not yet connectedIOExceptionpublic final long read(ByteBuffer[] dsts) throws IOException
read in interface ScatteringByteChannelNotYetConnectedException - If this channel is not yet connectedIOExceptionpublic abstract int write(ByteBuffer src) throws IOException
write in interface WritableByteChannelNotYetConnectedException - If this channel is not yet connectedIOExceptionpublic abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException
write in interface GatheringByteChannelNotYetConnectedException - If this channel is not yet connectedIOExceptionpublic final long write(ByteBuffer[] srcs) throws IOException
write in interface GatheringByteChannelNotYetConnectedException - If this channel is not yet connectedIOException