java.nio.channels
Class ServerSocketChannel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel 
      extended by java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel 
          extended by java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel 
              extended by java.nio.channels.ServerSocketChannel
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel

public abstract class ServerSocketChannel
extends AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel

A selectable channel for stream-oriented listening sockets.

Server-socket channels are not a complete abstraction of listening network sockets. Binding and the manipulation of socket options must be done through an associated ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket object obtained by invoking the socket sample code for java.nio.channels.ServerSocketChannel.socket() definition code for java.nio.channels.ServerSocketChannel.socket() method. It is not possible to create a channel for an arbitrary, pre-existing server socket, nor is it possible to specify the SocketImpl sample code for java.net.SocketImpl definition code for java.net.SocketImpl object to be used by a server socket associated with a server-socket channel.

A server-socket channel is created by invoking the open sample code for java.nio.channels.ServerSocketChannel.open() definition code for java.nio.channels.ServerSocketChannel.open() method of this class. A newly-created server-socket channel is open but not yet bound. An attempt to invoke the accept sample code for java.nio.channels.ServerSocketChannel.accept() definition code for java.nio.channels.ServerSocketChannel.accept() method of an unbound server-socket channel will cause a NotYetBoundException sample code for java.nio.channels.NotYetBoundException definition code for java.nio.channels.NotYetBoundException to be thrown. A server-socket channel can be bound by invoking one of the bind sample code for java.net.ServerSocket.bind(java.net.SocketAddress, int) definition code for java.net.ServerSocket.bind(java.net.SocketAddress, int) methods of an associated server socket.

Server-socket channels are safe for use by multiple concurrent threads.

Since:
1.4

Constructor Summary
protected ServerSocketChannel sample code for java.nio.channels.ServerSocketChannel.ServerSocketChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.ServerSocketChannel.ServerSocketChannel(java.nio.channels.spi.SelectorProvider) (SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
          Initializes a new instance of this class.
 
Method Summary
abstract  SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel accept sample code for java.nio.channels.ServerSocketChannel.accept() definition code for java.nio.channels.ServerSocketChannel.accept() ()
          Accepts a connection made to this channel's socket.
static ServerSocketChannel sample code for java.nio.channels.ServerSocketChannel definition code for java.nio.channels.ServerSocketChannel open sample code for java.nio.channels.ServerSocketChannel.open() definition code for java.nio.channels.ServerSocketChannel.open() ()
          Opens a server-socket channel.
abstract  ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket socket sample code for java.nio.channels.ServerSocketChannel.socket() definition code for java.nio.channels.ServerSocketChannel.socket() ()
          Retrieves a server socket associated with this channel.
 int validOps sample code for java.nio.channels.ServerSocketChannel.validOps() definition code for java.nio.channels.ServerSocketChannel.validOps() ()
          Returns an operation set identifying this channel's supported operations.
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel
blockingLock sample code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() definition code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() , configureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) , implCloseChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() , implCloseSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() , implConfigureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) , isBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() definition code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() , isRegistered sample code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() definition code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() , keyFor sample code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) definition code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) , provider sample code for java.nio.channels.spi.AbstractSelectableChannel.provider() definition code for java.nio.channels.spi.AbstractSelectableChannel.provider() , register sample code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object)
 
Methods inherited from class java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
register sample code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int) definition code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int)
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel
begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() , close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() , end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) , isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 

Constructor Detail

ServerSocketChannel sample code for java.nio.channels.ServerSocketChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.ServerSocketChannel(java.nio.channels.spi.SelectorProvider)

protected ServerSocketChannel(SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
Initializes a new instance of this class.

Method Detail

open sample code for java.nio.channels.ServerSocketChannel.open() definition code for java.nio.channels.ServerSocketChannel.open()

public static ServerSocketChannel sample code for java.nio.channels.ServerSocketChannel definition code for java.nio.channels.ServerSocketChannel  open()
                                throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Opens a server-socket channel.

The new channel is created by invoking the openServerSocketChannel sample code for java.nio.channels.spi.SelectorProvider.openServerSocketChannel() definition code for java.nio.channels.spi.SelectorProvider.openServerSocketChannel() method of the system-wide default SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider object.

The new channel's socket is initially unbound; it must be bound to a specific address via one of its socket's bind sample code for java.net.ServerSocket.bind(java.net.SocketAddress) definition code for java.net.ServerSocket.bind(java.net.SocketAddress) methods before connections can be accepted.

Returns:
A new socket channel
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

validOps sample code for java.nio.channels.ServerSocketChannel.validOps() definition code for java.nio.channels.ServerSocketChannel.validOps()

public final int validOps()
Returns an operation set identifying this channel's supported operations.

Server-socket channels only support the accepting of new connections, so this method returns SelectionKey.OP_ACCEPT sample code for java.nio.channels.SelectionKey.OP_ACCEPT definition code for java.nio.channels.SelectionKey.OP_ACCEPT .

Specified by:
validOps sample code for java.nio.channels.SelectableChannel.validOps() definition code for java.nio.channels.SelectableChannel.validOps() in class SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
Returns:
The valid-operation set

socket sample code for java.nio.channels.ServerSocketChannel.socket() definition code for java.nio.channels.ServerSocketChannel.socket()

public abstract ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket  socket()
Retrieves a server socket associated with this channel.

The returned object will not declare any public methods that are not declared in the ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket class.

Returns:
A server socket associated with this channel

accept sample code for java.nio.channels.ServerSocketChannel.accept() definition code for java.nio.channels.ServerSocketChannel.accept()

public abstract SocketChannel sample code for java.nio.channels.SocketChannel definition code for java.nio.channels.SocketChannel  accept()
                              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Accepts a connection made to this channel's socket.

If this channel is in non-blocking mode then this method will immediately return null if there are no pending connections. Otherwise it will block indefinitely until a new connection is available or an I/O error occurs.

The socket channel returned by this method, if any, will be in blocking mode regardless of the blocking mode of this channel.

This method performs exactly the same security checks as the accept sample code for java.net.ServerSocket.accept() definition code for java.net.ServerSocket.accept() method of the ServerSocket sample code for java.net.ServerSocket definition code for java.net.ServerSocket class. That is, if a security manager has been installed then for each new connection this method verifies that the address and port number of the connection's remote endpoint are permitted by the security manager's checkAccept sample code for java.lang.SecurityManager.checkAccept(java.lang.String, int) definition code for java.lang.SecurityManager.checkAccept(java.lang.String, int) method.

Returns:
The socket channel for the new connection, or null if this channel is in non-blocking mode and no connection is available to be accepted
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the accept operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status
NotYetBoundException sample code for java.nio.channels.NotYetBoundException definition code for java.nio.channels.NotYetBoundException - If this channel's socket has not yet been bound
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit access to the remote endpoint of the new connection
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs