jdk/src/share/classes/java/net/ServerSocket.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
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 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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            AccessController.doPrivileged(new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    public Object run() throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        Class[] cl = new Class[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        cl[0] = SocketAddress.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        cl[1] = Integer.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        impl.getClass().getDeclaredMethod("connect", cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            oldImpl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private void setImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            impl = factory.createSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // No need to do a checkOldImpl() here, we know it's an up to date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // SocketImpl!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            impl = new SocksSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (impl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            impl.setServerSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Creates the socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws IOException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    void createImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            impl.create(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
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
     * Binds the <code>ServerSocket</code> to a specific address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param   endpoint        The IP address & port number to bind to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @throws  SecurityException       if a <code>SecurityManager</code> is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * its <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void bind(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        bind(endpoint, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
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
     * Binds the <code>ServerSocket</code> to a specific address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * The <code>backlog</code> argument must be a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * value greater than 0. If the value passed is equal or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * than 0, then the default value will be assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param   endpoint        The IP address & port number to bind to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param   backlog         The listen backlog length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws  SecurityException       if a <code>SecurityManager</code> is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * its <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public void bind(SocketAddress endpoint, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (!oldImpl && isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (endpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            endpoint = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (!(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (backlog < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
          backlog = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (security != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                security.checkListen(epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            getImpl().bind(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            getImpl().listen(backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
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
     * Returns the local address of this server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * then this method will continue to return the local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return  the address to which this socket is bound,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *          or <code>null</code> if the socket is unbound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return getImpl().getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns the port number on which this socket is listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * then this method will continue to return the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return  the port number to which this socket is listening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *          -1 if the socket is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Returns the address of the endpoint this socket is bound to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * then this method will continue to return the address of the endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return a <code>SocketAddress</code> representing the local endpoint of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         socket, or <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return new InetSocketAddress(getInetAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Listens for a connection to be made to this socket and accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * it. The method blocks until a connection is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <p>A new Socket <code>s</code> is created and, if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * the security manager's <code>checkAccept</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * with <code>s.getInetAddress().getHostAddress()</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <code>s.getPort()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * as its arguments to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @exception  IOException  if an I/O error occurs when waiting for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *             <code>checkAccept</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *             the timeout has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *             if this socket has an associated channel, the channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *             non-blocking mode, and there is no connection ready to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *             accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return the new Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @see SecurityManager#checkAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            throw new SocketException("Socket is not bound yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        Socket s = new Socket((SocketImpl) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        implAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Subclasses of ServerSocket use this method to override accept()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * to return their own subclass of socket.  So a FooServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * will typically hand this method an <i>empty</i> FooSocket.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * return from implAccept the FooSocket will be connected to a client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param s the Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @throws java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *         if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *         and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @throws IOException if an I/O error occurs when waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    protected final void implAccept(Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        SocketImpl si = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (s.impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
              s.setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                s.impl.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            si = s.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            s.impl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            si.address = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            si.fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            getImpl().accept(si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                security.checkAccept(si.getInetAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                     si.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (si != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                si.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (si != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                si.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        s.impl = si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        s.postAccept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Any thread currently blocked in {@link #accept()} will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @exception  IOException  if an I/O error occurs when closing the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
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
     * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * associated with this socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <p> A server socket will have a channel if, and only if, the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * itself was created via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return  the server-socket channel associated with this socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *          or <tt>null</tt> if this socket was not created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *          for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public ServerSocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Returns the binding state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @return true if the ServerSocket succesfuly bound to an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // Before 1.3 ServerSockets were always bound during creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return bound || oldImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Returns the closed state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
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
     * Enable/disable SO_TIMEOUT with the specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * milliseconds.  With this option set to a non-zero timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * a call to accept() for this ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * ServerSocket is still valid.  The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * prior to entering the blocking operation to have effect.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * timeout must be > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param timeout the specified timeout, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @return the SO_TIMEOUT value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @exception IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public synchronized int getSoTimeout() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
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
     * Enable/disable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * When a TCP connection is closed the connection may remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * in a timeout state for a period of time after the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * is closed (typically known as the <tt>TIME_WAIT</tt> state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * or <tt>2MSL</tt> wait state).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * For applications using a well known socket address or port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * it may not be possible to bind a socket to the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <tt>SocketAddress</tt> if there is a connection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * timeout state involving the socket address or port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * using {@link #bind(SocketAddress)} allows the socket to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * bound even though a previous connection is in a timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * When a <tt>ServerSocket</tt> is created the initial setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * of <tt>SO_REUSEADDR</tt> is not defined. Applications can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * use {@link #getReuseAddress()} to determine the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * setting of <tt>SO_REUSEADDR</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * disabled after a socket is bound (See {@link #isBound()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param on  whether to enable or disable the socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @exception SocketException if an error occurs enabling or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *            disabling the <tt>SO_RESUEADDR</tt> socket option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *            or the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Tests if SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Returns the implementation address and implementation port of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * this socket as a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return "ServerSocket[unbound]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return "ServerSocket[addr=" + impl.getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                ",port=" + impl.getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                ",localport=" + impl.getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    void setBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    void setCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * The factory for all server sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private static SocketImplFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Sets the server socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * When an application creates a new server socket, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * implementation factory's <code>createSocketImpl</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * called to create the actual socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * Passing <code>null</code> to the method is a no-op unless the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *               socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @exception  SocketException  if the factory has already been defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *             <code>checkSetFactory</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Sets a default proposed value for the SO_RCVBUF option for sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * accepted from this <tt>ServerSocket</tt>. The value actually set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * in the accepted socket must be determined by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * {@link Socket#getReceiveBufferSize()} after the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * is returned by {@link #accept()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * The value of SO_RCVBUF is used both to set the size of the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * socket receive buffer, and to set the size of the TCP receive window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * that is advertized to the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * It is possible to change the value subsequently, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * {@link Socket#setReceiveBufferSize(int)}. However, if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * then the proposed value must be set in the ServerSocket <B>before</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * it is bound to a local address. This implies, that the ServerSocket must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * created with the no-argument constructor, then setReceiveBufferSize() must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * be called and lastly the ServerSocket is bound to an address by calling bind().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * 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
   770
     * requested value but the TCP receive window in sockets accepted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * this ServerSocket will be no larger than 64K bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @exception IllegalArgumentException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * value is 0 or is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @see #getReceiveBufferSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     public synchronized void setReceiveBufferSize (int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            throw new IllegalArgumentException("negative receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Gets the value of the SO_RCVBUF option for this <tt>ServerSocket</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * that is the proposed buffer size that will be used for Sockets accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * from this <tt>ServerSocket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p>Note, the value actually set in the accepted socket is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * calling {@link Socket#getReceiveBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Sets performance preferences for this ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * compared, with larger values indicating stronger preferences.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * bandwidth, for example, then it could invoke this method with the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <tt>(1, 0, 0)</tt>.  If the application prefers high bandwidth above low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * latency, and low latency above short connection time, then it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * invoke this method with the values <tt>(0, 1, 2)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <p> Invoking this method after this socket has been bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * will have no effect. This implies that in order to use this capability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * requires the socket to be created with the no-argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param  connectionTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *         An <tt>int</tt> expressing the relative importance of a short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @param  latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *         An <tt>int</tt> expressing the relative importance of low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @param  bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *         An <tt>int</tt> expressing the relative importance of high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
}