Package java.nio.channels
Class ServerSocketChannel
- java.lang.Object
-
- java.nio.channels.spi.AbstractInterruptibleChannel
-
- java.nio.channels.SelectableChannel
-
- java.nio.channels.spi.AbstractSelectableChannel
-
- java.nio.channels.ServerSocketChannel
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,InterruptibleChannel
public abstract class ServerSocketChannel extends AbstractSelectableChannel
AServerSocketChannelis a partial abstraction of a selectable, stream-oriented listening socket. Binding and manipulation of socket options can only be done through the associatedServerSocketobject, returned by callingsocket(). ServerSocketChannels can not be constructed for an already existing server-socket, nor can aSocketImplbe assigned.A server-socket channel is open but not bound when created by the
open()method. Callingacceptbefore bound will cause aNotYetBoundException. It can be bound by calling the bind method of a relatedServerSocketinstance.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedServerSocketChannel(SelectorProvider selectorProvider)Constructs a newServerSocketChannel.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract SocketChannelaccept()Accepts a connection to this server-socket channel.static ServerSocketChannelopen()Creates an open and unbound server-socket channel.abstract ServerSocketsocket()Return the server-socket assigned this channel, which does not declare any public methods that are not declared inServerSocket.intvalidOps()Gets the valid operations of this channel.-
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, register
-
Methods inherited from class java.nio.channels.SelectableChannel
register
-
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
-
-
-
-
Constructor Detail
-
ServerSocketChannel
protected ServerSocketChannel(SelectorProvider selectorProvider)
Constructs a newServerSocketChannel.- Parameters:
selectorProvider- an instance of SelectorProvider.
-
-
Method Detail
-
open
public static ServerSocketChannel open() throws IOException
Creates an open and unbound server-socket channel.This channel is created by calling
openServerSocketChannelmethod of the defaultSelectorProviderinstance.- Returns:
- the new channel which is open but unbound.
- Throws:
IOException- if an I/O error occurs.
-
validOps
public final int validOps()
Gets the valid operations of this channel. Server-socket channels support accepting operation, so this method returnsSelectionKey.OP_ACCEPT.- Specified by:
validOpsin classSelectableChannel- Returns:
- the operations supported by this channel.
- See Also:
SelectableChannel.validOps()
-
socket
public abstract ServerSocket socket()
Return the server-socket assigned this channel, which does not declare any public methods that are not declared inServerSocket.- Returns:
- the server-socket assigned to this channel.
-
accept
public abstract SocketChannel accept() throws IOException
Accepts a connection to this server-socket channel.This method returns
nullwhen this channel is non-blocking and no connection is available, otherwise it blocks until a new connection is available or an I/O error occurs. The socket channel returned by this method will always be in blocking mode.This method just executes the same security checks as the
accept()method of theServerSocketclass.- Returns:
- the accepted
SocketChannelinstance, ornullif the channel is non-blocking and no connection is available. - Throws:
AsynchronousCloseException- if this channel is closed by another thread while this method is in operation.ClosedByInterruptException- if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed.ClosedChannelException- if this channel is closed.IOException- if another I/O error occurs.NotYetBoundException- if the socket has not yet been bound.
-
-