Home » openjdk-7 » java » nio » channels » [javadoc | source]
java.nio.channels
abstract public class: SocketChannel [javadoc | source]
java.lang.Object
   java.nio.channels.spi.AbstractInterruptibleChannel
      java.nio.channels.SelectableChannel
         java.nio.channels.spi.AbstractSelectableChannel
            java.nio.channels.SocketChannel

All Implemented Interfaces:
    ByteChannel, NetworkChannel, GatheringByteChannel, ScatteringByteChannel, Channel, InterruptibleChannel

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.

Fields inherited from java.nio.channels.spi.AbstractSelectableChannel:
blocking
Constructor:
 protected SocketChannel(SelectorProvider provider) 
    Initializes a new instance of this class.
Method from java.nio.channels.SocketChannel Summary:
bind,   connect,   finishConnect,   getRemoteAddress,   isConnected,   isConnectionPending,   open,   open,   read,   read,   read,   setOption,   shutdownInput,   shutdownOutput,   socket,   validOps,   write,   write,   write
Methods from java.nio.channels.spi.AbstractSelectableChannel:
blockingLock,   configureBlocking,   implCloseChannel,   implCloseSelectableChannel,   implConfigureBlocking,   isBlocking,   isRegistered,   keyFor,   provider,   register,   removeKey
Methods from java.nio.channels.SelectableChannel:
blockingLock,   configureBlocking,   isBlocking,   isRegistered,   keyFor,   provider,   register,   register,   validOps
Methods from java.nio.channels.spi.AbstractInterruptibleChannel:
begin,   blockedOn,   close,   end,   implCloseChannel,   isOpen
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.nio.channels.SocketChannel Detail:
 abstract public SocketChannel bind(SocketAddress local) throws IOException
 abstract public boolean connect(SocketAddress remote) throws IOException
    Connects this channel's socket.

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

 abstract public boolean finishConnect() throws IOException
    Finishes the process of connecting a socket channel.

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

 abstract public SocketAddress getRemoteAddress() throws IOException
    Returns the remote address to which this channel's socket is connected.

    Where the channel is bound and connected to an Internet Protocol socket address then the return value from this method is of type java.net.InetSocketAddress .

 abstract public boolean isConnected()
    Tells whether or not this channel's network socket is connected.
 abstract public boolean isConnectionPending()
    Tells whether or not a connection operation is in progress on this channel.

 public static SocketChannel open() throws IOException 
 public static SocketChannel open(SocketAddress remote) throws IOException 
    Opens a socket channel and connects it to a remote address.

    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.

 abstract public int read(ByteBuffer dst) throws IOException
 public final long read(ByteBuffer[] dsts) throws IOException 
 abstract public long read(ByteBuffer[] dsts,
    int offset,
    int length) throws IOException
 abstract public SocketChannel setOption(SocketOption<T> name,
    T value) throws IOException
 abstract public SocketChannel shutdownInput() throws IOException
    Shutdown the connection for reading without closing the channel.

    Once shutdown for reading then further reads on the channel will return {@code -1}, the end-of-stream indication. If the input side of the connection is already shutdown then invoking this method has no effect.

 abstract public SocketChannel shutdownOutput() throws IOException
    Shutdown the connection for writing without closing the channel.

    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.

 abstract public Socket socket()
    Retrieves a socket associated with this channel.

    The returned object will not declare any public methods that are not declared in the java.net.Socket class.

 public final int validOps() 
 abstract public int write(ByteBuffer src) throws IOException
 public final long write(ByteBuffer[] srcs) throws IOException 
 abstract public long write(ByteBuffer[] srcs,
    int offset,
    int length) throws IOException