Asynchronous socket channels are created in one of two ways. A newly-created AsynchronousSocketChannel is created by invoking one of the open methods defined by this class. A newly-created channel is open but not yet connected. A connected AsynchronousSocketChannel is created when a connection is made to the socket of an AsynchronousServerSocketChannel. It is not possible to create an asynchronous socket channel for an arbitrary, pre-existing socket.
A newly-created channel is connected by invoking its connect method; once connected, a channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its getConnectedAddress method. Whether or not a connect operation is in progress may be determined by invoking the isConnectionPending method. An attempt to invoke an I/O operation upon an unconnected channel will cause a NotYetConnectedException to be thrown.
Channels of this type are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one read operation and one write operation can be outstanding at any time. If a thread initiates a read operation before a previous read operation has completed then a ReadPendingException will be thrown. Similarly, an attempt to initiate a write operation before a previous write has completed will throw a WritePendingException. Whether or not a read or write operation is pending may be determined by invoking the isReadPending and isWritePending methods.
Socket options are configured using the setOption method. Asynchronous socket channels support the following options:
and may support additional (implementation specific) options. The list of options supported is obtained by invoking the options method.
Option Name Description {@link StandardSocketOption#SO_SNDBUF SO_SNDBUF} The size of the socket send buffer {@link StandardSocketOption#SO_RCVBUF SO_RCVBUF} The size of the socket receive buffer {@link StandardSocketOption#SO_KEEPALIVE SO_KEEPALIVE} Keep connection alive {@link StandardSocketOption#SO_REUSEADDR SO_REUSEADDR} Re-use address {@link StandardSocketOption#TCP_NODELAY TCP_NODELAY} Disable the Nagle algorithm
When a timeout elapses then the state of the ByteBuffer, or the sequence of buffers, for the I/O operation is not defined. Buffers should be discarded or at least care must be taken to ensure that the buffers are not accessed while the channel remains open.
|
|
|
|