src/java.base/share/classes/java/net/ServerSocket.java
author chegar
Wed, 29 May 2019 13:58:05 +0100
changeset 55081 dd321e3596c0
parent 54743 ba74d2de9a01
child 55106 ebc4e5a625e2
child 57382 14e01d39c01a
permissions -rw-r--r--
8224477: java.net socket types new-style socket option methods - spec and impl mismatch Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
     2
 * Copyright (c) 1995, 2019, 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;
40696
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
    30
import java.lang.reflect.Constructor;
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.ServerSocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
54743
ba74d2de9a01 8223457: java.net.ServerSocket protected constructor should throw NPE if impl null
michaelm
parents: 54689
diff changeset
    35
import java.util.Objects;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
    36
import java.util.Set;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
    37
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
    39
import jdk.internal.access.JavaNetSocketAccess;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
    40
import jdk.internal.access.SharedSecrets;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
    41
import sun.net.PlatformSocketImpl;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class implements server sockets. A server socket waits for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * requests to come in over the network. It performs some operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * based on that request, and then possibly returns a result to the requester.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The actual work of the server socket is performed by an instance
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
    49
 * of the {@code SocketImpl} class. An application can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * change the socket factory that creates the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * implementation to configure itself to create sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * appropriate to the local firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see     java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see     java.nio.channels.ServerSocketChannel
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23879
diff changeset
    58
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class ServerSocket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The implementation of this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private SocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
52371
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    76
     * Creates a server socket with a user-specified {@code SocketImpl}.
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    77
     *
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    78
     * @param      impl an instance of a SocketImpl to use on the ServerSocket.
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    79
     *
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    80
     * @throws     NullPointerException if impl is {@code null}.
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    81
     *
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    82
     * @since 12
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
    83
     */
52371
ae4967993e17 8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents: 48737
diff changeset
    84
    protected ServerSocket(SocketImpl impl) {
54743
ba74d2de9a01 8223457: java.net.ServerSocket protected constructor should throw NPE if impl null
michaelm
parents: 54689
diff changeset
    85
        Objects.requireNonNull(impl);
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
    86
        this.impl = impl;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
    87
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
    88
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
    89
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates an unbound server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @exception IOException IO error when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public ServerSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Creates a server socket, bound to the specified port. A port number
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   101
     * of {@code 0} means that the port number is automatically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * allocated, typically from an ephemeral port range. This port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The maximum queue length for incoming connection indications (a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   106
     * request to connect) is set to {@code 50}. If a connection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * indication arrives when the queue is full, the connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * If the application has specified a server socket factory, that
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   110
     * factory's {@code createSocketImpl} method is called to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   114
     * its {@code checkListen} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   115
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   120
     * @param      port  the port number, or {@code 0} to use a port
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *                   number that is automatically allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception  IOException  if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception  SecurityException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   125
     * if a security manager exists and its {@code checkListen}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see        SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public ServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this(port, 50, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Creates a server socket and binds it to the specified local port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * number, with the specified backlog.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   143
     * A port number of {@code 0} means that the port number is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * automatically allocated, typically from an ephemeral port range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * This port number can then be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The maximum queue length for incoming connection indications (a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   149
     * request to connect) is set to the {@code backlog} parameter. If
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * a connection indication arrives when the queue is full, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * If the application has specified a server socket factory, that
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   154
     * factory's {@code createSocketImpl} method is called to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   158
     * its {@code checkListen} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   159
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   163
     * The {@code backlog} argument is the requested maximum number of
1096
7906d13db4eb 6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents: 715
diff changeset
   164
     * 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
   165
     * specific. In particular, an implementation may impose a maximum length
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
   166
     * or may choose to ignore the parameter altogether. The value provided
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   167
     * should be greater than {@code 0}. If it is less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   168
     * {@code 0}, then an implementation specific default will be used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   170
     * @param      port     the port number, or {@code 0} to use a port
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *                      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
   172
     * @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
   173
     *                      connections.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception  IOException  if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @exception  SecurityException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   177
     * if a security manager exists and its {@code checkListen}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see        SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public ServerSocket(int port, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this(port, backlog, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Create a server with the specified port, listen backlog, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * local IP address to bind to.  The <i>bindAddr</i> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * can be used on a multi-homed host for a ServerSocket that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * will only accept connect requests to one of its addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * If <i>bindAddr</i> is null, it will default accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * connections on any/all local addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The port must be between 0 and 65535, inclusive.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   200
     * A port number of {@code 0} means that the port number is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * automatically allocated, typically from an ephemeral port range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This port number can then be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * {@link #getLocalPort getLocalPort}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <P>If there is a security manager, this method
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   206
     * calls its {@code checkListen} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   207
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   211
     * The {@code backlog} argument is the requested maximum number of
1096
7906d13db4eb 6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents: 715
diff changeset
   212
     * 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
   213
     * specific. In particular, an implementation may impose a maximum length
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
   214
     * or may choose to ignore the parameter altogether. The value provided
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   215
     * should be greater than {@code 0}. If it is less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   216
     * {@code 0}, then an implementation specific default will be used.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 21278
diff changeset
   217
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   218
     * @param port  the port number, or {@code 0} to use a port
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *              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
   220
     * @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
   221
     *                connections.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param bindAddr the local InetAddress the server will bind to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws  SecurityException if a security manager exists and
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   225
     * its {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws  IOException if an I/O error occurs when opening the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see SocketOptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see SecurityManager#checkListen
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23879
diff changeset
   235
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (port < 0 || port > 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                       "Port value out of range: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (backlog < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
          backlog = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            bind(new InetSocketAddress(bindAddr, port), backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   256
     * Get the {@code SocketImpl} attached to this socket, creating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   259
     * @return  the {@code SocketImpl} attached to that ServerSocket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws SocketException if creation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    SocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void setImpl() {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   270
        SocketImplFactory factory = ServerSocket.factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            impl = factory.createSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   274
            impl = SocketImpl.createPlatformSocketImpl(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Creates the socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws IOException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void createImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            impl.create(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   297
     * Binds the {@code ServerSocket} to a specific address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   300
     * If the address is {@code null}, then the system will pick up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * an ephemeral port and a valid local address to bind the socket.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 21278
diff changeset
   302
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17469
diff changeset
   303
     * @param   endpoint        The IP address and port number to bind to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                     is already bound.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   306
     * @throws  SecurityException       if a {@code SecurityManager} is present and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   307
     * its {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public void bind(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        bind(endpoint, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   318
     * Binds the {@code ServerSocket} to a specific address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * (IP address and port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   321
     * If the address is {@code null}, then the system will pick up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <P>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   324
     * The {@code backlog} argument is the requested maximum number of
1096
7906d13db4eb 6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents: 715
diff changeset
   325
     * 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
   326
     * specific. In particular, an implementation may impose a maximum length
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
   327
     * or may choose to ignore the parameter altogether. The value provided
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   328
     * should be greater than {@code 0}. If it is less than or equal to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   329
     * {@code 0}, then an implementation specific default will be used.
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17469
diff changeset
   330
     * @param   endpoint        The IP address and 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
   331
     * @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
   332
     *                          incoming connections.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *                     is already bound.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   335
     * @throws  SecurityException       if a {@code SecurityManager} is present and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   336
     * its {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws  IllegalArgumentException if endpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void bind(SocketAddress endpoint, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new SocketException("Socket is closed");
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   344
        if (isBound())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (endpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            endpoint = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (!(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (backlog < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
          backlog = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (security != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                security.checkListen(epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            getImpl().bind(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            getImpl().listen(backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns the local address of this server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * then this method will continue to return the local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * after the socket is closed.
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   377
     * <p>
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   378
     * If there is a security manager set, its {@code checkConnect} method is
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   379
     * called with the local address and {@code -1} as its arguments to see
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   380
     * if the operation is allowed. If the operation is not allowed,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   381
     * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return  the address to which this socket is bound,
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   384
     *          or the loopback address if denied by the security manager,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   385
     *          or {@code null} if the socket is unbound.
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   386
     *
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   387
     * @see SecurityManager#checkConnect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        try {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   393
            InetAddress in = getImpl().getInetAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   394
            SecurityManager sm = System.getSecurityManager();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   395
            if (sm != null)
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   396
                sm.checkConnect(in.getHostAddress(), -1);
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   397
            return in;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   398
        } catch (SecurityException e) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   399
            return InetAddress.getLoopbackAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Returns the port number on which this socket is listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * then this method will continue to return the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return  the port number to which this socket is listening or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *          -1 if the socket is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // If we're bound, the impl has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            // so we shouldn't get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   432
     * Returns the address of the endpoint this socket is bound to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * then this method will continue to return the address of the endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * after the socket is closed.
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   437
     * <p>
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   438
     * If there is a security manager set, its {@code checkConnect} method is
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   439
     * called with the local address and {@code -1} as its arguments to see
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   440
     * if the operation is allowed. If the operation is not allowed,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   441
     * a {@code SocketAddress} representing the
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   442
     * {@link InetAddress#getLoopbackAddress loopback} address and the local
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   443
     * port to which the socket is bound is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   445
     * @return a {@code SocketAddress} representing the local endpoint of
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   446
     *         this socket, or a {@code SocketAddress} representing the
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   447
     *         loopback address if denied by the security manager,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   448
     *         or {@code null} if the socket is not bound yet.
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   449
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see #bind(SocketAddress)
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   453
     * @see SecurityManager#checkConnect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return new InetSocketAddress(getInetAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Listens for a connection to be made to this socket and accepts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * it. The method blocks until a connection is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   467
     * <p>A new Socket {@code s} is created and, if there
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   469
     * the security manager's {@code checkAccept} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   470
     * with {@code s.getInetAddress().getHostAddress()} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   471
     * {@code s.getPort()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * as its arguments to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @exception  IOException  if an I/O error occurs when waiting for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   478
     *             {@code checkAccept} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *             the timeout has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *             if this socket has an associated channel, the channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *             non-blocking mode, and there is no connection ready to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *             accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @return the new Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @see SecurityManager#checkAccept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new SocketException("Socket is not bound yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        Socket s = new Socket((SocketImpl) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        implAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Subclasses of ServerSocket use this method to override accept()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * to return their own subclass of socket.  So a FooServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * will typically hand this method an <i>empty</i> FooSocket.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * return from implAccept the FooSocket will be connected to a client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param s the Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @throws java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *         if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *         and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IOException if an I/O error occurs when waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * for a connection.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23879
diff changeset
   513
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    protected final void implAccept(Socket s) throws IOException {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   518
        SocketImpl si = s.impl;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   519
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   520
        // Socket has no SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   521
        if (si == null) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   522
            si = implAccept();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   523
            s.setImpl(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   524
            s.postAccept();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   525
            return;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   526
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   527
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   528
        // Socket has a SOCKS or HTTP SocketImpl, need delegate
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   529
        if (si instanceof DelegatingSocketImpl) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   530
            si = ((DelegatingSocketImpl) si).delegate();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   531
            assert si instanceof PlatformSocketImpl;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   532
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   533
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   534
        // Accept connection with a platform or custom SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   535
        // For the platform SocketImpl case:
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   536
        // - the connection is accepted with a new SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   537
        // - the SO_TIMEOUT socket option is copied to the new SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   538
        // - the Socket is connected to the new SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   539
        // - the existing/old SocketImpl is closed
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   540
        // For the custom SocketImpl case, the connection is accepted with the
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   541
        // existing custom SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   542
        ensureCompatible(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   543
        if (impl instanceof PlatformSocketImpl) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   544
            SocketImpl psi = platformImplAccept();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   545
            si.copyOptionsTo(psi);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   546
            s.setImpl(psi);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   547
            si.closeQuietly();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   548
        } else {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   549
            s.impl = null; // temporarily break connection to impl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   550
            try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   551
                customImplAccept(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   552
            } finally {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   553
                s.impl = si;  // restore connection to impl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   555
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   556
        s.postAccept();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   557
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   559
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   560
     * Accepts a connection with a new SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   561
     * @return the new SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   562
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   563
    private SocketImpl implAccept() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   564
        if (impl instanceof PlatformSocketImpl) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   565
            return platformImplAccept();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   566
        } else {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   567
            // custom server SocketImpl, client SocketImplFactory must be set
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   568
            SocketImplFactory factory = Socket.socketImplFactory();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   569
            if (factory == null) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   570
                throw new IOException("An instance of " + impl.getClass() +
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   571
                    " cannot accept connection with 'null' SocketImpl:" +
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   572
                    " client socket implementation factory not set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   574
            SocketImpl si = factory.createSocketImpl();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   575
            customImplAccept(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   576
            return si;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   577
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   578
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   579
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   580
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   581
     * Accepts a connection with a new platform SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   582
     * @return the new platform SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   583
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   584
    private SocketImpl platformImplAccept() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   585
        assert impl instanceof PlatformSocketImpl;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   586
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   587
        // create a new platform SocketImpl and accept the connection
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   588
        SocketImpl psi = SocketImpl.createPlatformSocketImpl(false);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   589
        implAccept(psi);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   590
        return psi;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   591
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   592
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   593
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   594
     * Accepts a new connection with the given custom SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   595
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   596
    private void customImplAccept(SocketImpl si) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   597
        assert !(impl instanceof PlatformSocketImpl)
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   598
                && !(si instanceof PlatformSocketImpl);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   599
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   600
        si.reset();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   601
        try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   602
            // custom SocketImpl may expect fd/address objects to be created
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   603
            si.fd = new FileDescriptor();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   604
            si.address = new InetAddress();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   605
            implAccept(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   606
        } catch (Exception e) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   607
            si.reset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   610
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   611
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   612
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   613
     * Accepts a new connection so that the given SocketImpl is connected to
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   614
     * the peer. The SocketImpl and connection are closed if the connection is
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   615
     * denied by the security manager.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   616
     * @throws IOException if an I/O error occurs
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   617
     * @throws SecurityException if the security manager's checkAccept method fails
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   618
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   619
    private void implAccept(SocketImpl si) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   620
        assert !(si instanceof DelegatingSocketImpl);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   621
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   622
        // accept a connection
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   623
        impl.accept(si);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   624
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   625
        // check permission, close SocketImpl/connection if denied
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   626
        SecurityManager sm = System.getSecurityManager();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   627
        if (sm != null) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   628
            try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   629
                sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   630
            } catch (SecurityException se) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   631
                si.close();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   632
                throw se;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   633
            }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   634
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   635
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   636
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   637
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   638
     * Throws IOException if the server SocketImpl and the given client
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   639
     * SocketImpl are not both platform or custom SocketImpls.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   640
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   641
    private void ensureCompatible(SocketImpl si) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   642
        if ((impl instanceof PlatformSocketImpl) != (si instanceof PlatformSocketImpl)) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   643
            throw new IOException("An instance of " + impl.getClass() +
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   644
                " cannot accept a connection with an instance of " + si.getClass());
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   645
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Any thread currently blocked in {@link #accept()} will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @exception  IOException  if an I/O error occurs when closing the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * associated with this socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p> A server socket will have a channel if, and only if, the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * itself was created via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @return  the server-socket channel associated with this socket,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   681
     *          or {@code null} if this socket was not created
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public ServerSocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Returns the binding state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   694
     * @return true if the ServerSocket successfully bound to an address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public boolean isBound() {
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   698
        return bound;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Returns the closed state of the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   714
     * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   715
     * specified timeout, in milliseconds.  With this option set to a non-zero
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   716
     * timeout, a call to accept() for this ServerSocket
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * ServerSocket is still valid.  The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * prior to entering the blocking operation to have effect.  The
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17469
diff changeset
   721
     * timeout must be {@code > 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @param timeout the specified timeout, in milliseconds
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   724
     * @throws  SocketException if there is an error in the underlying protocol,
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   725
     *          such as a TCP error
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   726
     * @throws  IllegalArgumentException  if {@code timeout} is negative
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23879
diff changeset
   727
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw new SocketException("Socket is closed");
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   733
        if (timeout < 0)
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   734
            throw new IllegalArgumentException("timeout < 0");
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 24865
diff changeset
   735
        getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   739
     * Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   740
     * 0 returns implies that the option is disabled (i.e., timeout of infinity).
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   741
     * @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @exception IOException if an I/O error occurs
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23879
diff changeset
   743
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public synchronized int getSoTimeout() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   759
     * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   760
     * socket option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * When a TCP connection is closed the connection may remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * in a timeout state for a period of time after the connection
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   764
     * is closed (typically known as the {@code TIME_WAIT} state
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   765
     * or {@code 2MSL} wait state).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * For applications using a well known socket address or port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * it may not be possible to bind a socket to the required
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   768
     * {@code SocketAddress} if there is a connection in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * timeout state involving the socket address or port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <p>
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   771
     * Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} prior to
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   772
     * binding the socket using {@link #bind(SocketAddress)} allows the socket
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   773
     * to be bound even though a previous connection is in a timeout state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   775
     * When a {@code ServerSocket} is created the initial setting
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   776
     * of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is not defined.
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   777
     * Applications can use {@link #getReuseAddress()} to determine the initial
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   778
     * setting of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * <p>
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   780
     * The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   781
     * enabled or disabled after a socket is bound (See {@link #isBound()})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @param on  whether to enable or disable the socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @exception SocketException if an error occurs enabling or
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   786
     *            disabling the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   787
     *            socket option, or the socket is closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   801
     * Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   803
     * @return a {@code boolean} indicating whether or not
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   804
     *         {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Returns the implementation address and implementation port of
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   818
     * this socket as a {@code String}.
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   819
     * <p>
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   820
     * If there is a security manager set, its {@code checkConnect} method is
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   821
     * called with the local address and {@code -1} as its arguments to see
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   822
     * if the operation is allowed. If the operation is not allowed,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   823
     * an {@code InetAddress} representing the
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   824
     * {@link InetAddress#getLoopbackAddress loopback} address is returned as
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   825
     * the implementation address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return "ServerSocket[unbound]";
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   832
        InetAddress in;
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   833
        if (System.getSecurityManager() != null)
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   834
            in = InetAddress.getLoopbackAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   835
        else
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   836
            in = impl.getInetAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   837
        return "ServerSocket[addr=" + in +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                ",localport=" + impl.getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * The factory for all server sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 52499
diff changeset
   844
    private static volatile SocketImplFactory factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Sets the server socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * When an application creates a new server socket, the socket
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   851
     * implementation factory's {@code createSocketImpl} method is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * called to create the actual socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   854
     * Passing {@code null} to the method is a no-op unless the factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * If there is a security manager, this method first calls
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   858
     * the security manager's {@code checkSetFactory} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *               socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @exception  SocketException  if the factory has already been defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   867
     *             {@code checkSetFactory} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   883
     * Sets a default proposed value for the
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   884
     * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   885
     * accepted from this {@code ServerSocket}. The value actually set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * in the accepted socket must be determined by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * {@link Socket#getReceiveBufferSize()} after the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * is returned by {@link #accept()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <p>
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   890
     * The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is used both to
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   891
     * set the size of the internal socket receive buffer, and to set the size
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
   892
     * of the TCP receive window that is advertised to the remote peer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * It is possible to change the value subsequently, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * {@link Socket#setReceiveBufferSize(int)}. However, if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * then the proposed value must be set in the ServerSocket <B>before</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * it is bound to a local address. This implies, that the ServerSocket must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * created with the no-argument constructor, then setReceiveBufferSize() must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * be called and lastly the ServerSocket is bound to an address by calling bind().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * 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
   903
     * requested value but the TCP receive window in sockets accepted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * this ServerSocket will be no larger than 64K bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @exception IllegalArgumentException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * value is 0 or is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @see #getReceiveBufferSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     public synchronized void setReceiveBufferSize (int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            throw new IllegalArgumentException("negative receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            throw new SocketException("Socket is closed");
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 24865
diff changeset
   924
        getImpl().setOption(SocketOptions.SO_RCVBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   928
     * Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   929
     * for this {@code ServerSocket}, that is the proposed buffer size that
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   930
     * will be used for Sockets accepted from this {@code ServerSocket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p>Note, the value actually set in the accepted socket is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * calling {@link Socket#getReceiveBufferSize()}.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   934
     * @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   935
     *         option for this {@code Socket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @exception SocketException if there is an error
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 14342
diff changeset
   937
     *            in the underlying protocol, such as a TCP error.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Sets performance preferences for this ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * compared, with larger values indicating stronger preferences.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * bandwidth, for example, then it could invoke this method with the values
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   969
     * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * latency, and low latency above short connection time, then it could
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   971
     * invoke this method with the values {@code (0, 1, 2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p> Invoking this method after this socket has been bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * will have no effect. This implies that in order to use this capability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * requires the socket to be created with the no-argument constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @param  connectionTime
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   978
     *         An {@code int} expressing the relative importance of a short
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @param  latency
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   982
     *         An {@code int} expressing the relative importance of low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @param  bandwidth
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   986
     *         An {@code int} expressing the relative importance of high
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
   998
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
   999
     * Sets the value of a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1000
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 25859
diff changeset
  1001
     * @param <T> The type of the socket option value
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1002
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1003
     * @param value The value of the socket option. A value of {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1004
     *              may be valid for some options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1005
     * @return this ServerSocket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1006
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1007
     * @throws UnsupportedOperationException if the server socket does not
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1008
     *         support the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1009
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1010
     * @throws IllegalArgumentException if the value is not valid for
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1011
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1012
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1013
     * @throws IOException if an I/O error occurs, or if the socket is closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1014
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1015
     * @throws NullPointerException if name is {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1016
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1017
     * @throws SecurityException if a security manager is set and if the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1018
     *         option requires a security permission and if the caller does
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1019
     *         not have the required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1020
     *         {@link java.net.StandardSocketOptions StandardSocketOptions}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1021
     *         do not require any security permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1022
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 28852
diff changeset
  1023
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1024
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1025
    public <T> ServerSocket setOption(SocketOption<T> name, T value)
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1026
        throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1027
    {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1028
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1029
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1030
            throw new SocketException("Socket is closed");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1031
        getImpl().setOption(name, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1032
        return this;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1033
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1034
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1035
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1036
     * Returns the value of a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1037
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 25859
diff changeset
  1038
     * @param <T> The type of the socket option value
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1039
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1040
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1041
     * @return The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1042
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1043
     * @throws UnsupportedOperationException if the server socket does not
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1044
     *         support the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1045
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1046
     * @throws IOException if an I/O error occurs, or if the socket is closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1047
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1048
     * @throws NullPointerException if name is {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1049
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1050
     * @throws SecurityException if a security manager is set and if the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1051
     *         option requires a security permission and if the caller does
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1052
     *         not have the required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1053
     *         {@link java.net.StandardSocketOptions StandardSocketOptions}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1054
     *         do not require any security permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1055
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 28852
diff changeset
  1056
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1057
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1058
    public <T> T getOption(SocketOption<T> name) throws IOException {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1059
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1060
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54743
diff changeset
  1061
            throw new SocketException("Socket is closed");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1062
        return getImpl().getOption(name);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1063
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1064
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1065
    // cache of unmodifiable impl options. Possibly set racy, in impl we trust
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1066
    private volatile Set<SocketOption<?>> options;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1067
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1068
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1069
     * Returns a set of the socket options supported by this server socket.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1070
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1071
     * This method will continue to return the set of options even after
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1072
     * the socket has been closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1073
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1074
     * @return A set of the socket options supported by this socket. This set
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1075
     *         may be empty if the socket's SocketImpl cannot be created.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1076
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 28852
diff changeset
  1077
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1078
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1079
    public Set<SocketOption<?>> supportedOptions() {
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1080
        Set<SocketOption<?>> so = options;
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1081
        if (so != null)
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1082
            return so;
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1083
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1084
        try {
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1085
            SocketImpl impl = getImpl();
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1086
            options = Collections.unmodifiableSet(impl.supportedOptions());
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1087
        } catch (IOException e) {
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1088
            options = Collections.emptySet();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1089
        }
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 52427
diff changeset
  1090
        return options;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1091
    }
40696
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1092
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1093
    static {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1094
        SharedSecrets.setJavaNetSocketAccess(
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1095
            new JavaNetSocketAccess() {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1096
                @Override
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1097
                public ServerSocket newServerSocket(SocketImpl impl) {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1098
                    return new ServerSocket(impl);
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1099
                }
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1100
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1101
                @Override
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1102
                public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1103
                    try {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1104
                        Constructor<? extends SocketImpl> ctor =
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1105
                            implClass.getDeclaredConstructor();
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1106
                        return ctor.newInstance();
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1107
                    } catch (NoSuchMethodException | InstantiationException |
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1108
                             IllegalAccessException | InvocationTargetException e) {
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1109
                        throw new AssertionError(e);
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1110
                    }
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1111
                }
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1112
            }
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1113
        );
d38ae985f810 8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents: 35302
diff changeset
  1114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
}