SocketChannel

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:

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

Protected Constructor Summary

SocketChannel(SelectorProvider provider)
Initializes a new instance of this class.

Public Method Summary

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 long
read(ByteBuffer[] dsts, int offset, int length)
Reads a sequence of bytes from this channel into a subsequence of the given buffers.
abstract int
read(ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer.
final long
read(ByteBuffer[] dsts)
Reads a sequence of bytes from this channel into 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.
final 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.
final 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.

Inherited Method Summary