src/java.base/share/classes/java/net/Socket.java
author pconcannon
Wed, 16 Oct 2019 11:12:47 +0100
changeset 58692 04946947ca79
parent 58610 94710bb2a5bb
child 59201 b24f4caa1411
permissions -rw-r--r--
8231719: Correct contradictions in timeout range descriptions. Summary: Clarified verbiage in method descriptions. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53133
bf2533105a26 8215912: Various Typos in java.net Method Documentation
coffeys
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: 5180
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: 5180
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: 5180
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
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
58610
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
    28
import sun.security.util.SecurityConstants;
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    33
import java.lang.invoke.MethodHandles;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    34
import java.lang.invoke.VarHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.SocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
    38
import java.util.Objects;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
    39
import java.util.Set;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
    40
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class implements client sockets (also called just
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * "sockets"). A socket is an endpoint for communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * between two machines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The actual work of the socket is performed by an instance of the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
    48
 * {@code SocketImpl} class. An application, by changing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the socket factory that creates the socket implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * can configure itself to create sockets appropriate to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see     java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see     java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see     java.nio.channels.SocketChannel
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
    57
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
class Socket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean connected = 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
    private boolean shutIn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean shutOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The implementation of this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    SocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    78
     * Socket input/output streams
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    79
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    80
    private volatile InputStream in;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    81
    private volatile OutputStream out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    82
    private static final VarHandle IN, OUT;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    83
    static {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    84
        try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    85
            MethodHandles.Lookup l = MethodHandles.lookup();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    86
            IN = l.findVarHandle(Socket.class, "in", InputStream.class);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    87
            OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    88
        } catch (Exception e) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    89
            throw new InternalError(e);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    90
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    91
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    92
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
    93
    /**
55323
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    94
     * Creates an unconnected Socket.
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    95
     * <p>
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    96
     * If the application has specified a client socket implementation
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    97
     * factory, that factory's {@code createSocketImpl} method is called to
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    98
     * create the actual socket implementation. Otherwise a system-default
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
    99
     * socket implementation is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   101
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public Socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Creates an unconnected socket, specifying the type of proxy, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * that should be used regardless of any other settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <P>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   112
     * If there is a security manager, its {@code checkConnect} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * is called with the proxy host address and port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Examples:
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   117
     * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * a plain socket ignoring any other proxy configuration.</LI>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   119
     * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * will create a socket connecting through the specified SOCKS proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * server.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param proxy a {@link java.net.Proxy Proxy} object specifying what kind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *              of proxying should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws IllegalArgumentException if the proxy is of an invalid type
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   127
     *          or {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws SecurityException if a security manager is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *                           permission to connect to the proxy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *                           denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see java.net.ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see java.net.Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public Socket(Proxy proxy) {
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   137
        // Create a copy of Proxy as a security measure
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   138
        if (proxy == null) {
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   139
            throw new IllegalArgumentException("Invalid Proxy");
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   140
        }
16061
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents: 10596
diff changeset
   141
        Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents: 10596
diff changeset
   142
                                          : sun.net.ApplicationProxy.create(proxy);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents: 10596
diff changeset
   143
        Proxy.Type type = p.type();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents: 10596
diff changeset
   144
        if (type == Proxy.Type.SOCKS || type == Proxy.Type.HTTP) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            SecurityManager security = System.getSecurityManager();
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   146
            InetSocketAddress epoint = (InetSocketAddress) p.address();
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   147
            if (epoint.getAddress() != null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   148
                checkAddress (epoint.getAddress(), "Socket");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   149
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (epoint.isUnresolved())
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   152
                    epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort());
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   153
                if (epoint.isUnresolved())
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   154
                    security.checkConnect(epoint.getHostName(), epoint.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    security.checkConnect(epoint.getAddress().getHostAddress(),
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   157
                                  epoint.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   159
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   160
            // create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   161
            SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   162
            impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate)
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   163
                                              : new HttpConnectSocketImpl(p, delegate, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
3442
02df74328591 6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents: 475
diff changeset
   165
            if (p == Proxy.NO_PROXY) {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   166
                // create a platform or custom SocketImpl for the DIRECT case
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   167
                SocketImplFactory factory = Socket.factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (factory == null) {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   169
                    impl = SocketImpl.createPlatformSocketImpl(false);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   170
                } else {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   171
                    impl = factory.createSocketImpl();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   172
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new IllegalArgumentException("Invalid Proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates an unconnected Socket with a user-specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * SocketImpl.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 22339
diff changeset
   181
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param impl an instance of a <B>SocketImpl</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the subclass wishes to use on the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   185
     * @throws    SocketException if there is an error in the underlying protocol,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * such as a TCP error.
58610
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   187
     *
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   188
     * @throws SecurityException if {@code impl} is non-null and a security manager is set
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   189
     * and its {@code checkPermission} method doesn't allow {@code NetPermission("setSocketImpl")}.
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   190
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   191
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    protected Socket(SocketImpl impl) throws SocketException {
58610
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   194
        checkPermission(impl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
58610
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   198
    private static Void checkPermission(SocketImpl impl) {
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   199
        if (impl == null) {
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   200
            return null;
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   201
        }
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   202
        SecurityManager sm = System.getSecurityManager();
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   203
        if (sm != null) {
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   204
            sm.checkPermission(SecurityConstants.SET_SOCKETIMPL_PERMISSION);
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   205
        }
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   206
        return null;
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   207
    }
94710bb2a5bb 8217997: Better socket support
michaelm
parents: 58242
diff changeset
   208
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * number on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   213
     * If the specified host is {@code null} it is the equivalent of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   214
     * specifying the address as
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   215
     * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>
55323
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   219
     * If the application has specified a client socket implementation
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   220
     * factory, that factory's {@code createSocketImpl} method is called to
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   221
     * create the actual socket implementation. Otherwise a system-default
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   222
     * socket implementation is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   225
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   226
     * with the host address and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   229
     * @param      host   the host name, or {@code null} for the loopback address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param      port   the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   232
     * @throws     UnknownHostException if the IP address of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the host could not be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   235
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   236
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   237
     *             {@code checkConnect} method doesn't allow the operation.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   238
     * @throws     IllegalArgumentException if the port parameter is outside
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public Socket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws UnknownHostException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
             new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             (SocketAddress) null, true);
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
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * number at the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>
55323
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   258
     * If the application has specified a client socket implementation
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   259
     * factory, that factory's {@code createSocketImpl} method is called to
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   260
     * create the actual socket implementation. Otherwise a system-default
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   261
     * socket implementation is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   264
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   265
     * with the host address and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param      address   the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param      port      the port number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   270
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   271
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   272
     *             {@code checkConnect} method doesn't allow the operation.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   273
     * @throws     IllegalArgumentException if the port parameter is outside
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *             0 and 65535, inclusive.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   276
     * @throws     NullPointerException if {@code address} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public Socket(InetAddress address, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        this(address != null ? new InetSocketAddress(address, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             (SocketAddress) null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Creates a socket and connects it to the specified remote host on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * the specified remote port. The Socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   292
     * If the specified host is {@code null} it is the equivalent of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   293
     * specifying the address as
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   294
     * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   298
     * A local port number of {@code zero} will let the system pick up a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   299
     * free port in the {@code bind} operation.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   302
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   303
     * with the host address and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   306
     * @param host the name of the remote host, or {@code null} for the loopback address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param port the remote port
475
b8b79ef97ac8 6571950: SSLSocket(raddr, rport, laddr, lport) allows null as laddr that spec doesn't reflect
xuelei
parents: 94
diff changeset
   308
     * @param localAddr the local address the socket is bound to, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   309
     *        {@code null} for the {@code anyLocal} address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param localPort the local port the socket is bound to, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   311
     *        {@code zero} for a system selected free port.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   312
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   313
     * @throws     SecurityException  if a security manager exists and its
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   314
     *             {@code checkConnect} method doesn't allow the connection
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   315
     *             to the destination, or if its {@code checkListen} method
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   316
     *             doesn't allow the bind to the local port.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   317
     * @throws     IllegalArgumentException if the port parameter or localPort
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *             parameter is outside the specified range of valid port values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *             which is between 0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see        SecurityManager#checkConnect
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   321
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public Socket(String host, int port, InetAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                  int localPort) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
               new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             new InetSocketAddress(localAddr, localPort), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Creates a socket and connects it to the specified remote address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * the specified remote port. The Socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   335
     * If the specified local address is {@code null} it is the equivalent of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   336
     * specifying the address as the AnyLocal address
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   337
     * (see {@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}{@code ()}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   339
     * A local port number of {@code zero} will let the system pick up a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   340
     * free port in the {@code bind} operation.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   343
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   344
     * with the host address and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param address the remote address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param port the remote port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param localAddr the local address the socket is bound to, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   350
     *        {@code null} for the {@code anyLocal} address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param localPort the local port the socket is bound to or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   352
     *        {@code zero} for a system selected free port.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   353
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   354
     * @throws     SecurityException  if a security manager exists and its
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   355
     *             {@code checkConnect} method doesn't allow the connection
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   356
     *             to the destination, or if its {@code checkListen} method
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   357
     *             doesn't allow the bind to the local port.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   358
     * @throws     IllegalArgumentException if the port parameter or localPort
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *             parameter is outside the specified range of valid port values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *             which is between 0 and 65535, inclusive.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   361
     * @throws     NullPointerException if {@code address} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @see        SecurityManager#checkConnect
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   363
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public Socket(InetAddress address, int port, InetAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                  int localPort) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        this(address != null ? new InetSocketAddress(address, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
             new InetSocketAddress(localAddr, localPort), true);
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
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * number on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   375
     * If the specified host is {@code null} it is the equivalent of
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   376
     * specifying the address as
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   377
     * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   381
     * If the stream argument is {@code true}, this creates a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   382
     * stream socket. If the stream argument is {@code false}, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * creates a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p>
55323
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   385
     * If the application has specified a client socket implementation
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   386
     * factory, that factory's {@code createSocketImpl} method is called to
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   387
     * create the actual socket implementation. Otherwise a system-default
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   388
     * socket implementation is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   391
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   392
     * with the host address and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * If a UDP socket is used, TCP/IP related socket options will not apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   397
     * @param      host     the host name, or {@code null} for the loopback address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param      port     the port number.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   399
     * @param      stream   a {@code boolean} indicating whether this is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *                      a stream socket or a datagram socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   401
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   402
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   403
     *             {@code checkConnect} method doesn't allow the operation.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   404
     * @throws     IllegalArgumentException if the port parameter is outside
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @deprecated Use DatagramSocket instead for UDP transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public Socket(String host, int port, boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
               new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
             (SocketAddress) null, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Creates a socket and connects it to the specified port number at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   424
     * If the stream argument is {@code true}, this creates a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   425
     * stream socket. If the stream argument is {@code false}, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * creates a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p>
55323
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   428
     * If the application has specified a client socket implementation
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   429
     * factory, that factory's {@code createSocketImpl} method is called to
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   430
     * create the actual socket implementation. Otherwise a system-default
fc4042870cc4 8225426: Replace plain with system-default in Socket and ServerSocket
chegar
parents: 55081
diff changeset
   431
     * socket implementation is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   434
     * {@code checkConnect} method is called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   435
     * with {@code host.getHostAddress()} and {@code port}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * If UDP socket is used, TCP/IP related socket options will not apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param      host     the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param      port      the port number.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   442
     * @param      stream    if {@code true}, create a stream socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *                       otherwise, create a datagram socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   444
     * @throws     IOException  if an I/O error occurs when creating the socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   445
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   446
     *             {@code checkConnect} method doesn't allow the operation.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   447
     * @throws     IllegalArgumentException if the port parameter is outside
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *             0 and 65535, inclusive.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   450
     * @throws     NullPointerException if {@code host} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @deprecated Use DatagramSocket instead for UDP transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public Socket(InetAddress host, int port, boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        this(host != null ? new InetSocketAddress(host, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
             new InetSocketAddress(0), stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private Socket(SocketAddress address, SocketAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                   boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (address == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            createImpl(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (localAddr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                bind(localAddr);
10439
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   475
            connect(address);
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   476
        } catch (IOException | IllegalArgumentException | SecurityException e) {
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   477
            try {
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   478
                close();
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   479
            } catch (IOException ce) {
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   480
                e.addSuppressed(ce);
b36f86ad26e1 7088747: Use multicatch in Socket constructor
michaelm
parents: 10437
diff changeset
   481
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Creates the socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   489
     * @param stream a {@code boolean} value : {@code true} for a TCP socket,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   490
     *               {@code false} for UDP.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @throws IOException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     void createImpl(boolean stream) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            impl.create(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   505
    void setImpl(SocketImpl si) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   506
         impl = si;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   507
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   508
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Sets impl to the system-default type of SocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    void setImpl() {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   514
        SocketImplFactory factory = Socket.factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            impl = factory.createSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        } else {
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   518
            // create a SOCKS SocketImpl that delegates to a platform SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   519
            SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   520
            impl = new SocksSocketImpl(delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   525
     * Get the {@code SocketImpl} attached to this socket, creating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   528
     * @return  the {@code SocketImpl} attached to that ServerSocket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @throws SocketException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    SocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            createImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Connects this socket to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   541
     * @param   endpoint the {@code SocketAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @throws  IOException if an error occurs during the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @throws  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *          if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *          and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @throws  IllegalArgumentException if endpoint is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void connect(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        connect(endpoint, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Connects this socket to the server with a specified timeout value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * A timeout of zero is interpreted as an infinite timeout. The connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * will then block until established or an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   560
     * @param   endpoint the {@code SocketAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param   timeout  the timeout value to be used in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @throws  IOException if an error occurs during the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @throws  SocketTimeoutException if timeout expires before connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @throws  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *          if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *          and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @throws  IllegalArgumentException if endpoint is null or is a
54289
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   568
     *          SocketAddress subclass not supported by this socket, or
6183f835b9b6 8219446: Specify behaviour of timeout accepting methods of Socket and ServerSocket if timeout is negative
chegar
parents: 54155
diff changeset
   569
     *          if {@code timeout} is negative
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public void connect(SocketAddress endpoint, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (endpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
          throw new IllegalArgumentException("connect: timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   583
        if (isConnected())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            throw new SocketException("already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (!(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   590
        InetAddress addr = epoint.getAddress ();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   591
        int port = epoint.getPort();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   592
        checkAddress(addr, "connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (epoint.isUnresolved())
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   597
                security.checkConnect(epoint.getHostName(), port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            else
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   599
                security.checkConnect(addr.getHostAddress(), port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            createImpl(true);
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   603
        impl.connect(epoint, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * If the socket was not bound before the connect, it is now because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * the kernel will have picked an ephemeral port & a local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Binds the socket to a local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <P>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   615
     * If the address is {@code null}, then the system will pick up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   618
     * @param   bindpoint the {@code SocketAddress} to bind to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @throws  IllegalArgumentException if bindpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *          SocketAddress subclass not supported by this socket
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   623
     * @throws  SecurityException  if a security manager exists and its
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   624
     *          {@code checkListen} method doesn't allow the bind
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   625
     *          to the local port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see #isBound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public void bind(SocketAddress bindpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throw new SocketException("Socket is closed");
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   633
        if (isBound())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            throw new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        InetSocketAddress epoint = (InetSocketAddress) bindpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (epoint != null && epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            throw new SocketException("Unresolved address");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   641
        if (epoint == null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   642
            epoint = new InetSocketAddress(0);
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   643
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   644
        InetAddress addr = epoint.getAddress();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   645
        int port = epoint.getPort();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   646
        checkAddress (addr, "bind");
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   647
        SecurityManager security = System.getSecurityManager();
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   648
        if (security != null) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   649
            security.checkListen(port);
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   650
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   651
        getImpl().bind (addr, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   655
    private void checkAddress (InetAddress addr, String op) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   656
        if (addr == null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   657
            return;
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   658
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   659
        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   660
            throw new IllegalArgumentException(op + ": invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   661
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   662
    }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 3450
diff changeset
   663
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * set the flags after an accept() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    final void postAccept() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Returns the address to which the socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @return  the remote IP address to which this socket is connected,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   681
     *          or {@code null} if the socket is not connected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return getImpl().getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Gets the local address to which the socket is bound.
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   695
     * <p>
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   696
     * If there is a security manager set, its {@code checkConnect} method is
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   697
     * called with the local address and {@code -1} as its arguments to see
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   698
     * if the operation is allowed. If the operation is not allowed,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   699
     * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   701
     * @return the local address to which the socket is bound,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   702
     *         the loopback address if denied by the security manager, or
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   703
     *         the wildcard address if the socket is closed or not bound yet.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
   704
     * @since   1.1
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   705
     *
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   706
     * @see SecurityManager#checkConnect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // This is for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            return InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        InetAddress in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   715
            SecurityManager sm = System.getSecurityManager();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   716
            if (sm != null)
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   717
                sm.checkConnect(in.getHostAddress(), -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (in.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                in = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   721
        } catch (SecurityException e) {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   722
            in = InetAddress.getLoopbackAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            in = InetAddress.anyLocalAddress(); // "0.0.0.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Returns the remote port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * then this method will continue to return the connected port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @return  the remote port number to which this socket is connected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *          0 if the socket is not connected yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            return getImpl().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // Shouldn't happen as we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Returns the local port number to which this socket is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * then this method will continue to return the local port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return  the local port number to which this socket is bound or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *          if the socket is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } catch(SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // shouldn't happen as we're bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * Returns the address of the endpoint this socket is connected to, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   773
     * {@code null} if it is unconnected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
94
wetmore
parents: 81 51
diff changeset
   779
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   780
     * @return a {@code SocketAddress} representing the remote endpoint of this
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   781
     *         socket, or {@code null} if it is not connected yet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @see #connect(SocketAddress, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @see #connect(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public SocketAddress getRemoteSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        return new InetSocketAddress(getInetAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   795
     * Returns the address of the endpoint this socket is bound to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * If a socket bound to an endpoint represented by an
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   798
     * {@code InetSocketAddress } is {@link #close closed},
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   799
     * then this method will continue to return an {@code InetSocketAddress}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * after the socket is closed. In that case the returned
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   801
     * {@code InetSocketAddress}'s address is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * {@link InetAddress#isAnyLocalAddress wildcard} address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * and its port is the local port that it was bound to.
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   804
     * <p>
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   805
     * If there is a security manager set, its {@code checkConnect} method is
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   806
     * called with the local address and {@code -1} as its arguments to see
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   807
     * if the operation is allowed. If the operation is not allowed,
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   808
     * a {@code SocketAddress} representing the
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   809
     * {@link InetAddress#getLoopbackAddress loopback} address and the local
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   810
     * port to which this socket is bound is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   812
     * @return a {@code SocketAddress} representing the local endpoint of
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   813
     *         this socket, or a {@code SocketAddress} representing the
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   814
     *         loopback address if denied by the security manager, or
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   815
     *         {@code null} if the socket is not bound yet.
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   816
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @see #getLocalAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @see #bind(SocketAddress)
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 16061
diff changeset
   820
     * @see SecurityManager#checkConnect
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return new InetSocketAddress(getLocalAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Returns the unique {@link java.nio.channels.SocketChannel SocketChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * object associated with this socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * <p> A socket will have a channel if, and only if, the channel itself was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * created via the {@link java.nio.channels.SocketChannel#open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * SocketChannel.open} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * java.nio.channels.ServerSocketChannel#accept ServerSocketChannel.accept}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return  the socket channel associated with this socket,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   841
     *          or {@code null} if this socket was not created
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *          for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public SocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Returns an input stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p> If this socket has an associated channel then the resulting input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * stream delegates all of its operations to the channel.  If the channel
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   856
     * is in non-blocking mode then the input stream's {@code read} operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * will throw an {@link java.nio.channels.IllegalBlockingModeException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <p>Under abnormal conditions the underlying connection may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * broken by the remote host or the network software (for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * a connection reset in the case of TCP connections). When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * broken connection is detected by the network software the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * following applies to the returned input stream :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *   <li><p>The network software may discard bytes that are buffered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *   by the socket. Bytes that aren't discarded by the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *   software can be read using {@link java.io.InputStream#read read}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *   <li><p>If there are no bytes buffered on the socket, or all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *   buffered bytes have been consumed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *   {@link java.io.InputStream#read read}, then all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *   calls to {@link java.io.InputStream#read read} will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *   {@link java.io.IOException IOException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *   <li><p>If there are no bytes buffered on the socket, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *   socket has not been closed using {@link #close close}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *   {@link java.io.InputStream#available available} will
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   880
     *   return {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <p> Closing the returned {@link java.io.InputStream InputStream}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * will close the associated socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @return     an input stream for reading bytes from this socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   888
     * @throws     IOException  if an I/O error occurs when creating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *             input stream, the socket is closed, the socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *             not connected, or the socket input has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *             using {@link #shutdownInput()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        if (isInputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            throw new SocketException("Socket input is shutdown");
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   903
        InputStream in = this.in;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   904
        if (in == null) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   905
            // wrap the input stream so that the close method closes this socket
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   906
            in = new SocketInputStream(this, impl.getInputStream());
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   907
            if (!IN.compareAndSet(this, null, in)) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   908
                in = this.in;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   909
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   911
        return in;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   912
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   913
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   914
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   915
     * An InputStream that delegates read/available operations to an underlying
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   916
     * input stream. The close method is overridden to close the Socket.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   917
     *
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   918
     * This class is instrumented by Java Flight Recorder (JFR) to get socket
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   919
     * I/O events.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   920
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   921
    private static class SocketInputStream extends InputStream {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   922
        private final Socket parent;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   923
        private final InputStream in;
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   924
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   925
        SocketInputStream(Socket parent, InputStream in) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   926
            this.parent = parent;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   927
            this.in = in;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   928
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   929
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   930
        public int read() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   931
            byte[] a = new byte[1];
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   932
            int n = read(a, 0, 1);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   933
            return (n > 0) ? (a[0] & 0xff) : -1;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   934
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   935
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   936
        public int read(byte b[], int off, int len) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   937
            return in.read(b, off, len);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   938
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   939
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   940
        public int available() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   941
            return in.available();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   942
        }
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
   943
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   944
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   945
        public void close() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   946
            parent.close();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   947
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * Returns an output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * <p> If this socket has an associated channel then the resulting output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * stream delegates all of its operations to the channel.  If the channel
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
   955
     * is in non-blocking mode then the output stream's {@code write}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * operations will throw an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * java.nio.channels.IllegalBlockingModeException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * <p> Closing the returned {@link java.io.OutputStream OutputStream}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * will close the associated socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @return     an output stream for writing bytes to this socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
   963
     * @throws     IOException  if an I/O error occurs when creating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *               output stream or if the socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        if (isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            throw new SocketException("Socket output is shutdown");
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   975
        OutputStream out = this.out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   976
        if (out == null) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   977
            // wrap the output stream so that the close method closes this socket
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   978
            out = new SocketOutputStream(this, impl.getOutputStream());
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   979
            if (!OUT.compareAndSet(this, null, out)) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   980
                out = this.out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   981
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   983
        return out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   984
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   985
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   986
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   987
     * An OutputStream that delegates write operations to an underlying output
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   988
     * stream. The close method is overridden to close the Socket.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   989
     *
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   990
     * This class is instrumented by Java Flight Recorder (JFR) to get socket
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   991
     * I/O events.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   992
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   993
    private static class SocketOutputStream extends OutputStream {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   994
        private final Socket parent;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   995
        private final OutputStream out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   996
        SocketOutputStream(Socket parent, OutputStream out) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   997
            this.parent = parent;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   998
            this.out = out;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
   999
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1000
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1001
        public void write(int b) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1002
            byte[] a = new byte[] { (byte) b };
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1003
            write(a, 0, 1);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1004
        }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1005
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1006
        public void write(byte b[], int off, int len) throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1007
            out.write(b, off, len);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1008
        }
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
  1009
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1010
        @Override
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1011
        public void close() throws IOException {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1012
            parent.close();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1013
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1017
     * Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1018
     * (disable/enable Nagle's algorithm).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1020
     * @param on {@code true} to enable TCP_NODELAY,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1021
     * {@code false} to disable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1023
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1026
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @see #getTcpNoDelay()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    public void setTcpNoDelay(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1037
     * Tests if {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1039
     * @return a {@code boolean} indicating whether or not
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1040
     *         {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1041
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * in the underlying protocol, such as a TCP error.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1043
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @see #setTcpNoDelay(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    public boolean getTcpNoDelay() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1053
     * Enable/disable {@link SocketOptions#SO_LINGER SO_LINGER} with the
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1054
     * specified linger time in seconds. The maximum timeout value is platform
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1055
     * specific.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * The setting only affects socket close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param on     whether or not to linger on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @param linger how long to linger for, if on is true.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1061
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * in the underlying protocol, such as a TCP error.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1063
     * @throws    IllegalArgumentException if the linger value is negative.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1064
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @see #getSoLinger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    public void setSoLinger(boolean on, int linger) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if (!on) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23879
diff changeset
  1071
            getImpl().setOption(SocketOptions.SO_LINGER, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            if (linger < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                throw new IllegalArgumentException("invalid value for SO_LINGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            if (linger > 65535)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                linger = 65535;
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 24865
diff changeset
  1078
            getImpl().setOption(SocketOptions.SO_LINGER, linger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1083
     * Returns setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1084
     * -1 returns implies that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * option is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * The setting only affects socket close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1089
     * @return the setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1090
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * in the underlying protocol, such as a TCP error.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1092
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @see #setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    public int getSoLinger() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        Object o = getImpl().getOption(SocketOptions.SO_LINGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * Send one byte of urgent data on the socket. The byte to be sent is the lowest eight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * bits of the data parameter. The urgent byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * sent after any preceding writes to the socket OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * and before any future writes to the OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param data The byte of data to send
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1112
     * @throws    IOException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *  sending the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public void sendUrgentData (int data) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (!getImpl().supportsUrgentData ()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            throw new SocketException ("Urgent data not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        getImpl().sendUrgentData (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1124
     * Enable/disable {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1125
     * (receipt of TCP urgent data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * By default, this option is disabled and TCP urgent data received on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * socket is silently discarded. If the user wishes to receive urgent data, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * this option must be enabled. When enabled, urgent data is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * inline with normal data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Note, only limited support is provided for handling incoming urgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * data. In particular, no notification of incoming urgent data is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * and there is no capability to distinguish between normal data and urgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * data unless provided by a higher level protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1137
     * @param on {@code true} to enable
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1138
     *           {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE},
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1139
     *           {@code false} to disable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1141
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @see #getOOBInline()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    public void setOOBInline(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1155
     * Tests if {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1157
     * @return a {@code boolean} indicating whether or not
53133
bf2533105a26 8215912: Various Typos in java.net Method Documentation
coffeys
parents: 52499
diff changeset
  1158
     *         {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE} is enabled.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1159
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1160
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @see #setOOBInline(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    public boolean getOOBInline() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1172
     *  Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1173
     *  with the specified timeout, in milliseconds. With this option set
58692
04946947ca79 8231719: Correct contradictions in timeout range descriptions.
pconcannon
parents: 58610
diff changeset
  1174
     *  to a positive timeout value, a read() call on the InputStream associated with
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1175
     *  this Socket will block for only this amount of time.  If the timeout
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1176
     *  expires, a <B>java.net.SocketTimeoutException</B> is raised, though the
58692
04946947ca79 8231719: Correct contradictions in timeout range descriptions.
pconcannon
parents: 58610
diff changeset
  1177
     *  Socket is still valid. A timeout of zero is interpreted as an infinite timeout.
04946947ca79 8231719: Correct contradictions in timeout range descriptions.
pconcannon
parents: 58610
diff changeset
  1178
     *  The option <B>must</B> be enabled prior to entering the blocking operation
04946947ca79 8231719: Correct contradictions in timeout range descriptions.
pconcannon
parents: 58610
diff changeset
  1179
     *  to have effect.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1180
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @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
  1182
     * @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
  1183
     *          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
  1184
     * @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: 24685
diff changeset
  1185
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
          throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 24865
diff changeset
  1194
        getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1198
     * Returns setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1199
     * 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: 16061
diff changeset
  1200
     *
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1201
     * @return the setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1202
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * in the underlying protocol, such as a TCP error.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1204
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24685
diff changeset
  1205
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    public synchronized int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1221
     * Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1222
     * specified value for this {@code Socket}.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1223
     * The {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option is used by the
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1224
     * platform's networking code as a hint for the size to set the underlying
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1225
     * network I/O buffers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1227
     * <p>Because {@link SocketOptions#SO_SNDBUF SO_SNDBUF} is a hint,
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1228
     * applications that want to verify what size the buffers were set to
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1229
     * should call {@link #getSendBufferSize()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1231
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @param size the size to which to set the send buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1237
     * @throws    IllegalArgumentException if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * value is 0 or is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * @see #getSendBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    public synchronized void setSendBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            throw new IllegalArgumentException("negative send size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            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
  1250
        getImpl().setOption(SocketOptions.SO_SNDBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1254
     * Get value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1255
     * for this {@code Socket}, that is the buffer size used by the platform
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1256
     * for output on this {@code Socket}.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1257
     * @return the value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1258
     *         option for this {@code Socket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1260
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * @see #setSendBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    public synchronized int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1278
     * Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1279
     * specified value for this {@code Socket}. The
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1280
     * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1281
     * used by the platform's networking code as a hint for the size to set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * the underlying network I/O buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <p>Increasing the receive buffer size can increase the performance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * network I/O for high-volume connection, while decreasing it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * help reduce the backlog of incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1288
     * <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint,
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1289
     * applications that want to verify what size the buffers were set to
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1290
     * should call {@link #getReceiveBufferSize()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1292
     * <p>The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is also used
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
  1293
     * to set the TCP receive window that is advertised to the remote peer.
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1294
     * Generally, the window size can be modified at any time when a socket is
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1295
     * connected. However, if a receive window larger than 64K is required then
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1296
     * this must be requested <B>before</B> the socket is connected to the
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
  1297
     * remote peer. There are two cases to be aware of:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * <li>For sockets accepted from a ServerSocket, this must be done by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 22339
diff changeset
  1301
     * is bound to a local address.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * <li>For client sockets, setReceiveBufferSize() must be called before
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
  1303
     * connecting the socket to its remote peer.</li></ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1307
     * @throws    IllegalArgumentException if the value is 0 or is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1310
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @see #getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * @see ServerSocket#setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    public synchronized void setReceiveBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            throw new IllegalArgumentException("invalid receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            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
  1324
        getImpl().setOption(SocketOptions.SO_RCVBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1328
     * 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
  1329
     * for this {@code Socket}, that is the buffer size used by the platform
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1330
     * for input on this {@code Socket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1332
     * @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1333
     *         option for this {@code Socket}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1334
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1352
     * Enable/disable {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1354
     * @param on  whether or not to have socket keep alive turned on.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1355
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @see #getKeepAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    public void setKeepAlive(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1367
     * Tests if {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1369
     * @return a {@code boolean} indicating whether or not
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1370
     *         {@link SocketOptions#SO_KEEPALIVE SO_KEEPALIVE} is enabled.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1371
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @see #setKeepAlive(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    public boolean getKeepAlive() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Sets traffic class or type-of-service octet in the IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * header for packets sent from this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * As the underlying network implementation may ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * value applications should consider it a hint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17469
diff changeset
  1388
     * <P> The tc <B>must</B> be in the range {@code 0 <= tc <=
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 17469
diff changeset
  1389
     * 255} or an IllegalArgumentException will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * <p>Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * <p>For Internet Protocol v4 the value consists of an
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1392
     * {@code integer}, the least significant 8 bits of which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * represent the value of the TOS octet in IP packets sent by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * RFC 1349 defines the TOS values as follows:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
  1396
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * The last low order bit is always ignored as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * corresponds to the MBZ (must be zero) bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * Setting bits in the precedence field may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * SocketException indicating that the operation is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * As RFC 1122 section 4.2.4.2 indicates, a compliant TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * implementation should, but is not required to, let application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * change the TOS field during the lifetime of a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * So whether the type-of-service field can be changed after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * TCP connection has been established depends on the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * in the underlying platform. Applications should not assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * they can change the TOS field after the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1418
     * For Internet Protocol v6 {@code tc} is the value that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * would be placed into the sin6_flowinfo field of the IP header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1421
     * @param tc        an {@code int} value for the bitset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * @throws SocketException if there is an error setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * traffic class or type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @see #getTrafficClass
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1426
     * @see SocketOptions#IP_TOS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    public void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (tc < 0 || tc > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            throw new IllegalArgumentException("tc is not in range 0 -- 255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            throw new SocketException("Socket is closed");
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1434
        try {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1435
            getImpl().setOption(SocketOptions.IP_TOS, tc);
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1436
        } catch (SocketException se) {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1437
            // not supported if socket already connected
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1438
            // Solaris returns error in such cases
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1439
            if(!isConnected())
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1440
                throw se;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
  1441
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * Gets traffic class or type-of-service in the IP header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * for packets sent from this Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * As the underlying network implementation may ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * traffic class or type-of-service set using {@link #setTrafficClass(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * this method may return a different value than was previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * set using the {@link #setTrafficClass(int)} method on this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @return the traffic class or type-of-service already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @throws SocketException if there is an error obtaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * traffic class or type-of-service value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @see #setTrafficClass(int)
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1458
     * @see SocketOptions#IP_TOS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    public int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1465
     * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1466
     * socket option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * When a TCP connection is closed the connection may remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * 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
  1470
     * is closed (typically known as the {@code TIME_WAIT} state
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1471
     * or {@code 2MSL} wait state).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * For applications using a well known socket address or port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * 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
  1474
     * {@code SocketAddress} if there is a connection in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * timeout state involving the socket address or port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * <p>
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1477
     * Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1478
     * prior to binding the socket using {@link #bind(SocketAddress)} allows
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1479
     * the socket to be bound even though a previous connection is in a timeout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1482
     * When a {@code Socket} is created the initial setting
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1483
     * of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * <p>
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1485
     * The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1486
     * enabled or disabled after a socket is bound (See {@link #isBound()})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @param on  whether to enable or disable the socket option
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1490
     * @throws    SocketException if an error occurs enabling or
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1491
     *            disabling the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1492
     *            socket option, or the socket is closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /**
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1506
     * Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1508
     * @return a {@code boolean} indicating whether or not
17469
e00a02431a8b 6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents: 16061
diff changeset
  1509
     *         {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1510
     * @throws    SocketException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * Any thread currently blocked in an I/O operation upon this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * will throw a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * Once a socket has been closed, it is not available for further networking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * use (i.e. can't be reconnected or rebound). A new socket needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * <p> Closing this socket will also close the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * {@link java.io.InputStream InputStream} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * {@link java.io.OutputStream OutputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1538
     * @throws     IOException  if an I/O error occurs when closing this socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * @see #isClosed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    public synchronized void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            if (created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * Places the input stream for this socket at "end of stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * Any data sent to the input stream side of the socket is acknowledged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * and then silently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * <p>
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9775
diff changeset
  1558
     * If you read from a socket input stream after invoking this method on the
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9775
diff changeset
  1559
     * socket, the stream's {@code available} method will return 0, and its
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9775
diff changeset
  1560
     * {@code read} methods will return {@code -1} (end of stream).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1562
     * @throws    IOException if an I/O error occurs when shutting down this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * @see java.net.Socket#shutdownOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * @see #isInputShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    public void shutdownInput() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        if (isInputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            throw new SocketException("Socket input is already shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        getImpl().shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        shutIn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * Disables the output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * For a TCP socket, any previously written data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * followed by TCP's normal connection termination sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * If you write to a socket output stream after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * shutdownOutput() on the socket, the stream will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1592
     * @throws    IOException if an I/O error occurs when shutting down this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * @see java.net.Socket#shutdownInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * @see #isOutputShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    public void shutdownOutput() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        if (isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            throw new SocketException("Socket output is already shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        getImpl().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        shutOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1614
     * Converts this socket to a {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            if (isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                return "Socket[addr=" + getImpl().getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    ",port=" + getImpl().getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    ",localport=" + getImpl().getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        return "Socket[unconnected]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * Returns the connection state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * Note: Closing a socket doesn't clear its connection state, which means
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1633
     * this method will return {@code true} for a closed socket
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
  1634
     * (see {@link #isClosed()}) if it was successfully connected prior
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * to being closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     *
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
  1637
     * @return true if the socket was successfully connected to a server
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    public boolean isConnected() {
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
  1641
        return connected;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * Returns the binding state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * Note: Closing a socket doesn't clear its binding state, which means
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1648
     * this method will return {@code true} for a closed socket
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
  1649
     * (see {@link #isClosed()}) if it was successfully bound prior
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * to being closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 52456
diff changeset
  1652
     * @return true if the socket was successfully bound to an address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * @see #bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    public boolean isBound() {
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54289
diff changeset
  1657
        return bound;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * Returns the closed state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * @see #close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * Returns whether the read-half of the socket connection is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @return true if the input of the socket has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * @see #shutdownInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        return shutIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * Returns whether the write-half of the socket connection is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @return true if the output of the socket has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * @see #shutdownOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        return shutOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * The factory for all client sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     */
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1698
    private static volatile SocketImplFactory factory;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1699
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1700
    static SocketImplFactory socketImplFactory() {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1701
        return factory;
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 53133
diff changeset
  1702
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * Sets the client socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * When an application creates a new client socket, the socket
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1709
     * implementation factory's {@code createSocketImpl} method is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * called to create the actual socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1712
     * Passing {@code null} to the method is a no-op unless the factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * <p>If there is a security manager, this method first calls
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1715
     * the security manager's {@code checkSetFactory} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * @param      fac   the desired factory.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1720
     * @throws     IOException  if an I/O error occurs when setting the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     *               socket factory.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1722
     * @throws     SocketException  if the factory is already defined.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55323
diff changeset
  1723
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1724
     *             {@code checkSetFactory} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    public static synchronized void setSocketImplFactory(SocketImplFactory fac)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * Sets performance preferences for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * compared, with larger values indicating stronger preferences. Negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * values represent a lower priority than positive values. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * 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
  1758
     * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * latency, and low latency above short connection time, then it could
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1760
     * invoke this method with the values {@code (0, 1, 2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * <p> Invoking this method after this socket has been connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @param  connectionTime
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1766
     *         An {@code int} expressing the relative importance of a short
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * @param  latency
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1770
     *         An {@code int} expressing the relative importance of low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * @param  bandwidth
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18274
diff changeset
  1774
     *         An {@code int} expressing the relative importance of high
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    public void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1785
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1786
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1787
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1788
     * Sets the value of a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1789
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 25859
diff changeset
  1790
     * @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
  1791
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1792
     * @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
  1793
     *              may be valid for some options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1794
     * @return this Socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1795
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1796
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1797
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1798
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1799
     * @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
  1800
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1801
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1802
     * @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
  1803
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1804
     * @throws NullPointerException if name is {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1805
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1806
     * @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
  1807
     *         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
  1808
     *         not have the required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1809
     *         {@link java.net.StandardSocketOptions StandardSocketOptions}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1810
     *         do not require any security permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1811
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30963
diff changeset
  1812
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1813
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1814
    public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
  1815
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
  1816
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
  1817
            throw new SocketException("Socket is closed");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1818
        getImpl().setOption(name, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1819
        return this;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1820
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1821
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1822
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1823
     * Returns the value of a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1824
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 25859
diff changeset
  1825
     * @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
  1826
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1827
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1828
     * @return The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1829
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1830
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1831
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1832
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1833
     * @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
  1834
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1835
     * @throws NullPointerException if name is {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1836
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1837
     * @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
  1838
     *         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
  1839
     *         not have the required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1840
     *         {@link java.net.StandardSocketOptions StandardSocketOptions}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1841
     *         do not require any security permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1842
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30963
diff changeset
  1843
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1844
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1845
    @SuppressWarnings("unchecked")
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1846
    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: 54689
diff changeset
  1847
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
  1848
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
  1849
            throw new SocketException("Socket is closed");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1850
        return getImpl().getOption(name);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1851
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1852
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1853
    // 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: 47216
diff changeset
  1854
    private volatile Set<SocketOption<?>> options;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1855
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1856
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1857
     * Returns a set of the socket options supported by this socket.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1858
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1859
     * 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
  1860
     * the socket has been closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1861
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1862
     * @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
  1863
     *         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
  1864
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30963
diff changeset
  1865
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1866
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1867
    public Set<SocketOption<?>> supportedOptions() {
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1868
        Set<SocketOption<?>> so = options;
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1869
        if (so != null)
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1870
            return so;
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1871
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1872
        try {
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1873
            SocketImpl impl = getImpl();
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1874
            options = Collections.unmodifiableSet(impl.supportedOptions());
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1875
        } catch (IOException e) {
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1876
            options = Collections.emptySet();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1877
        }
52456
90ff0e286a5e 8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents: 47216
diff changeset
  1878
        return options;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 23720
diff changeset
  1879
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
}