ServerSocketChannel (Java Platform SE 8 ) (original) (raw)
- java.nio.channels.spi.AbstractInterruptibleChannel
- java.nio.channels.SelectableChannel
- java.nio.channels.spi.AbstractSelectableChannel
* * java.nio.channels.ServerSocketChannel
- java.nio.channels.spi.AbstractSelectableChannel
- java.nio.channels.SelectableChannel
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, InterruptibleChannel, NetworkChannel
public abstract class ServerSocketChannel
extends AbstractSelectableChannel
implements NetworkChannel
A selectable channel for stream-oriented listening sockets.
A server-socket channel is created by invoking the open method of this class. It is not possible to create a channel for an arbitrary, pre-existing ServerSocket. A newly-created server-socket channel is open but not yet bound. An attempt to invoke the accept method of an unbound server-socket channel will cause a NotYetBoundException to be thrown. A server-socket channel can be bound by invoking one of thebind methods defined by this class.
Socket options are configured using the setOption method. Server-socket channels support the following 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. Server-socket channels are safe for use by multiple concurrent threads. Since: 1.4
Constructor Summary
Constructors
Modifier Constructor Description protected ServerSocketChannel(SelectorProvider provider) Initializes a new instance of this class. Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
Modifier and Type Method Description abstract SocketChannel accept() Accepts a connection made to this channel's socket. ServerSocketChannel bind(SocketAddress local) Binds the channel's socket to a local address and configures the socket to listen for connections. abstract ServerSocketChannel bind(SocketAddress local, int backlog) Binds the channel's socket to a local address and configures the socket to listen for connections. abstract SocketAddress getLocalAddress() Returns the socket address that this channel's socket is bound to. static ServerSocketChannel open() Opens a server-socket channel. abstract ServerSocketChannel setOption(SocketOption name, T value) Sets the value of a socket option. abstract ServerSocket socket() Retrieves a server socket associated with this channel. int validOps() Returns an operation set identifying this channel's supported operations. * ### Methods inherited from class java.nio.channels.spi.[AbstractSelectableChannel](../../../java/nio/channels/spi/AbstractSelectableChannel.html "class in java.nio.channels.spi") `[blockingLock](../../../java/nio/channels/spi/AbstractSelectableChannel.html#blockingLock--), [configureBlocking](../../../java/nio/channels/spi/AbstractSelectableChannel.html#configureBlocking-boolean-), [implCloseChannel](../../../java/nio/channels/spi/AbstractSelectableChannel.html#implCloseChannel--), [implCloseSelectableChannel](../../../java/nio/channels/spi/AbstractSelectableChannel.html#implCloseSelectableChannel--), [implConfigureBlocking](../../../java/nio/channels/spi/AbstractSelectableChannel.html#implConfigureBlocking-boolean-), [isBlocking](../../../java/nio/channels/spi/AbstractSelectableChannel.html#isBlocking--), [isRegistered](../../../java/nio/channels/spi/AbstractSelectableChannel.html#isRegistered--), [keyFor](../../../java/nio/channels/spi/AbstractSelectableChannel.html#keyFor-java.nio.channels.Selector-), [provider](../../../java/nio/channels/spi/AbstractSelectableChannel.html#provider--), [register](../../../java/nio/channels/spi/AbstractSelectableChannel.html#register-java.nio.channels.Selector-int-java.lang.Object-)` * ### Methods inherited from class java.nio.channels.[SelectableChannel](../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels") `[register](../../../java/nio/channels/SelectableChannel.html#register-java.nio.channels.Selector-int-)` * ### Methods inherited from class java.nio.channels.spi.[AbstractInterruptibleChannel](../../../java/nio/channels/spi/AbstractInterruptibleChannel.html "class in java.nio.channels.spi") `[begin](../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#begin--), [close](../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#close--), [end](../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#end-boolean-), [isOpen](../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#isOpen--)` * ### Methods inherited from class java.lang.[Object](../../../java/lang/Object.html "class in java.lang") `[clone](../../../java/lang/Object.html#clone--), [equals](../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../java/lang/Object.html#finalize--), [getClass](../../../java/lang/Object.html#getClass--), [hashCode](../../../java/lang/Object.html#hashCode--), [notify](../../../java/lang/Object.html#notify--), [notifyAll](../../../java/lang/Object.html#notifyAll--), [toString](../../../java/lang/Object.html#toString--), [wait](../../../java/lang/Object.html#wait--), [wait](../../../java/lang/Object.html#wait-long-), [wait](../../../java/lang/Object.html#wait-long-int-)` * ### Methods inherited from interface java.nio.channels.[NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels") `[getOption](../../../java/nio/channels/NetworkChannel.html#getOption-java.net.SocketOption-), [supportedOptions](../../../java/nio/channels/NetworkChannel.html#supportedOptions--)` * ### Methods inherited from interface java.nio.channels.[Channel](../../../java/nio/channels/Channel.html "interface in java.nio.channels") `[close](../../../java/nio/channels/Channel.html#close--), [isOpen](../../../java/nio/channels/Channel.html#isOpen--)`
Constructor Detail
* #### ServerSocketChannel protected ServerSocketChannel([SelectorProvider](../../../java/nio/channels/spi/SelectorProvider.html "class in java.nio.channels.spi") provider) Initializes a new instance of this class. Parameters: `provider` \- The provider that created this channel
Method Detail
* #### open public static [ServerSocketChannel](../../../java/nio/channels/ServerSocketChannel.html "class in java.nio.channels") open() throws [IOException](../../../java/io/IOException.html "class in java.io") Opens a server-socket channel. The new channel is created by invoking the [openServerSocketChannel](../../../java/nio/channels/spi/SelectorProvider.html#openServerSocketChannel--) method of the system-wide default [SelectorProvider](../../../java/nio/channels/spi/SelectorProvider.html "class in java.nio.channels.spi") object. The new channel's socket is initially unbound; it must be bound to a specific address via one of its socket's [bind](../../../java/net/ServerSocket.html#bind-java.net.SocketAddress-) methods before connections can be accepted. Returns: A new socket channel Throws: `[IOException](../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs * #### 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](../../../java/nio/channels/SelectionKey.html#OP%5FACCEPT). Specified by: `[validOps](../../../java/nio/channels/SelectableChannel.html#validOps--)` in class `[SelectableChannel](../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Returns: The valid-operation set * #### bind public final [ServerSocketChannel](../../../java/nio/channels/ServerSocketChannel.html "class in java.nio.channels") bind([SocketAddress](../../../java/net/SocketAddress.html "class in java.net") local) throws [IOException](../../../java/io/IOException.html "class in java.io") 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: `[bind](../../../java/nio/channels/NetworkChannel.html#bind-java.net.SocketAddress-)` in interface `[NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")` Parameters: `local` \- The local address to bind the socket, or `null` to bind to an automatically assigned socket address Returns: This channel Throws: `[AlreadyBoundException](../../../java/nio/channels/AlreadyBoundException.html "class in java.nio.channels")` \- If the socket is already bound `[UnsupportedAddressTypeException](../../../java/nio/channels/UnsupportedAddressTypeException.html "class in java.nio.channels")` \- If the type of the given address is not supported `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If the channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs `[SecurityException](../../../java/lang/SecurityException.html "class in java.lang")` \- If a security manager has been installed and its [checkListen](../../../java/lang/SecurityManager.html#checkListen-int-) method denies the operation Since: 1.7 See Also: [NetworkChannel.getLocalAddress()](../../../java/nio/channels/NetworkChannel.html#getLocalAddress--) * #### bind public abstract [ServerSocketChannel](../../../java/nio/channels/ServerSocketChannel.html "class in java.nio.channels") bind([SocketAddress](../../../java/net/SocketAddress.html "class in java.net") local, int backlog) throws [IOException](../../../java/io/IOException.html "class in java.io") 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 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 altogther. If the `backlog` parameter has the value `0`, or a negative value, then an implementation specific default is used. Parameters: `local` \- The address to bind the socket, or `null` to bind to an automatically assigned socket address `backlog` \- The maximum number of pending connections Returns: This channel Throws: `[AlreadyBoundException](../../../java/nio/channels/AlreadyBoundException.html "class in java.nio.channels")` \- If the socket is already bound `[UnsupportedAddressTypeException](../../../java/nio/channels/UnsupportedAddressTypeException.html "class in java.nio.channels")` \- If the type of the given address is not supported `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs `[SecurityException](../../../java/lang/SecurityException.html "class in java.lang")` \- If a security manager has been installed and its [checkListen](../../../java/lang/SecurityManager.html#checkListen-int-) method denies the operation Since: 1.7 * #### setOption public abstract <T> [ServerSocketChannel](../../../java/nio/channels/ServerSocketChannel.html "class in java.nio.channels") setOption([SocketOption](../../../java/net/SocketOption.html "interface in java.net")<T> name, T value) throws [IOException](../../../java/io/IOException.html "class in java.io") Sets the value of a socket option. Specified by: `[setOption](../../../java/nio/channels/NetworkChannel.html#setOption-java.net.SocketOption-T-)` in interface `[NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")` Type Parameters: `T` \- The type of the socket option value Parameters: `name` \- The socket option `value` \- The value of the socket option. A value of `null` may be a valid value for some socket options. Returns: This channel Throws: `[UnsupportedOperationException](../../../java/lang/UnsupportedOperationException.html "class in java.lang")` \- If the socket option is not supported by this channel `[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- If the value is not a valid value for this socket option `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs Since: 1.7 See Also: [StandardSocketOptions](../../../java/net/StandardSocketOptions.html "class in java.net") * #### socket public abstract [ServerSocket](../../../java/net/ServerSocket.html "class in java.net") 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](../../../java/net/ServerSocket.html "class in java.net") class. Returns: A server socket associated with this channel * #### accept public abstract [SocketChannel](../../../java/nio/channels/SocketChannel.html "class in java.nio.channels") accept() throws [IOException](../../../java/io/IOException.html "class in java.io") 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](../../../java/net/ServerSocket.html#accept--) method of the [ServerSocket](../../../java/net/ServerSocket.html "class in java.net") 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](../../../java/lang/SecurityManager.html#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](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[AsynchronousCloseException](../../../java/nio/channels/AsynchronousCloseException.html "class in java.nio.channels")` \- If another thread closes this channel while the accept operation is in progress `[ClosedByInterruptException](../../../java/nio/channels/ClosedByInterruptException.html "class in java.nio.channels")` \- 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](../../../java/nio/channels/NotYetBoundException.html "class in java.nio.channels")` \- If this channel's socket has not yet been bound `[SecurityException](../../../java/lang/SecurityException.html "class in java.lang")` \- If a security manager has been installed and it does not permit access to the remote endpoint of the new connection `[IOException](../../../java/io/IOException.html "class in java.io")` \- If some other I/O error occurs * #### getLocalAddress public abstract [SocketAddress](../../../java/net/SocketAddress.html "class in java.net") getLocalAddress() throws [IOException](../../../java/io/IOException.html "class in java.io") Returns the socket address that this channel's socket is bound to. Where the channel is [bound](../../../java/nio/channels/NetworkChannel.html#bind-java.net.SocketAddress-) to an Internet Protocol socket address then the return value from this method is of type [InetSocketAddress](../../../java/net/InetSocketAddress.html "class in java.net"). If there is a security manager set, its `checkConnect` method is called with the local address and `-1` as its arguments to see if the operation is allowed. If the operation is not allowed, a `SocketAddress` representing the[loopback](../../../java/net/InetAddress.html#getLoopbackAddress--) address and the local port of the channel's socket is returned. Specified by: `[getLocalAddress](../../../java/nio/channels/NetworkChannel.html#getLocalAddress--)` in interface `[NetworkChannel](../../../java/nio/channels/NetworkChannel.html "interface in java.nio.channels")` Returns: The `SocketAddress` that the socket is bound to, or the`SocketAddress` representing the loopback address if denied by the security manager, or `null` if the channel's socket is not bound Throws: `[ClosedChannelException](../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If the channel is closed `[IOException](../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.