A SocketChannel is a selectable channel for part abstraction of stream connecting socket. The
socket
method of this class can return the related
Socket
instance, which can handle the socket.
A socket channel is open but not connected when created by open
method. After connected by calling the connect
method, it will keep connected before closed. The connection is non-blocking that the connect
method is for the initial connection and following finishConnect
method is for the final steps of connection. The isConnectionPending
method can tell the connection is blocked or not; the isConnected
method can tell the socket is connected finally or not.
The shut down operation can be independent and asynchronous for input and output. The shutdownInput
method is for input, and can make the following read operation fail as end of stream. If the input is shut down and another thread is pending in read operation, the read will end without effect and return end of stream. The shutdownOutput
method is for output, and can make the following write operation throwing a ClosedChannelException
. If the output is shut down and another is pending in a write operation, an AsynchronousCloseException
will thrown to the pending thread.
Socket channels are thread-safe, no more than one thread can read or write at given time. The connect
and finishConnect
methods are concurrent each other, when they are processing, other read and write will block.