Module java.base

Class AsynchronousServerSocketChannel

java.lang.Object
java.nio.channels.AsynchronousServerSocketChannel
All Implemented Interfaces:
Closeable, AutoCloseable, AsynchronousChannel, Channel, NetworkChannel

public abstract class AsynchronousServerSocketChannel extends Object implements AsynchronousChannel, NetworkChannel
An asynchronous channel for stream-oriented listening sockets.

An asynchronous server-socket channel is created by invoking the open method of this class. A newly-created asynchronous server-socket channel is open but not yet bound. It can be bound to a local address and configured to listen for connections by invoking the bind method. Once bound, the accept method is used to initiate the accepting of connections to the channel's socket. An attempt to invoke the accept method on an unbound channel will cause a NotYetBoundException to be thrown.

Channels of this type are safe for use by multiple concurrent threads though at most one accept operation can be outstanding at any time. If a thread initiates an accept operation before a previous accept operation has completed then an AcceptPendingException will be thrown.

Socket options are configured using the setOption method. Channels of this type support the following options:

Socket options
Option Name Description
SO_RCVBUF The size of the socket receive buffer
SO_REUSEADDR Re-use address
Additional (implementation specific) options may also be supported.

Usage Example:

 final AsynchronousServerSocketChannel listener =
     AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000));

 listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
     public void completed(AsynchronousSocketChannel ch, Void att) {
         // accept the next connection
         listener.accept(null, this);

         // handle this connection
         handle(ch);
     }
     public void failed(Throwable exc, Void att) {
         ...
     }
 });
Since:
1.7