APIdia Launch Partner
Top Description Fields Constructors Methods
java.nio.channels

public abstract Class AsynchronousServerSocketChannel

extends Object
implements AsynchronousChannel, NetworkChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.NetworkChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.AsynchronousChannel
Known Direct Subclasses
sun.nio.ch.AsynchronousServerSocketChannelImpl
Imports
java.nio.channels.spi.*, java.net.SocketOption, .SocketAddress, java.util.concurrent.Future, java.io.IOException

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) { ... } });
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

Field Summary

Modifier and TypeField and Description
private final AsynchronousChannelProvider

Constructor Summary

AccessConstructor and Description
protected
AsynchronousServerSocketChannel(AsynchronousChannelProvider
The provider that created this channel
provider
)

Initializes a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
public abstract <
The type of the attachment
A
>
void
accept(A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<AsynchronousSocketChannel, ? super A>
The handler for consuming the result
handler
)

Accepts a connection.

public abstract Future<AsynchronousSocketChannel>

Returns:

a Future object representing the pending result
accept
()

Accepts a connection.

public final AsynchronousServerSocketChannel

Returns:

This channel
bind
(SocketAddress
The local address to bind the socket, or null to bind to an automatically assigned socket address
local
)

Implements java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address and configures the socket to listen for connections.

public abstract AsynchronousServerSocketChannel

Returns:

This channel
bind
(SocketAddress
The local address to bind the socket, or null to bind to an automatically assigned socket address
local
,
int
The maximum number of pending connections
backlog
)

Binds the channel's socket to a local address and configures the socket to listen for connections.

public abstract SocketAddress

Returns:

The SocketAddress that the socket is bound to; null if the channel's socket is not bound
getLocalAddress
()

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.

public static AsynchronousServerSocketChannel

Returns:

A new asynchronous server socket channel
open
(AsynchronousChannelGroup
The group to which the newly constructed channel should be bound, or null for the default group
group
)

Opens an asynchronous server-socket channel.

public static AsynchronousServerSocketChannel

Returns:

A new asynchronous server socket channel
open
()

Opens an asynchronous server-socket channel.

public final AsynchronousChannelProvider

Returns:

The provider that created this channel
provider
()

Returns the provider that created this channel.

public abstract <T> AsynchronousServerSocketChannel
setOption(SocketOption<T> name, T value)

Redeclares java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

providerback to summary
private final AsynchronousChannelProvider provider

Constructor Detail

AsynchronousServerSocketChannelback to summary
protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider)

Initializes a new instance of this class.

Parameters
provider:AsynchronousChannelProvider

The provider that created this channel

Method Detail

acceptback to summary
public abstract <A> void accept(A attachment, CompletionHandler<AsynchronousSocketChannel, ? super A> handler)

Accepts a connection.

This method initiates an asynchronous operation to accept a connection made to this channel's socket. The handler parameter is a completion handler that is invoked when a connection is accepted (or the operation fails). The result passed to the completion handler is the AsynchronousSocketChannel to the new connection.

When a new connection is accepted then the resulting AsynchronousSocketChannel will be bound to the same AsynchronousChannelGroup as this channel. If the group is shutdown and a connection is accepted, then the connection is closed, and the operation completes with an IOException and cause ShutdownChannelGroupException.

To allow for concurrent handling of new connections, the completion handler is not invoked directly by the initiating thread when a new connection is accepted immediately (see Threading).

Parameters
<A>
The type of the attachment
attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<AsynchronousSocketChannel, ? super A>

The handler for consuming the result

Exceptions
AcceptPendingException:
If an accept operation is already in progress on this channel
NotYetBoundException:
If this channel's socket has not yet been bound
ShutdownChannelGroupException:
If the channel group has terminated
acceptback to summary
public abstract Future<AsynchronousSocketChannel> accept()

Accepts a connection.

This method initiates an asynchronous operation to accept a connection made to this channel's socket. The method behaves in exactly the same manner as the accept(Object, CompletionHandler) method except that instead of specifying a completion handler, this method returns a Future representing the pending result. The Future's get method returns the AsynchronousSocketChannel to the new connection on successful completion.

Returns:Future<AsynchronousSocketChannel>

a Future object representing the pending result

Exceptions
AcceptPendingException:
If an accept operation is already in progress on this channel
NotYetBoundException:
If this channel's socket has not yet been bound
bindback to summary
public final AsynchronousServerSocketChannel bind(SocketAddress local) throws IOException

Implements java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address and configures the socket to listen for connections.

An invocation of this method is equivalent to the following:

bind(local, 0);
bind(local, 0);
Parameters
local:SocketAddress

The local address to bind the socket, or null to bind to an automatically assigned socket address

Returns:AsynchronousServerSocketChannel

This channel

Exceptions
IOException:
If some other I/O error occurs
AlreadyBoundException:
If the socket is already bound
UnsupportedAddressTypeException:
If the type of the given address is not supported
ClosedChannelException:
If the channel is closed
bindback to summary
public abstract AsynchronousServerSocketChannel bind(SocketAddress local, int backlog) throws IOException

Binds the channel's socket to a local address and configures the socket to listen for connections.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the associated channel is closed.

The backlog parameter is the maximum number of pending connections on the socket. Its exact semantics are implementation specific. In particular, an implementation may impose a maximum length or may choose to ignore the parameter altogether. If the backlog parameter has the value 0, or a negative value, then an implementation specific default is used.

Parameters
local:SocketAddress

The local address to bind the socket, or null to bind to an automatically assigned socket address

backlog:int

The maximum number of pending connections

Returns:AsynchronousServerSocketChannel

This channel

Exceptions
IOException:
If some other I/O error occurs
AlreadyBoundException:
If the socket is already bound
UnsupportedAddressTypeException:
If the type of the given address is not supported
ClosedChannelException:
If the channel is closed
getLocalAddressback to summary
public abstract SocketAddress getLocalAddress() throws IOException

Redeclares java.nio.channels.NetworkChannel.getLocalAddress.

Doc from java.nio.channels.NetworkChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.

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

Returns:SocketAddress

The SocketAddress that the socket is bound to; null if the channel's socket is not bound

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
ClosedChannelException:
If the channel is closed
openback to summary
public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group) throws IOException

Opens an asynchronous server-socket channel.

The new channel is created by invoking the openAsynchronousServerSocketChannel method on the java.nio.channels.spi.AsynchronousChannelProvider object that created the given group. If the group parameter is null then the resulting channel is created by the system-wide default provider, and bound to the default group.

Parameters
group:AsynchronousChannelGroup

The group to which the newly constructed channel should be bound, or null for the default group

Returns:AsynchronousServerSocketChannel

A new asynchronous server socket channel

Exceptions
IOException:
If an I/O error occurs
ShutdownChannelGroupException:
If the channel group is shutdown
openback to summary
public static AsynchronousServerSocketChannel open() throws IOException

Opens an asynchronous server-socket channel.

This method returns an asynchronous server socket channel that is bound to the default group. This method is equivalent to evaluating the expression:

open((AsynchronousChannelGroup)null);
open((AsynchronousChannelGroup)null);
Returns:AsynchronousServerSocketChannel

A new asynchronous server socket channel

Exceptions
IOException:
If an I/O error occurs
providerback to summary
public final AsynchronousChannelProvider provider()

Returns the provider that created this channel.

Returns:AsynchronousChannelProvider

The provider that created this channel

setOptionback to summary
public abstract <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException

Redeclares java.nio.channels.NetworkChannel.setOption.

Doc from java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

Exceptions
IOException:
If an I/O error occurs
IllegalArgumentException:
If the value is not a valid value for this socket option
ClosedChannelException:
If this channel is closed