- 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
openmethod 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 thebindmethod. Once bound, theacceptmethod 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 aNotYetBoundExceptionto 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
AcceptPendingExceptionwill be thrown.Socket options are configured using the
setOptionmethod. Channels of this type support the following options:
Additional (implementation specific) options may also be supported.Option Name Description SO_RCVBUFThe size of the socket receive buffer SO_REUSEADDRRe-use address 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
-
-
Constructor Summary
Constructors Modifier Constructor and Description protectedAsynchronousServerSocketChannel(AsynchronousChannelProvider provider)Initializes a new instance of this class.
-
Method Summary
Methods Modifier and Type Method and Description abstract Future<AsynchronousSocketChannel>accept()Accepts a connection.abstract <A> voidaccept(A attachment, CompletionHandler<AsynchronousSocketChannel,? super A> handler)Accepts a connection.AsynchronousServerSocketChannelbind(SocketAddress local)Binds the channel's socket to a local address and configures the socket to listen for connections.abstract AsynchronousServerSocketChannelbind(SocketAddress local, int backlog)Binds the channel's socket to a local address and configures the socket to listen for connections.static AsynchronousServerSocketChannelopen()Opens an asynchronous server-socket channel.static AsynchronousServerSocketChannelopen(AsynchronousChannelGroup group)Opens an asynchronous server-socket channel.AsynchronousChannelProviderprovider()Returns the provider that created this channel.abstract <T> AsynchronousServerSocketChannelsetOption(SocketOption<T> name, T value)Sets the value of a socket option.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.nio.channels.AsynchronousChannel
close
-
Methods inherited from interface java.nio.channels.NetworkChannel
getLocalAddress, getOption, supportedOptions
-
Methods inherited from interface java.nio.channels.Channel
isOpen
-
-
-
-
Constructor Detail
-
AsynchronousServerSocketChannel
protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider)
Initializes a new instance of this class.
-
-
Method Detail
-
provider
public final AsynchronousChannelProvider provider()
Returns the provider that created this channel.
-
open
public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group) throws IOException
Opens an asynchronous server-socket channel.The new channel is created by invoking the
openAsynchronousServerSocketChannelmethod on theAsynchronousChannelProviderobject 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- The group to which the newly constructed channel should be bound, or null for the default group- Returns:
- A new asynchronous server socket channel
- Throws:
ShutdownChannelGroupException- If the channel group is shutdownIOException- If an I/O error occurs
-
open
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);
- Returns:
- A new asynchronous server socket channel
- Throws:
IOException- If an I/O error occurs
-
bind
public final AsynchronousServerSocketChannel bind(SocketAddress local) throws IOException
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);
- Specified by:
bindin interfaceNetworkChannel- Parameters:
local- The local address to bind the socket, or null to bind to an automatically assigned socket address- Returns:
- This channel
- Throws:
AlreadyBoundException- If the socket is already boundUnsupportedAddressTypeException- If the type of the given address is not supportedSecurityException- If a security manager is installed and it denies an unspecified permission. An implementation of this interface should specify any required permissions.ClosedChannelException- If the channel is closedIOException- If some other I/O error occurs- See Also:
NetworkChannel.getLocalAddress()
-
bind
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
backlogparameter 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 altogther. If thebacklogparameter has the value0, or a negative value, then an implementation specific default is used.- Parameters:
local- The local address to bind the socket, ornullto bind to an automatically assigned socket addressbacklog- The maximum number of pending connections- Returns:
- This channel
- Throws:
AlreadyBoundException- If the socket is already boundUnsupportedAddressTypeException- If the type of the given address is not supportedSecurityException- If a security manager has been installed and itscheckListenmethod denies the operationClosedChannelException- If the channel is closedIOException- If some other I/O error occurs
-
setOption
public abstract <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value) throws IOException
Description copied from interface:NetworkChannelSets the value of a socket option.- Specified by:
setOptionin interfaceNetworkChannel- Parameters:
name- The socket optionvalue- The value of the socket option. A value ofnullmay be a valid value for some socket options.- Returns:
- This channel
- Throws:
IllegalArgumentException- If the value is not a valid value for this socket optionClosedChannelException- If this channel is closedIOException- If an I/O error occurs- See Also:
StandardSocketOptions
-
accept
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
handlerparameter is a completion handler that is invoked when a connection is accepted (or the operation fails). The result passed to the completion handler is theAsynchronousSocketChannelto the new connection.When a new connection is accepted then the resulting
AsynchronousSocketChannelwill be bound to the sameAsynchronousChannelGroupas this channel. If the group isshutdownand a connection is accepted, then the connection is closed, and the operation completes with anIOExceptionand causeShutdownChannelGroupException.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).
If a security manager has been installed then it verifies that the address and port number of the connection's remote endpoint are permitted by the security manager's
checkAcceptmethod. The permission check is performed with privileges that are restricted by the calling context of this method. If the permission check fails then the connection is closed and the operation completes with aSecurityException.- Parameters:
attachment- The object to attach to the I/O operation; can benullhandler- The handler for consuming the result- Throws:
AcceptPendingException- If an accept operation is already in progress on this channelNotYetBoundException- If this channel's socket has not yet been boundShutdownChannelGroupException- If the channel group has terminated
-
accept
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 aFuturerepresenting the pending result. TheFuture'sgetmethod returns theAsynchronousSocketChannelto the new connection on successful completion.- Returns:
- a
Futureobject representing the pending result - Throws:
AcceptPendingException- If an accept operation is already in progress on this channelNotYetBoundException- If this channel's socket has not yet been bound
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
24/10/2025 06:08:40 Cette version de la page est en cache (à la date du 24/10/2025 06:08:40) afin d'accélérer le traitement.Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.
Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/nio/channels/asynchronousserversocketchannel.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.