Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.io.IOException;28import java.net.ServerSocket;29import java.net.SocketOption;30import java.net.SocketAddress;31import java.nio.channels.spi.AbstractSelectableChannel;32import java.nio.channels.spi.SelectorProvider;3334/**35* A selectable channel for stream-oriented listening sockets.36*37* <p> A server-socket channel is created by invoking the {@link #open() open}38* method of this class. It is not possible to create a channel for an arbitrary,39* pre-existing {@link ServerSocket}. A newly-created server-socket channel is40* open but not yet bound. An attempt to invoke the {@link #accept() accept}41* method of an unbound server-socket channel will cause a {@link NotYetBoundException}42* to be thrown. A server-socket channel can be bound by invoking one of the43* {@link #bind(java.net.SocketAddress,int) bind} methods defined by this class.44*45* <p> Socket options are configured using the {@link #setOption(SocketOption,Object)46* setOption} method. Server-socket channels support the following options:47* <blockquote>48* <table border summary="Socket options">49* <tr>50* <th>Option Name</th>51* <th>Description</th>52* </tr>53* <tr>54* <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>55* <td> The size of the socket receive buffer </td>56* </tr>57* <tr>58* <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>59* <td> Re-use address </td>60* </tr>61* </table>62* </blockquote>63* Additional (implementation specific) options may also be supported.64*65* <p> Server-socket channels are safe for use by multiple concurrent threads.66* </p>67*68* @author Mark Reinhold69* @author JSR-51 Expert Group70* @since 1.471*/7273public abstract class ServerSocketChannel74extends AbstractSelectableChannel75implements NetworkChannel76{7778/**79* Initializes a new instance of this class.80*81* @param provider82* The provider that created this channel83*/84protected ServerSocketChannel(SelectorProvider provider) {85super(provider);86}8788/**89* Opens a server-socket channel.90*91* <p> The new channel is created by invoking the {@link92* java.nio.channels.spi.SelectorProvider#openServerSocketChannel93* openServerSocketChannel} method of the system-wide default {@link94* java.nio.channels.spi.SelectorProvider} object.95*96* <p> The new channel's socket is initially unbound; it must be bound to a97* specific address via one of its socket's {@link98* java.net.ServerSocket#bind(SocketAddress) bind} methods before99* connections can be accepted. </p>100*101* @return A new socket channel102*103* @throws IOException104* If an I/O error occurs105*/106public static ServerSocketChannel open() throws IOException {107return SelectorProvider.provider().openServerSocketChannel();108}109110/**111* Returns an operation set identifying this channel's supported112* operations.113*114* <p> Server-socket channels only support the accepting of new115* connections, so this method returns {@link SelectionKey#OP_ACCEPT}.116* </p>117*118* @return The valid-operation set119*/120public final int validOps() {121return SelectionKey.OP_ACCEPT;122}123124125// -- ServerSocket-specific operations --126127/**128* Binds the channel's socket to a local address and configures the socket129* to listen for connections.130*131* <p> An invocation of this method is equivalent to the following:132* <blockquote><pre>133* bind(local, 0);134* </pre></blockquote>135*136* @param local137* The local address to bind the socket, or {@code null} to bind138* to an automatically assigned socket address139*140* @return This channel141*142* @throws AlreadyBoundException {@inheritDoc}143* @throws UnsupportedAddressTypeException {@inheritDoc}144* @throws ClosedChannelException {@inheritDoc}145* @throws IOException {@inheritDoc}146* @throws SecurityException147* If a security manager has been installed and its {@link148* SecurityManager#checkListen checkListen} method denies the149* operation150*151* @since 1.7152*/153public final ServerSocketChannel bind(SocketAddress local)154throws IOException155{156return bind(local, 0);157}158159/**160* Binds the channel's socket to a local address and configures the socket to161* listen for connections.162*163* <p> This method is used to establish an association between the socket and164* a local address. Once an association is established then the socket remains165* bound until the channel is closed.166*167* <p> The {@code backlog} parameter is the maximum number of pending168* connections on the socket. Its exact semantics are implementation specific.169* In particular, an implementation may impose a maximum length or may choose170* to ignore the parameter altogther. If the {@code backlog} parameter has171* the value {@code 0}, or a negative value, then an implementation specific172* default is used.173*174* @param local175* The address to bind the socket, or {@code null} to bind to an176* automatically assigned socket address177* @param backlog178* The maximum number of pending connections179*180* @return This channel181*182* @throws AlreadyBoundException183* If the socket is already bound184* @throws UnsupportedAddressTypeException185* If the type of the given address is not supported186* @throws ClosedChannelException187* If this channel is closed188* @throws IOException189* If some other I/O error occurs190* @throws SecurityException191* If a security manager has been installed and its {@link192* SecurityManager#checkListen checkListen} method denies the193* operation194*195* @since 1.7196*/197public abstract ServerSocketChannel bind(SocketAddress local, int backlog)198throws IOException;199200/**201* @throws UnsupportedOperationException {@inheritDoc}202* @throws IllegalArgumentException {@inheritDoc}203* @throws ClosedChannelException {@inheritDoc}204* @throws IOException {@inheritDoc}205*206* @since 1.7207*/208public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value)209throws IOException;210211/**212* Retrieves a server socket associated with this channel.213*214* <p> The returned object will not declare any public methods that are not215* declared in the {@link java.net.ServerSocket} class. </p>216*217* @return A server socket associated with this channel218*/219public abstract ServerSocket socket();220221/**222* Accepts a connection made to this channel's socket.223*224* <p> If this channel is in non-blocking mode then this method will225* immediately return <tt>null</tt> if there are no pending connections.226* Otherwise it will block indefinitely until a new connection is available227* or an I/O error occurs.228*229* <p> The socket channel returned by this method, if any, will be in230* blocking mode regardless of the blocking mode of this channel.231*232* <p> This method performs exactly the same security checks as the {@link233* java.net.ServerSocket#accept accept} method of the {@link234* java.net.ServerSocket} class. That is, if a security manager has been235* installed then for each new connection this method verifies that the236* address and port number of the connection's remote endpoint are237* permitted by the security manager's {@link238* java.lang.SecurityManager#checkAccept checkAccept} method. </p>239*240* @return The socket channel for the new connection,241* or <tt>null</tt> if this channel is in non-blocking mode242* and no connection is available to be accepted243*244* @throws ClosedChannelException245* If this channel is closed246*247* @throws AsynchronousCloseException248* If another thread closes this channel249* while the accept operation is in progress250*251* @throws ClosedByInterruptException252* If another thread interrupts the current thread253* while the accept operation is in progress, thereby254* closing the channel and setting the current thread's255* interrupt status256*257* @throws NotYetBoundException258* If this channel's socket has not yet been bound259*260* @throws SecurityException261* If a security manager has been installed262* and it does not permit access to the remote endpoint263* of the new connection264*265* @throws IOException266* If some other I/O error occurs267*/268public abstract SocketChannel accept() throws IOException;269270/**271* {@inheritDoc}272* <p>273* If there is a security manager set, its {@code checkConnect} method is274* called with the local address and {@code -1} as its arguments to see275* if the operation is allowed. If the operation is not allowed,276* a {@code SocketAddress} representing the277* {@link java.net.InetAddress#getLoopbackAddress loopback} address and the278* local port of the channel's socket is returned.279*280* @return The {@code SocketAddress} that the socket is bound to, or the281* {@code SocketAddress} representing the loopback address if282* denied by the security manager, or {@code null} if the283* channel's socket is not bound284*285* @throws ClosedChannelException {@inheritDoc}286* @throws IOException {@inheritDoc}287*/288@Override289public abstract SocketAddress getLocalAddress() throws IOException;290291}292293294