0N/A/*
2362N/A * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.nio.channels;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.net.ServerSocket;
524N/Aimport java.net.SocketOption;
0N/Aimport java.net.SocketAddress;
893N/Aimport java.nio.channels.spi.AbstractSelectableChannel;
893N/Aimport java.nio.channels.spi.SelectorProvider;
0N/A
0N/A/**
0N/A * A selectable channel for stream-oriented listening sockets.
0N/A *
524N/A * <p> A server-socket channel is created by invoking the {@link #open() open}
524N/A * method of this class. It is not possible to create a channel for an arbitrary,
524N/A * pre-existing {@link ServerSocket}. A newly-created server-socket channel is
524N/A * open but not yet bound. An attempt to invoke the {@link #accept() accept}
524N/A * method of an unbound server-socket channel will cause a {@link NotYetBoundException}
524N/A * to be thrown. A server-socket channel can be bound by invoking one of the
524N/A * {@link #bind(java.net.SocketAddress,int) bind} methods defined by this class.
0N/A *
524N/A * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
524N/A * setOption} method. Server-socket channels support the following options:
524N/A * <blockquote>
524N/A * <table border>
524N/A * <tr>
524N/A * <th>Option Name</th>
524N/A * <th>Description</th>
524N/A * </tr>
524N/A * <tr>
4216N/A * <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>
524N/A * <td> The size of the socket receive buffer </td>
524N/A * </tr>
524N/A * <tr>
4216N/A * <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>
524N/A * <td> Re-use address </td>
524N/A * </tr>
524N/A * </table>
524N/A * </blockquote>
524N/A * Additional (implementation specific) options may also be supported.
0N/A *
0N/A * <p> Server-socket channels are safe for use by multiple concurrent threads.
0N/A * </p>
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic abstract class ServerSocketChannel
0N/A extends AbstractSelectableChannel
524N/A implements NetworkChannel
0N/A{
0N/A
0N/A /**
0N/A * Initializes a new instance of this class.
0N/A */
0N/A protected ServerSocketChannel(SelectorProvider provider) {
0N/A super(provider);
0N/A }
0N/A
0N/A /**
0N/A * Opens a server-socket channel.
0N/A *
0N/A * <p> The new channel is created by invoking the {@link
0N/A * java.nio.channels.spi.SelectorProvider#openServerSocketChannel
0N/A * openServerSocketChannel} method of the system-wide default {@link
0N/A * java.nio.channels.spi.SelectorProvider} object.
0N/A *
0N/A * <p> The new channel's socket is initially unbound; it must be bound to a
0N/A * specific address via one of its socket's {@link
0N/A * java.net.ServerSocket#bind(SocketAddress) bind} methods before
0N/A * connections can be accepted. </p>
0N/A *
0N/A * @return A new socket channel
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A */
0N/A public static ServerSocketChannel open() throws IOException {
0N/A return SelectorProvider.provider().openServerSocketChannel();
0N/A }
0N/A
0N/A /**
0N/A * Returns an operation set identifying this channel's supported
0N/A * operations.
0N/A *
0N/A * <p> Server-socket channels only support the accepting of new
0N/A * connections, so this method returns {@link SelectionKey#OP_ACCEPT}.
0N/A * </p>
0N/A *
0N/A * @return The valid-operation set
0N/A */
0N/A public final int validOps() {
0N/A return SelectionKey.OP_ACCEPT;
0N/A }
0N/A
0N/A
0N/A // -- ServerSocket-specific operations --
0N/A
0N/A /**
524N/A * Binds the channel's socket to a local address and configures the socket
524N/A * to listen for connections.
524N/A *
524N/A * <p> An invocation of this method is equivalent to the following:
524N/A * <blockquote><pre>
524N/A * bind(local, 0);
524N/A * </pre></blockquote>
524N/A *
524N/A * @param local
524N/A * The local address to bind the socket, or {@code null} to bind
524N/A * to an automatically assigned socket address
524N/A *
524N/A * @return This channel
524N/A *
524N/A * @throws AlreadyBoundException {@inheritDoc}
524N/A * @throws UnsupportedAddressTypeException {@inheritDoc}
524N/A * @throws ClosedChannelException {@inheritDoc}
524N/A * @throws IOException {@inheritDoc}
524N/A * @throws SecurityException
524N/A * If a security manager has been installed and its {@link
524N/A * SecurityManager#checkListen checkListen} method denies the
524N/A * operation
524N/A *
524N/A * @since 1.7
524N/A */
524N/A public final ServerSocketChannel bind(SocketAddress local)
524N/A throws IOException
524N/A {
524N/A return bind(local, 0);
524N/A }
524N/A
524N/A /**
524N/A * Binds the channel's socket to a local address and configures the socket to
524N/A * listen for connections.
524N/A *
524N/A * <p> This method is used to establish an association between the socket and
524N/A * a local address. Once an association is established then the socket remains
524N/A * bound until the channel is closed.
524N/A *
524N/A * <p> The {@code backlog} parameter is the maximum number of pending
524N/A * connections on the socket. Its exact semantics are implementation specific.
524N/A * In particular, an implementation may impose a maximum length or may choose
524N/A * to ignore the parameter altogther. If the {@code backlog} parameter has
524N/A * the value {@code 0}, or a negative value, then an implementation specific
524N/A * default is used.
524N/A *
524N/A * @param local
524N/A * The address to bind the socket, or {@code null} to bind to an
524N/A * automatically assigned socket address
524N/A * @param backlog
524N/A * The maximum number of pending connections
524N/A *
524N/A * @return This channel
524N/A *
524N/A * @throws AlreadyBoundException
524N/A * If the socket is already bound
524N/A * @throws UnsupportedAddressTypeException
524N/A * If the type of the given address is not supported
524N/A * @throws ClosedChannelException
524N/A * If this channel is closed
524N/A * @throws IOException
524N/A * If some other I/O error occurs
524N/A * @throws SecurityException
524N/A * If a security manager has been installed and its {@link
524N/A * SecurityManager#checkListen checkListen} method denies the
524N/A * operation
524N/A *
524N/A * @since 1.7
524N/A */
524N/A public abstract ServerSocketChannel bind(SocketAddress local, int backlog)
524N/A throws IOException;
524N/A
524N/A /**
893N/A * @throws UnsupportedOperationException {@inheritDoc}
524N/A * @throws IllegalArgumentException {@inheritDoc}
524N/A * @throws ClosedChannelException {@inheritDoc}
524N/A * @throws IOException {@inheritDoc}
524N/A *
524N/A * @since 1.7
524N/A */
524N/A public abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
524N/A throws IOException;
524N/A
524N/A /**
0N/A * Retrieves a server socket associated with this channel.
0N/A *
0N/A * <p> The returned object will not declare any public methods that are not
0N/A * declared in the {@link java.net.ServerSocket} class. </p>
0N/A *
0N/A * @return A server socket associated with this channel
0N/A */
0N/A public abstract ServerSocket socket();
0N/A
0N/A /**
0N/A * Accepts a connection made to this channel's socket.
0N/A *
0N/A * <p> If this channel is in non-blocking mode then this method will
0N/A * immediately return <tt>null</tt> if there are no pending connections.
0N/A * Otherwise it will block indefinitely until a new connection is available
0N/A * or an I/O error occurs.
0N/A *
0N/A * <p> The socket channel returned by this method, if any, will be in
0N/A * blocking mode regardless of the blocking mode of this channel.
0N/A *
0N/A * <p> This method performs exactly the same security checks as the {@link
0N/A * java.net.ServerSocket#accept accept} method of the {@link
0N/A * java.net.ServerSocket} class. That is, if a security manager has been
0N/A * installed then for each new connection this method verifies that the
0N/A * address and port number of the connection's remote endpoint are
0N/A * permitted by the security manager's {@link
0N/A * java.lang.SecurityManager#checkAccept checkAccept} method. </p>
0N/A *
0N/A * @return The socket channel for the new connection,
0N/A * or <tt>null</tt> if this channel is in non-blocking mode
0N/A * and no connection is available to be accepted
0N/A *
0N/A * @throws ClosedChannelException
0N/A * If this channel is closed
0N/A *
0N/A * @throws AsynchronousCloseException
0N/A * If another thread closes this channel
0N/A * while the accept operation is in progress
0N/A *
0N/A * @throws ClosedByInterruptException
0N/A * If another thread interrupts the current thread
0N/A * while the accept operation is in progress, thereby
0N/A * closing the channel and setting the current thread's
0N/A * interrupt status
0N/A *
0N/A * @throws NotYetBoundException
0N/A * If this channel's socket has not yet been bound
0N/A *
0N/A * @throws SecurityException
0N/A * If a security manager has been installed
0N/A * and it does not permit access to the remote endpoint
0N/A * of the new connection
0N/A *
0N/A * @throws IOException
0N/A * If some other I/O error occurs
0N/A */
0N/A public abstract SocketChannel accept() throws IOException;
0N/A
0N/A}