jdk/src/share/classes/java/net/ServerSocket.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
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 1995-2007 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.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.ServerSocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements server sockets. A server socket waits for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * requests to come in over the network. It performs some operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * based on that request, and then possibly returns a result to the requester.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The actual work of the server socket is performed by an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of the <code>SocketImpl</code> class. An application can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * change the socket factory that creates the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * implementation to configure itself to create sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * appropriate to the local firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see     java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see     java.nio.channels.ServerSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class ServerSocket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The implementation of this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private SocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Are we using an older SocketImpl?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean oldImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Creates an unbound server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @exception IOException IO error when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public ServerSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a server socket, bound to the specified port. A port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * of <code>0</code> means that the port number is automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * allocated, typically from an ephemeral port range. This port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The maximum queue length for incoming connection indications (a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * request to connect) is set to <code>50</code>. If a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * indication arrives when the queue is full, the connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * If the application has specified a server socket factory, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * factory's <code>createSocketImpl</code> method is called to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * its <code>checkListen</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * with the <code>port</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param      port  the port number, or <code>0</code> to use a port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *                   number that is automatically allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception  IOException  if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * if a security manager exists and its <code>checkListen</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see        SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public ServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this(port, 50, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Creates a server socket and binds it to the specified local port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * number, with the specified backlog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * A port number of <code>0</code> means that the port number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * automatically allocated, typically from an ephemeral port range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * This port number can then be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * The maximum queue length for incoming connection indications (a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * request to connect) is set to the <code>backlog</code> parameter. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * a connection indication arrives when the queue is full, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * If the application has specified a server socket factory, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * factory's <code>createSocketImpl</code> method is called to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * its <code>checkListen</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * with the <code>port</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <P>The <code>backlog</code> argument must be a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * value greater than 0. If the value passed is equal or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * than 0, then the default value will be assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param      port     the port number, or <code>0</code> to use a port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *                      number that is automatically allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param      backlog  the maximum length of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception  IOException  if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * if a security manager exists and its <code>checkListen</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see        SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public ServerSocket(int port, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this(port, backlog, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Create a server with the specified port, listen backlog, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * local IP address to bind to.  The <i>bindAddr</i> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * can be used on a multi-homed host for a ServerSocket that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * will only accept connect requests to one of its addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If <i>bindAddr</i> is null, it will default accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * connections on any/all local addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The port must be between 0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * A port number of <code>0</code> means that the port number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * automatically allocated, typically from an ephemeral port range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * This port number can then be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <P>If there is a security manager, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * calls its <code>checkListen</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * with the <code>port</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <P>The <code>backlog</code> argument must be a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * value greater than 0. If the value passed is equal or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * than 0, then the default value will be assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param port  the port number, or <code>0</code> to use a port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *              number that is automatically allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param backlog the listen backlog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param bindAddr the local InetAddress the server will bind to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @throws  SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * its <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws  IOException if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @see SocketOptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @see SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (port < 0 || port > 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                       "Port value out of range: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (backlog < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
          backlog = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            bind(new InetSocketAddress(bindAddr, port), backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Get the <code>SocketImpl</code> attached to this socket, creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return  the <code>SocketImpl</code> attached to that ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @throws SocketException if creation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    SocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private void checkOldImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // SocketImpl.connect() is a protected method, therefore we need to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // getDeclaredMethod, therefore we need permission to access the member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   250
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   251
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   252
                    public Void run() throws NoSuchMethodException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        Class[] cl = new Class[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        cl[0] = SocketAddress.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        cl[1] = Integer.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        impl.getClass().getDeclaredMethod("connect", cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            oldImpl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void setImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            impl = factory.createSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // No need to do a checkOldImpl() here, we know it's an up to date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // SocketImpl!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            impl = new SocksSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (impl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            impl.setServerSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Creates the socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws IOException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void createImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            impl.create(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Binds the <code>ServerSocket</code> to a specific address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param   endpoint        The IP address & port number to bind to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @throws  SecurityException       if a <code>SecurityManager</code> is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * its <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public void bind(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        bind(endpoint, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Binds the <code>ServerSocket</code> to a specific address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * The <code>backlog</code> argument must be a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * value greater than 0. If the value passed is equal or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * than 0, then the default value will be assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param   endpoint        The IP address & port number to bind to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param   backlog         The listen backlog length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws  SecurityException       if a <code>SecurityManager</code> is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * its <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void bind(SocketAddress endpoint, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (!oldImpl && isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (endpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            endpoint = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (!(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (backlog < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
          backlog = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (security != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                security.checkListen(epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            getImpl().bind(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            getImpl().listen(backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Returns the local address of this server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * then this method will continue to return the local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return  the address to which this socket is bound,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *          or <code>null</code> if the socket is unbound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return getImpl().getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the port number on which this socket is listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * then this method will continue to return the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return  the port number to which this socket is listening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *          -1 if the socket is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Returns the address of the endpoint this socket is bound to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * then this method will continue to return the address of the endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return a <code>SocketAddress</code> representing the local endpoint of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *         socket, or <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return new InetSocketAddress(getInetAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Listens for a connection to be made to this socket and accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * it. The method blocks until a connection is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <p>A new Socket <code>s</code> is created and, if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * the security manager's <code>checkAccept</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * with <code>s.getInetAddress().getHostAddress()</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>s.getPort()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * as its arguments to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @exception  IOException  if an I/O error occurs when waiting for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *             <code>checkAccept</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *             the timeout has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *             if this socket has an associated channel, the channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *             non-blocking mode, and there is no connection ready to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *             accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return the new Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @see SecurityManager#checkAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new SocketException("Socket is not bound yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        Socket s = new Socket((SocketImpl) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        implAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Subclasses of ServerSocket use this method to override accept()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * to return their own subclass of socket.  So a FooServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * will typically hand this method an <i>empty</i> FooSocket.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * return from implAccept the FooSocket will be connected to a client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param s the Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @throws java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *         if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *         and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @throws IOException if an I/O error occurs when waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    protected final void implAccept(Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        SocketImpl si = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (s.impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
              s.setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                s.impl.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            si = s.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            s.impl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            si.address = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            si.fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            getImpl().accept(si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                security.checkAccept(si.getInetAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                     si.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (si != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                si.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (si != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                si.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        s.postAccept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Any thread currently blocked in {@link #accept()} will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @exception  IOException  if an I/O error occurs when closing the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * associated with this socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <p> A server socket will have a channel if, and only if, the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * itself was created via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @return  the server-socket channel associated with this socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *          or <tt>null</tt> if this socket was not created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *          for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public ServerSocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Returns the binding state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return true if the ServerSocket succesfuly bound to an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // Before 1.3 ServerSockets were always bound during creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return bound || oldImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the closed state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Enable/disable SO_TIMEOUT with the specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * milliseconds.  With this option set to a non-zero timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * a call to accept() for this ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * ServerSocket is still valid.  The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * prior to entering the blocking operation to have effect.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * timeout must be > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param timeout the specified timeout, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @return the SO_TIMEOUT value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @exception IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public synchronized int getSoTimeout() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Enable/disable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * When a TCP connection is closed the connection may remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * in a timeout state for a period of time after the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * is closed (typically known as the <tt>TIME_WAIT</tt> state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * or <tt>2MSL</tt> wait state).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * For applications using a well known socket address or port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * it may not be possible to bind a socket to the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <tt>SocketAddress</tt> if there is a connection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * timeout state involving the socket address or port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * using {@link #bind(SocketAddress)} allows the socket to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * bound even though a previous connection is in a timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * When a <tt>ServerSocket</tt> is created the initial setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * of <tt>SO_REUSEADDR</tt> is not defined. Applications can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * use {@link #getReuseAddress()} to determine the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * setting of <tt>SO_REUSEADDR</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * disabled after a socket is bound (See {@link #isBound()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param on  whether to enable or disable the socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @exception SocketException if an error occurs enabling or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *            disabling the <tt>SO_RESUEADDR</tt> socket option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *            or the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Tests if SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Returns the implementation address and implementation port of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * this socket as a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return "ServerSocket[unbound]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return "ServerSocket[addr=" + impl.getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                ",port=" + impl.getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                ",localport=" + impl.getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    void setBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    void setCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * The factory for all server sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    private static SocketImplFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Sets the server socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * When an application creates a new server socket, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * implementation factory's <code>createSocketImpl</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * called to create the actual socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Passing <code>null</code> to the method is a no-op unless the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *               socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @exception  SocketException  if the factory has already been defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *             <code>checkSetFactory</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Sets a default proposed value for the SO_RCVBUF option for sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * accepted from this <tt>ServerSocket</tt>. The value actually set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * in the accepted socket must be determined by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * {@link Socket#getReceiveBufferSize()} after the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * is returned by {@link #accept()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * The value of SO_RCVBUF is used both to set the size of the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * socket receive buffer, and to set the size of the TCP receive window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * that is advertized to the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * It is possible to change the value subsequently, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * {@link Socket#setReceiveBufferSize(int)}. However, if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * then the proposed value must be set in the ServerSocket <B>before</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * it is bound to a local address. This implies, that the ServerSocket must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * created with the no-argument constructor, then setReceiveBufferSize() must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * be called and lastly the ServerSocket is bound to an address by calling bind().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Failure to do this will not cause an error, and the buffer size may be set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * requested value but the TCP receive window in sockets accepted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * this ServerSocket will be no larger than 64K bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @exception IllegalArgumentException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * value is 0 or is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @see #getReceiveBufferSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     public synchronized void setReceiveBufferSize (int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            throw new IllegalArgumentException("negative receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Gets the value of the SO_RCVBUF option for this <tt>ServerSocket</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * that is the proposed buffer size that will be used for Sockets accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * from this <tt>ServerSocket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <p>Note, the value actually set in the accepted socket is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * calling {@link Socket#getReceiveBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * Sets performance preferences for this ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * compared, with larger values indicating stronger preferences.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * bandwidth, for example, then it could invoke this method with the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * <tt>(1, 0, 0)</tt>.  If the application prefers high bandwidth above low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * latency, and low latency above short connection time, then it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * invoke this method with the values <tt>(0, 1, 2)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <p> Invoking this method after this socket has been bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * will have no effect. This implies that in order to use this capability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * requires the socket to be created with the no-argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param  connectionTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *         An <tt>int</tt> expressing the relative importance of a short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @param  latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *         An <tt>int</tt> expressing the relative importance of low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @param  bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *         An <tt>int</tt> expressing the relative importance of high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    public void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
}