See Also: SocketChannel Members
A SocketChannel is a selectable channel that provides a partial abstraction of stream connecting socket. The SocketChannel.Socket method returns a Java.Net.Socket instance which allows a wider range of socket operations than SocketChannel itself.
A socket channel is open but not connected when created by SocketChannel.Open. After connecting it by calling SocketChannel.Connect(Java.Net.SocketAddress), it will remain connected until closed.
If the connection is non-blocking then SocketChannel.Connect(Java.Net.SocketAddress) is used to initiate the connection, followed by a call of SocketChannel.FinishConnect to perform the final steps of connecting. SocketChannel.IsConnectionPending to tests whether we're still trying to connect; SocketChannel.IsConnected tests whether the socket connect completed successfully. Note that realistic code should use a Java.Nio.Channels.Selector instead of polling. Note also that Java.Net.Socket can connect with a timeout, which is the most common use for a non-blocking connect.
The input and output sides of a channel can be shut down independently and asynchronously without closing the channel. The Java.Net.Socket.ShutdownInput method on the socket returned by SocketChannel.Socket is used for the input side of a channel and subsequent read operations return -1, which means end of stream. If another thread is blocked in a read operation when the shutdown occurs, the read will end without effect and return end of stream. Likewise the Java.Net.Socket.ShutdownOutput method is used for the output side of the channel; subsequent write operations throw a Java.Nio.Channels.ClosedChannelException. If the output is shut down and another thread is blocked in a write operation, an Java.Nio.Channels.AsynchronousCloseException will be thrown to the pending thread.
Socket channels are thread-safe, no more than one thread can read or write at any given time. The SocketChannel.Connect(Java.Net.SocketAddress) and SocketChannel.FinishConnect methods are synchronized against each other; when they are processing, calls to SocketChannel.Read(Java.Nio.ByteBuffer) and SocketChannel.Write(Java.Nio.ByteBuffer) will block.