jdk/src/share/classes/java/net/Socket.java
author wetmore
Tue, 11 Mar 2008 23:37:02 -0700
changeset 94 842a7d676774
parent 81 c2d2c408d6ed
parent 51 6fe31bc95bbc
child 475 b8b79ef97ac8
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.SocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class implements client sockets (also called just
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * "sockets"). A socket is an endpoint for communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * between two machines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The actual work of the socket is performed by an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>SocketImpl</code> class. An application, by changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the socket factory that creates the socket implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * can configure itself to create sockets appropriate to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * firewall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see     java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see     java.nio.channels.SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
class Socket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean shutIn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean shutOut = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The implementation of this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    SocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Are we using an older SocketImpl?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean oldImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Creates an unconnected socket, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * system-default type of SocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public Socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Creates an unconnected socket, specifying the type of proxy, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * that should be used regardless of any other settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * If there is a security manager, its <code>checkConnect</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * is called with the proxy host address and port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <UL> <LI><code>Socket s = new Socket(Proxy.NO_PROXY);</code> will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * a plain socket ignoring any other proxy configuration.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <LI><code>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * will create a socket connecting through the specified SOCKS proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * server.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param proxy a {@link java.net.Proxy Proxy} object specifying what kind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *              of proxying should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws IllegalArgumentException if the proxy is of an invalid type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws SecurityException if a security manager is present and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *                           permission to connect to the proxy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *                           denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see java.net.ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see java.net.Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Socket(Proxy proxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (proxy != null && proxy.type() == Proxy.Type.SOCKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            InetSocketAddress epoint = (InetSocketAddress) proxy.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    security.checkConnect(epoint.getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                          epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    security.checkConnect(epoint.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                          epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            impl = new SocksSocketImpl(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            impl.setSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (proxy == Proxy.NO_PROXY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    impl = new PlainSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    impl.setSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new IllegalArgumentException("Invalid Proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates an unconnected Socket with a user-specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * SocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param impl an instance of a <B>SocketImpl</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * the subclass wishes to use on the Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception SocketException if there is an error in the underlying protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected Socket(SocketImpl impl) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (impl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            this.impl.setSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * number on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * If the specified host is <tt>null</tt> it is the equivalent of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * If the application has specified a server socket factory, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * factory's <code>createSocketImpl</code> method is called to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * with the host address and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param      host   the host name, or <code>null</code> for the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param      port   the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @exception  UnknownHostException if the IP address of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the host could not be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Socket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throws UnknownHostException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
             new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
             (SocketAddress) null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * number at the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * If the application has specified a socket factory, that factory's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>createSocketImpl</code> method is called to create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * with the host address and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param      address   the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param      port      the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception  NullPointerException if <code>address</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public Socket(InetAddress address, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this(address != null ? new InetSocketAddress(address, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             (SocketAddress) null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Creates a socket and connects it to the specified remote host on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the specified remote port. The Socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * If the specified host is <tt>null</tt> it is the equivalent of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * A local port number of <code>zero</code> will let the system pick up a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * free port in the <code>bind</code> operation.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * with the host address and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param host the name of the remote host, or <code>null</code> for the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param port the remote port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param localAddr the local address the socket is bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param localPort the local port the socket is bound to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *        <code>zero</code> for a system selected free port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception  IllegalArgumentException if the port parameter or localPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *             parameter is outside the specified range of valid port values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *             which is between 0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Socket(String host, int port, InetAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                  int localPort) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
               new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
             new InetSocketAddress(localAddr, localPort), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Creates a socket and connects it to the specified remote address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the specified remote port. The Socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * If the specified local address is <tt>null</tt> it is the equivalent of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * specifying the address as the AnyLocal address (see <tt>{@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}()</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * A local port number of <code>zero</code> will let the system pick up a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * free port in the <code>bind</code> operation.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * with the host address and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param address the remote address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param port the remote port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param localAddr the local address the socket is bound to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *        <code>null</code> for the <code>anyLocal</code> address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param localPort the local port the socket is bound to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *        <code>zero</code> for a system selected free port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @exception  IllegalArgumentException if the port parameter or localPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *             parameter is outside the specified range of valid port values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *             which is between 0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception  NullPointerException if <code>address</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public Socket(InetAddress address, int port, InetAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                  int localPort) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        this(address != null ? new InetSocketAddress(address, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             new InetSocketAddress(localAddr, localPort), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Creates a stream socket and connects it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * number on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * If the specified host is <tt>null</tt> it is the equivalent of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * In other words, it is equivalent to specifying an address of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * loopback interface. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * If the stream argument is <code>true</code>, this creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * stream socket. If the stream argument is <code>false</code>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * creates a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * If the application has specified a server socket factory, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * factory's <code>createSocketImpl</code> method is called to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * with the host address and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * If a UDP socket is used, TCP/IP related socket options will not apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param      host     the host name, or <code>null</code> for the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param      port     the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param      stream   a <code>boolean</code> indicating whether this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *                      a stream socket or a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @deprecated Use DatagramSocket instead for UDP transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public Socket(String host, int port, boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        this(host != null ? new InetSocketAddress(host, port) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
               new InetSocketAddress(InetAddress.getByName(null), port),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             (SocketAddress) null, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Creates a socket and connects it to the specified port number at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the specified IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * If the stream argument is <code>true</code>, this creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * stream socket. If the stream argument is <code>false</code>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * creates a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * If the application has specified a server socket factory, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * factory's <code>createSocketImpl</code> method is called to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the actual socket implementation. Otherwise a "plain" socket is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <code>checkConnect</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * with <code>host.getHostAddress()</code> and <code>port</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * as its arguments. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * If UDP socket is used, TCP/IP related socket options will not apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param      host     the IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param      port      the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param      stream    if <code>true</code>, create a stream socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *                       otherwise, create a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @exception  IOException  if an I/O error occurs when creating the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *             <code>checkConnect</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception  IllegalArgumentException if the port parameter is outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *             the specified range of valid port values, which is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *             0 and 65535, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception  NullPointerException if <code>host</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see        java.net.SocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @deprecated Use DatagramSocket instead for UDP transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public Socket(InetAddress host, int port, boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        this(host != null ? new InetSocketAddress(host, port) : null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
             new InetSocketAddress(0), stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private Socket(SocketAddress address, SocketAddress localAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                   boolean stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (address == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            createImpl(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (localAddr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                bind(localAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (address != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                connect(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Creates the socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param stream a <code>boolean</code> value : <code>true</code> for a TCP socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *               <code>false</code> for UDP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @throws IOException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     void createImpl(boolean stream) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            setImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            impl.create(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private void checkOldImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // SocketImpl.connect() is a protected method, therefore we need to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // getDeclaredMethod, therefore we need permission to access the member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        oldImpl = AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                (new PrivilegedAction<Boolean>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                Class[] cl = new Class[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                cl[0] = SocketAddress.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                cl[1] = Integer.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                Class clazz = impl.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        clazz.getDeclaredMethod("connect", cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        clazz = clazz.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        // java.net.SocketImpl class will always have this abstract method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        // If we have not found it by now in the hierarchy then it does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        // exist, we are an old style impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        if (clazz.equals(java.net.SocketImpl.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Sets impl to the system-default type of SocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    void setImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            impl = factory.createSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // No need to do a checkOldImpl() here, we know it's an up to date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            // SocketImpl!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            impl = new SocksSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (impl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            impl.setSocket(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Get the <code>SocketImpl</code> attached to this socket, creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return  the <code>SocketImpl</code> attached to that ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @throws SocketException if creation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    SocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            createImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Connects this socket to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param   endpoint the <code>SocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @throws  IOException if an error occurs during the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @throws  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *          if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *          and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @throws  IllegalArgumentException if endpoint is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public void connect(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        connect(endpoint, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Connects this socket to the server with a specified timeout value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * A timeout of zero is interpreted as an infinite timeout. The connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * will then block until established or an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param   endpoint the <code>SocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param   timeout  the timeout value to be used in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @throws  IOException if an error occurs during the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @throws  SocketTimeoutException if timeout expires before connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *          if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *          and the channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @throws  IllegalArgumentException if endpoint is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void connect(SocketAddress endpoint, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (endpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
          throw new IllegalArgumentException("connect: timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (!oldImpl && isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new SocketException("already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (!(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                security.checkConnect(epoint.getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                security.checkConnect(epoint.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            createImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (!oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            impl.connect(epoint, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        else if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                impl.connect(epoint.getAddress().getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                             epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                impl.connect(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * If the socket was not bound before the connect, it is now because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * the kernel will have picked an ephemeral port & a local address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Binds the socket to a local address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param   bindpoint the <code>SocketAddress</code> to bind to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @throws  IOException if the bind operation fails, or if the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *                     is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @throws  IllegalArgumentException if bindpoint is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @see #isBound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public void bind(SocketAddress bindpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (!oldImpl && isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        InetSocketAddress epoint = (InetSocketAddress) bindpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (epoint != null && epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (bindpoint == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            getImpl().bind(InetAddress.anyLocalAddress(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            getImpl().bind(epoint.getAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                           epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * set the flags after an accept() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    final void postAccept() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    void setCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    void setBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    void setConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Returns the address to which the socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @return  the remote IP address to which this socket is connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *          or <code>null</code> if the socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return getImpl().getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Gets the local address to which the socket is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return the local address to which the socket is bound, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *         the {@link InetAddress#isAnyLocalAddress wildcard} address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *         if the socket is closed or not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        // This is for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        InetAddress in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (in.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                in = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            in = InetAddress.anyLocalAddress(); // "0.0.0.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Returns the remote port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * then this method will continue to return the connected port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @return  the remote port number to which this socket is connected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *          0 if the socket is not connected yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return getImpl().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // Shouldn't happen as we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Returns the local port number to which this socket is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * then this method will continue to return the local port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return  the local port number to which this socket is bound or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *          if the socket is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        } catch(SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // shouldn't happen as we're bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Returns the address of the endpoint this socket is connected to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * <code>null</code> if it is unconnected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
94
wetmore
parents: 81 51
diff changeset
   734
81
c2d2c408d6ed 6667108: typo in javadoc for java.net.Socket.getRemoteSocketAddress()
chegar
parents: 2
diff changeset
   735
     * @return a <code>SocketAddress</code> representing the remote endpoint of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *         socket, or <code>null</code> if it is not connected yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @see #connect(SocketAddress, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @see #connect(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    public SocketAddress getRemoteSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return new InetSocketAddress(getInetAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Returns the address of the endpoint this socket is bound to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * If a socket bound to an endpoint represented by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <code>InetSocketAddress </code> is {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * then this method will continue to return an <code>InetSocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * after the socket is closed. In that case the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * <code>InetSocketAddress</code>'s address is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * {@link InetAddress#isAnyLocalAddress wildcard} address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * and its port is the local port that it was bound to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @return a <code>SocketAddress</code> representing the local endpoint of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *         socket, or <code>null</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @see #getLocalAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return new InetSocketAddress(getLocalAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Returns the unique {@link java.nio.channels.SocketChannel SocketChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * object associated with this socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * <p> A socket will have a channel if, and only if, the channel itself was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * created via the {@link java.nio.channels.SocketChannel#open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * SocketChannel.open} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * java.nio.channels.ServerSocketChannel#accept ServerSocketChannel.accept}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @return  the socket channel associated with this socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *          or <tt>null</tt> if this socket was not created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *          for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public SocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Returns an input stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p> If this socket has an associated channel then the resulting input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * stream delegates all of its operations to the channel.  If the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * is in non-blocking mode then the input stream's <tt>read</tt> operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * will throw an {@link java.nio.channels.IllegalBlockingModeException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <p>Under abnormal conditions the underlying connection may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * broken by the remote host or the network software (for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * a connection reset in the case of TCP connections). When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * broken connection is detected by the network software the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * following applies to the returned input stream :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *   <li><p>The network software may discard bytes that are buffered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *   by the socket. Bytes that aren't discarded by the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *   software can be read using {@link java.io.InputStream#read read}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *   <li><p>If there are no bytes buffered on the socket, or all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *   buffered bytes have been consumed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *   {@link java.io.InputStream#read read}, then all subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *   calls to {@link java.io.InputStream#read read} will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *   {@link java.io.IOException IOException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *   <li><p>If there are no bytes buffered on the socket, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *   socket has not been closed using {@link #close close}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *   {@link java.io.InputStream#available available} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *   return <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * <p> Closing the returned {@link java.io.InputStream InputStream}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * will close the associated socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return     an input stream for reading bytes from this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @exception  IOException  if an I/O error occurs when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *             input stream, the socket is closed, the socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *             not connected, or the socket input has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *             using {@link #shutdownInput()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (isInputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            throw new SocketException("Socket input is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        final Socket s = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   851
            is = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   852
                new PrivilegedExceptionAction<InputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   853
                    public InputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        return impl.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            throw (IOException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * Returns an output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <p> If this socket has an associated channel then the resulting output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * stream delegates all of its operations to the channel.  If the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * is in non-blocking mode then the output stream's <tt>write</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * operations will throw an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * java.nio.channels.IllegalBlockingModeException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * <p> Closing the returned {@link java.io.OutputStream OutputStream}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * will close the associated socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @return     an output stream for writing bytes to this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @exception  IOException  if an I/O error occurs when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *               output stream or if the socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            throw new SocketException("Socket output is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        final Socket s = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   891
            os = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   892
                new PrivilegedExceptionAction<OutputStream>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   893
                    public OutputStream run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        return impl.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            throw (IOException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @param on <code>true</code> to enable TCP_NODELAY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * <code>false</code> to disable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @see #getTcpNoDelay()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public void setTcpNoDelay(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * Tests if TCP_NODELAY is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @return a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @see #setTcpNoDelay(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public boolean getTcpNoDelay() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Enable/disable SO_LINGER with the specified linger time in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * The maximum timeout value is platform specific.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * The setting only affects socket close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param on     whether or not to linger on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param linger how long to linger for, if on is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @exception IllegalArgumentException if the linger value is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @see #getSoLinger()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public void setSoLinger(boolean on, int linger) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        if (!on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (linger < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                throw new IllegalArgumentException("invalid value for SO_LINGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            if (linger > 65535)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                linger = 65535;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Returns setting for SO_LINGER. -1 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * option is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * The setting only affects socket close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @return the setting for SO_LINGER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @see #setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public int getSoLinger() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        Object o = getImpl().getOption(SocketOptions.SO_LINGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * 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
   991
     * bits of the data parameter. The urgent byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * sent after any preceding writes to the socket OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * and before any future writes to the OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @param data The byte of data to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @exception IOException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *  sending the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    public void sendUrgentData (int data) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (!getImpl().supportsUrgentData ()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            throw new SocketException ("Urgent data not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        getImpl().sendUrgentData (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * Enable/disable OOBINLINE (receipt of TCP urgent data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * By default, this option is disabled and TCP urgent data received on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * socket is silently discarded. If the user wishes to receive urgent data, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * this option must be enabled. When enabled, urgent data is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * inline with normal data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Note, only limited support is provided for handling incoming urgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * data. In particular, no notification of incoming urgent data is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * and there is no capability to distinguish between normal data and urgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * data unless provided by a higher level protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param on <code>true</code> to enable OOBINLINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <code>false</code> to disable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @see #getOOBInline()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    public void setOOBInline(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * Tests if OOBINLINE is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @return a <code>boolean</code> indicating whether or not OOBINLINE is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @see #setOOBInline(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    public boolean getOOBInline() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *  Enable/disable SO_TIMEOUT with the specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *  milliseconds.  With this option set to a non-zero timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *  a read() call on the InputStream associated with this Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *  will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *  a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *  Socket is still valid. The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *  prior to entering the blocking operation to have effect. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *  timeout must be > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *  A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @param timeout the specified timeout, in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @since   JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
          throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * Returns setting for SO_TIMEOUT.  0 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @return the setting for SO_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    public synchronized int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * Sets the SO_SNDBUF option to the specified value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * <tt>Socket</tt>. The SO_SNDBUF option is used by the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * networking code as a hint for the size to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * the underlying network I/O buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * <p>Because SO_SNDBUF is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * verify what size the buffers were set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * {@link #getSendBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @param size the size to which to set the send buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @exception IllegalArgumentException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * value is 0 or is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @see #getSendBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public synchronized void setSendBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            throw new IllegalArgumentException("negative send size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * Get value of the SO_SNDBUF option for this <tt>Socket</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * that is the buffer size used by the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * for output on this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * @return the value of the SO_SNDBUF option for this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @see #setSendBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    public synchronized int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * Sets the SO_RCVBUF option to the specified value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <tt>Socket</tt>. The SO_RCVBUF option is used by the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * networking code as a hint for the size to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * the underlying network I/O buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <p>Increasing the receive buffer size can increase the performance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * network I/O for high-volume connection, while decreasing it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * help reduce the backlog of incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <p>Because SO_RCVBUF is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * verify what size the buffers were set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * {@link #getReceiveBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <p>The value of SO_RCVBUF is also used to set the TCP receive window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * that is advertized to the remote peer. Generally, the window size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * can be modified at any time when a socket is connected. However, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * a receive window larger than 64K is required then this must be requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <B>before</B> the socket is connected to the remote peer. There are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * cases to be aware of:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <li>For sockets accepted from a ServerSocket, this must be done by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * is bound to a local address.<p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <li>For client sockets, setReceiveBufferSize() must be called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * connecting the socket to its remote peer.<p></li></ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @exception IllegalArgumentException if the value is 0 or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @see #getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @see ServerSocket#setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    public synchronized void setReceiveBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            throw new IllegalArgumentException("invalid receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * Gets the value of the SO_RCVBUF option for this <tt>Socket</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * that is the buffer size used by the platform for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * input on this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Enable/disable SO_KEEPALIVE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @param on     whether or not to have socket keep alive turned on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * @see #getKeepAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    public void setKeepAlive(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * Tests if SO_KEEPALIVE is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @see #setKeepAlive(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    public boolean getKeepAlive() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Sets traffic class or type-of-service octet in the IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * header for packets sent from this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * As the underlying network implementation may ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * value applications should consider it a hint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <P> The tc <B>must</B> be in the range <code> 0 <= tc <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * 255</code> or an IllegalArgumentException will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * <p>Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <p>For Internet Protocol v4 the value consists of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <code>integer</code>, the least significant 8 bits of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * represent the value of the TOS octet in IP packets sent by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * RFC 1349 defines the TOS values as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * The last low order bit is always ignored as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * corresponds to the MBZ (must be zero) bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * Setting bits in the precedence field may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * SocketException indicating that the operation is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * As RFC 1122 section 4.2.4.2 indicates, a compliant TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * implementation should, but is not required to, let application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * change the TOS field during the lifetime of a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * So whether the type-of-service field can be changed after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * TCP connection has been established depends on the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * in the underlying platform. Applications should not assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * they can change the TOS field after the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * For Internet Protocol v6 <code>tc</code> is the value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * would be placed into the sin6_flowinfo field of the IP header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @param tc        an <code>int</code> value for the bitset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @throws SocketException if there is an error setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * traffic class or type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @see #getTrafficClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        if (tc < 0 || tc > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            throw new IllegalArgumentException("tc is not in range 0 -- 255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Gets traffic class or type-of-service in the IP header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * for packets sent from this Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * As the underlying network implementation may ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * traffic class or type-of-service set using {@link #setTrafficClass(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * this method may return a different value than was previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * set using the {@link #setTrafficClass(int)} method on this Socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @return the traffic class or type-of-service already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @throws SocketException if there is an error obtaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * traffic class or type-of-service value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * @see #setTrafficClass(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    public int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * Enable/disable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * When a TCP connection is closed the connection may remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * in a timeout state for a period of time after the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * is closed (typically known as the <tt>TIME_WAIT</tt> state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * or <tt>2MSL</tt> wait state).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * For applications using a well known socket address or port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * it may not be possible to bind a socket to the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * <tt>SocketAddress</tt> if there is a connection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * timeout state involving the socket address or port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * using {@link #bind(SocketAddress)} allows the socket to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * bound even though a previous connection is in a timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * When a <tt>Socket</tt> is created the initial setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * of <tt>SO_REUSEADDR</tt> is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * disabled after a socket is bound (See {@link #isBound()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @param on  whether to enable or disable the socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * @exception SocketException if an error occurs enabling or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *            disabling the <tt>SO_RESUEADDR</tt> socket option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *            or the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    public void setReuseAddress(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_REUSEADDR, 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Tests if SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * in the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * Any thread currently blocked in an I/O operation upon this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * will throw a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * Once a socket has been closed, it is not available for further networking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * use (i.e. can't be reconnected or rebound). A new socket needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * <p> Closing this socket will also close the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * {@link java.io.InputStream InputStream} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * {@link java.io.OutputStream OutputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @exception  IOException  if an I/O error occurs when closing this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @see #isClosed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    public synchronized void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            if (created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * Places the input stream for this socket at "end of stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * Any data sent to the input stream side of the socket is acknowledged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * and then silently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * If you read from a socket input stream after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * shutdownInput() on the socket, the stream will return EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @exception IOException if an I/O error occurs when shutting down this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @see java.net.Socket#shutdownOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * @see #isInputShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    public void shutdownInput() throws IOException
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");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        if (isInputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            throw new SocketException("Socket input is already shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        getImpl().shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        shutIn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * Disables the output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * For a TCP socket, any previously written data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * followed by TCP's normal connection termination sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * If you write to a socket output stream after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * shutdownOutput() on the socket, the stream will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @exception IOException if an I/O error occurs when shutting down this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @see java.net.Socket#shutdownInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @see #isOutputShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    public void shutdownOutput() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        if (isOutputShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            throw new SocketException("Socket output is already shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        getImpl().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        shutOut = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * Converts this socket to a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            if (isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                return "Socket[addr=" + getImpl().getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                    ",port=" + getImpl().getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    ",localport=" + getImpl().getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        return "Socket[unconnected]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * Returns the connection state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * Note: Closing a socket doesn't clear its connection state, which means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * this method will return <code>true</code> for a closed socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * (see {@link #isClosed()}) if it was successfuly connected prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * to being closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @return true if the socket was successfuly connected to a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        // Before 1.3 Sockets were always connected during creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        return connected || oldImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * Returns the binding state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * Note: Closing a socket doesn't clear its binding state, which means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * this method will return <code>true</code> for a closed socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * (see {@link #isClosed()}) if it was successfuly bound prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * to being closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @return true if the socket was successfuly bound to an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * @see #bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        // Before 1.3 Sockets were always bound during creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        return bound || oldImpl;
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
     * Returns the closed state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @see #close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * Returns whether the read-half of the socket connection is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * @return true if the input of the socket has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @see #shutdownInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        return shutIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * Returns whether the write-half of the socket connection is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * @return true if the output of the socket has been shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * @see #shutdownOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        return shutOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * The factory for all client sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    private static SocketImplFactory factory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * Sets the client socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * When an application creates a new client socket, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * implementation factory's <code>createSocketImpl</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * called to create the actual socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * Passing <code>null</code> to the method is a no-op unless the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * <p>If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *               socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * @exception  SocketException  if the factory is already defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     *             <code>checkSetFactory</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * @see        java.net.SocketImplFactory#createSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    public static synchronized void setSocketImplFactory(SocketImplFactory fac)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * Sets performance preferences for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * compared, with larger values indicating stronger preferences. Negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * values represent a lower priority than positive values. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * bandwidth, for example, then it could invoke this method with the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * <tt>(1, 0, 0)</tt>.  If the application prefers high bandwidth above low
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * latency, and low latency above short connection time, then it could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * invoke this method with the values <tt>(0, 1, 2)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * <p> Invoking this method after this socket has been connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * @param  connectionTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     *         An <tt>int</tt> expressing the relative importance of a short
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * @param  latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *         An <tt>int</tt> expressing the relative importance of low
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * @param  bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *         An <tt>int</tt> expressing the relative importance of high
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    public void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
}