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