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