jdk/src/share/classes/java/nio/channels/ServerSocketChannel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.SocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A selectable channel for stream-oriented listening sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> Server-socket channels are not a complete abstraction of listening
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * network sockets.  Binding and the manipulation of socket options must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * done through an associated {@link java.net.ServerSocket} object obtained by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * invoking the {@link #socket() socket} method.  It is not possible to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a channel for an arbitrary, pre-existing server socket, nor is it possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to specify the {@link java.net.SocketImpl} object to be used by a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * socket associated with a server-socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> A server-socket channel is created by invoking the {@link #open() open}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * method of this class.  A newly-created server-socket channel is open but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * yet bound.  An attempt to invoke the {@link #accept() accept} method of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * unbound server-socket channel will cause a {@link NotYetBoundException} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * be thrown.  A server-socket channel can be bound by invoking one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * {@link java.net.ServerSocket#bind(java.net.SocketAddress,int) bind} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of an associated server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> Server-socket channels are safe for use by multiple concurrent threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public abstract class ServerSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    extends AbstractSelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected ServerSocketChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Opens a server-socket channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p> The new channel is created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * java.nio.channels.spi.SelectorProvider#openServerSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * openServerSocketChannel} method of the system-wide default {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * java.nio.channels.spi.SelectorProvider} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p> The new channel's socket is initially unbound; it must be bound to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * specific address via one of its socket's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * java.net.ServerSocket#bind(SocketAddress) bind} methods before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * connections can be accepted.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return  A new socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static ServerSocketChannel open() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return SelectorProvider.provider().openServerSocketChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns an operation set identifying this channel's supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p> Server-socket channels only support the accepting of new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * connections, so this method returns {@link SelectionKey#OP_ACCEPT}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return  The valid-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public final int validOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return SelectionKey.OP_ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // -- ServerSocket-specific operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Retrieves a server socket associated with this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p> The returned object will not declare any public methods that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * declared in the {@link java.net.ServerSocket} class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return  A server socket associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public abstract ServerSocket socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Accepts a connection made to this channel's socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p> If this channel is in non-blocking mode then this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * immediately return <tt>null</tt> if there are no pending connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Otherwise it will block indefinitely until a new connection is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p> The socket channel returned by this method, if any, will be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * blocking mode regardless of the blocking mode of this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * java.net.ServerSocket#accept accept} method of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * java.net.ServerSocket} class.  That is, if a security manager has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * installed then for each new connection this method verifies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * address and port number of the connection's remote endpoint are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * permitted by the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * java.lang.SecurityManager#checkAccept checkAccept} method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return  The socket channel for the new connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *          or <tt>null</tt> if this channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          and no connection is available to be accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          while the accept operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          while the accept operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws  NotYetBoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          If this channel's socket has not yet been bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          and it does not permit access to the remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *          of the new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public abstract SocketChannel accept() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}