jdk/src/share/classes/java/net/DatagramSocket.java
author juh
Tue, 30 Jul 2013 11:04:19 -0700
changeset 19069 1d9cb0d080e3
parent 18156 edb590d448c5
child 21334 c60dfce46a77
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8021833: javadoc cleanup in java.net Summary: <code> and <tt> converted to {@code }; package.html to package-info.java Reviewed-by: darcy, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
     2
 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5465
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5465
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5465
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5465
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5465
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.DatagramChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class represents a socket for sending and receiving datagram packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>A datagram socket is the sending or receiving point for a packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * delivery service. Each packet sent or received on a datagram socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * is individually addressed and routed. Multiple packets sent from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * one machine to another may be routed differently, and may arrive in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * any order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
1817
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    42
 * <p> Where possible, a newly constructed {@code DatagramSocket} has the
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    43
 * {@link SocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    44
 * to allow the transmission of broadcast datagrams. In order to receive
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    45
 * broadcast packets a DatagramSocket should be bound to the wildcard address.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    46
 * In some implementations, broadcast packets may also be received when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a DatagramSocket is bound to a more specific address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Example:
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    50
 * {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *              DatagramSocket s = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *              s.bind(new InetSocketAddress(8888));
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    53
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Which is equivalent to:
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    55
 * {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *              DatagramSocket s = new DatagramSocket(8888);
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    57
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Both cases will create a DatagramSocket able to receive broadcasts on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * UDP port 8888.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see     java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see     java.nio.channels.DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
class DatagramSocket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The implementation of this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    DatagramSocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Are we using an older DatagramSocketImpl?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    boolean oldImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Connection state:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * ST_NOT_CONNECTED = socket not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * ST_CONNECTED = socket connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * ST_CONNECTED_NO_IMPL = socket connected but not at impl level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static final int ST_NOT_CONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static final int ST_CONNECTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static final int ST_CONNECTED_NO_IMPL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int connectState = ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Connected address & port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    InetAddress connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Connects this socket to a remote socket address (IP address + port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Binds socket if not already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12047
diff changeset
   108
     * @param   address The remote address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param   port    The remote port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @throws  SocketException if binding the socket fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private synchronized void connectInternal(InetAddress address, int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new IllegalArgumentException("connect: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IllegalArgumentException("connect: null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   119
        checkAddress (address, "connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (address.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                security.checkMulticast(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                security.checkConnect(address.getHostAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                security.checkAccept(address.getHostAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
          bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // old impls do not support connect/disconnect
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   136
        if (oldImpl || (impl instanceof AbstractPlainDatagramSocketImpl &&
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   137
             ((AbstractPlainDatagramSocketImpl)impl).nativeConnectDisabled())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            connectState = ST_CONNECTED_NO_IMPL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                getImpl().connect(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // socket is now connected by the impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                connectState = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // connection will be emulated by DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                connectState = ST_CONNECTED_NO_IMPL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        connectedAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        connectedPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Constructs a datagram socket and binds it to any available port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * on the local host machine.  The socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   164
     * its {@code checkListen} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * with 0 as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   171
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public DatagramSocket() throws SocketException {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   176
        this(new InetSocketAddress(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Creates an unbound datagram socket with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * DatagramSocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param impl an instance of a <B>DatagramSocketImpl</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *        the subclass wishes to use on the DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected DatagramSocket(DatagramSocketImpl impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Creates a datagram socket, bound to the specified local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   198
     * If, if the address is {@code null}, creates an unbound socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   201
     * its {@code checkListen} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * with the port from the socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   206
     * @param bindaddr local socket address to bind, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *                 for an unbound socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   212
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public DatagramSocket(SocketAddress bindaddr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // create a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (bindaddr != null) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   221
            try {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   222
                bind(bindaddr);
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   223
            } finally {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   224
                if (!isBound())
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   225
                    close();
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 10134
diff changeset
   226
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Constructs a datagram socket and binds it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * on the local host machine.  The socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   237
     * its {@code checkListen} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   238
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param      port port to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   246
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public DatagramSocket(int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this(port, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates a datagram socket, bound to the specified local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * address.  The local port must be between 0 and 65535 inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * If the IP address is 0.0.0.0, the socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   262
     * its {@code checkListen} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   263
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param port local port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param laddr local address to bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   273
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public DatagramSocket(int port, InetAddress laddr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        this(new InetSocketAddress(laddr, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private void checkOldImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // DatagramSocketImpl.peekdata() is a protected method, therefore we need to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // getDeclaredMethod, therefore we need permission to access the member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   288
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   289
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   290
                    public Void run() throws NoSuchMethodException {
10701
2c8dde454979 7090499: missing rawtypes warnings in anonymous inner class
chegar
parents: 10596
diff changeset
   291
                        Class<?>[] cl = new Class<?>[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        cl[0] = DatagramPacket.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        impl.getClass().getDeclaredMethod("peekData", cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            oldImpl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   302
    static Class<?> implClass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    void createImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (impl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                impl = factory.createDatagramSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                boolean isMulticast = (this instanceof MulticastSocket) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(isMulticast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // creates a udp socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        impl.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   322
     * Get the {@code DatagramSocketImpl} attached to this socket,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * creating it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   325
     * @return  the {@code DatagramSocketImpl} attached to that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *          DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @throws SocketException if creation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    DatagramSocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   337
     * Binds this DatagramSocket to a specific address and port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   339
     * If the address is {@code null}, then the system will pick up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *<p>
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   342
     * @param   addr The address and port to bind to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws  SocketException if any error happens during the bind, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          socket is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   346
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws IllegalArgumentException if addr is a SocketAddress subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         not supported by this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public synchronized void bind(SocketAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new SocketException("already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            addr = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (!(addr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new IllegalArgumentException("Unsupported address type!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        InetSocketAddress epoint = (InetSocketAddress) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new SocketException("Unresolved address");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   363
        InetAddress iaddr = epoint.getAddress();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   364
        int port = epoint.getPort();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   365
        checkAddress(iaddr, "bind");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (sec != null) {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   368
            sec.checkListen(port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        try {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   371
            getImpl().bind(port, iaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            getImpl().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   379
    void checkAddress (InetAddress addr, String op) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   380
        if (addr == null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   381
            return;
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   382
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   383
        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   384
            throw new IllegalArgumentException(op + ": invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   385
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   386
    }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   387
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Connects the socket to a remote address for this socket. When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * socket is connected to a remote address, packets may only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * sent to or received from that address. By default a datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>If the remote destination to which the socket is connected does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * exist, or is otherwise unreachable, and if an ICMP destination unreachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * packet has been received for that address, then a subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * send or receive may throw a PortUnreachableException. Note, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * guarantee that the exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
5465
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   400
     * <p> If a security manager has been installed then it is invoked to check
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   401
     * access to the remote address. Specifically, if the given {@code address}
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   402
     * is a {@link InetAddress#isMulticastAddress multicast address},
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   403
     * the security manager's {@link
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   404
     * java.lang.SecurityManager#checkMulticast(InetAddress)
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   405
     * checkMulticast} method is invoked with the given {@code address}.
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   406
     * Otherwise, the security manager's {@link
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   407
     * java.lang.SecurityManager#checkConnect(String,int) checkConnect}
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   408
     * and {@link java.lang.SecurityManager#checkAccept checkAccept} methods
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   409
     * are invoked, with the given {@code address} and {@code port}, to
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   410
     * verify that datagrams are permitted to be sent and received
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   411
     * respectively.
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   412
     *
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   413
     * <p> When a socket is connected, {@link #receive receive} and
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   414
     * {@link #send send} <b>will not perform any security checks</b>
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   415
     * on incoming and outgoing packets, other than matching the packet's
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   416
     * and the socket's address and port. On a send operation, if the
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   417
     * packet's address is set and the packet's address and the socket's
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   418
     * address do not match, an {@code IllegalArgumentException} will be
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   419
     * thrown. A socket connected to a multicast address may only be used
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   420
     * to send packets.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param address the remote address for the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param port the remote port for the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
5465
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   426
     * @throws IllegalArgumentException
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   427
     *         if the address is null, or the port is out of range.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
5465
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   429
     * @throws SecurityException
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   430
     *         if a security manager has been installed and it does
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   431
     *         not permit access to the given remote address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @see #disconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public void connect(InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            connectInternal(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            throw new Error("connect failed", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Connects this socket to a remote socket address (IP address + port number).
5465
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   445
     *
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   446
     * <p> If given an {@link InetSocketAddress InetSocketAddress}, this method
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   447
     * behaves as if invoking {@link #connect(InetAddress,int) connect(InetAddress,int)}
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   448
     * with the the given socket addresses IP address and port number.
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   449
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param   addr    The remote address.
5465
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   451
     *
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   452
     * @throws  SocketException
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   453
     *          if the connect fails
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   454
     *
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   455
     * @throws IllegalArgumentException
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   456
     *         if {@code addr} is {@code null}, or {@code addr} is a SocketAddress
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   457
     *         subclass not supported by this socket
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   458
     *
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   459
     * @throws SecurityException
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   460
     *         if a security manager has been installed and it does
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   461
     *         not permit access to the given remote address
950789cfc3c5 6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents: 5180
diff changeset
   462
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void connect(SocketAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new IllegalArgumentException("Address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (!(addr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        InetSocketAddress epoint = (InetSocketAddress) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        connectInternal(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Disconnects the socket. If the socket is closed or not connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * then this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see #connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (connectState == ST_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                impl.disconnect ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            connectState = ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Returns the binding state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * If the socket was bound prior to being {@link #close closed},
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   499
     * then this method will continue to return {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return true if the socket successfully bound to an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Returns the connection state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * If the socket was connected prior to being {@link #close closed},
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   513
     * then this method will continue to return {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return true if the socket successfully connected to a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return connectState != ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Returns the address to which this socket is connected. Returns
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   525
     * {@code null} if the socket is not connected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return the address to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return connectedAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Returns the port number to which this socket is connected.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   539
     * Returns {@code -1} if the socket is not connected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * then this method will continue to return the connected port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @return the port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return connectedPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Returns the address of the endpoint this socket is connected to, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   553
     * {@code null} if it is unconnected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   559
     * @return a {@code SocketAddress} representing the remote
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   560
     *         endpoint of this socket, or {@code null} if it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *         not connected yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see #connect(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public SocketAddress getRemoteSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return new InetSocketAddress(getInetAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns the address of the endpoint this socket is bound to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   576
     * @return a {@code SocketAddress} representing the local endpoint of this
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   577
     *         socket, or {@code null} if it is closed or not bound yet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see #getLocalAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return new InetSocketAddress(getLocalAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Sends a datagram packet from this socket. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   594
     * {@code DatagramPacket} includes information indicating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * data to be sent, its length, the IP address of the remote host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * and the port number on the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>If there is a security manager, and the socket is not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * connected to a remote address, this method first performs some
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   600
     * security checks. First, if {@code p.getAddress().isMulticastAddress()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * is true, this method calls the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   602
     * security manager's {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   603
     * with {@code p.getAddress()} as its argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * If the evaluation of that expression is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * this method instead calls the security manager's
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   606
     * {@code checkConnect} method with arguments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   607
     * {@code p.getAddress().getHostAddress()} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   608
     * {@code p.getPort()}. Each call to a security manager method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * could result in a SecurityException if the operation is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   611
     * @param      p   the {@code DatagramPacket} to be sent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   615
     *             {@code checkMulticast} or {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *             method doesn't allow the send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @exception  PortUnreachableException may be thrown if the socket is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *             to a currently unreachable destination. Note, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *             guarantee that the exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *             if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *             and the channel is in non-blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @exception  IllegalArgumentException if the socket is connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *             and connected address and packet address differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @see        java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @see        SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public void send(DatagramPacket p) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        InetAddress packetAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                throw new SocketException("Socket is closed");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   637
            checkAddress (p.getAddress(), "send");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (connectState == ST_NOT_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                // check the address is ok wiht the security manager on every send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                // The reason you want to synchronize on datagram packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                // is because you dont want an applet to change the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                // while you are trying to send the packet for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                // after the security check but before the send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    if (p.getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        security.checkMulticast(p.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        security.checkConnect(p.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                              p.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                // we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                packetAddress = p.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                if (packetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    p.setAddress(connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    p.setPort(connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                } else if ((!packetAddress.equals(connectedAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                           p.getPort() != connectedPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    throw new IllegalArgumentException("connected address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                                       "and packet address" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                                       " differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            // Check whether the socket is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            // call the  method to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            getImpl().send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Receives a datagram packet from this socket. When this method
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   677
     * returns, the {@code DatagramPacket}'s buffer is filled with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * the data received. The datagram packet also contains the sender's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * IP address, and the port number on the sender's machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * This method blocks until a datagram is received. The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   682
     * {@code length} field of the datagram packet object contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * the length of the received message. If the message is longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * the packet's length, the message is truncated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * If there is a security manager, a packet cannot be received if the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   687
     * security manager's {@code checkAccept} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * does not allow it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   690
     * @param      p   the {@code DatagramPacket} into which to place
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *                 the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @exception  SocketTimeoutException  if setSoTimeout was previously called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *                 and the timeout has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @exception  PortUnreachableException may be thrown if the socket is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *             to a currently unreachable destination. Note, there is no guarantee that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *             exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *             if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *             and the channel is in non-blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @see        java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @see        java.net.DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public synchronized void receive(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (connectState == ST_NOT_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                // check the address is ok with the security manager before every recv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        String peekAd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        int peekPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        // peek at the packet to see who it is from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        if (!oldImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                            // We can use the new peekData() API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            peekPort = getImpl().peekData(peekPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            peekAd = peekPacket.getAddress().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            InetAddress adr = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                            peekPort = getImpl().peek(adr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            peekAd = adr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                            security.checkAccept(peekAd, peekPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                            // security check succeeded - so now break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                            // and recv the packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            // Throw away the offending packet by consuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                            // it in a tmp buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            DatagramPacket tmp = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                            getImpl().receive(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            // silently discard the offending packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            // and continue: unknown/malicious
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                            // entities on nets should not make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            // runtime throw security exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                            // disrupt the applet by sending random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                            // datagram packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    } // end of while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (connectState == ST_CONNECTED_NO_IMPL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                // We have to do the filtering the old fashioned way since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                // the native impl doesn't support connect or the connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                // via the impl failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                boolean stop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                while (!stop) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   756
                    InetAddress peekAddress = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   757
                    int peekPort = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    // peek at the packet to see who it is from.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   759
                    if (!oldImpl) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   760
                        // We can use the new peekData() API
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   761
                        DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   762
                        peekPort = getImpl().peekData(peekPacket);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   763
                        peekAddress = peekPacket.getAddress();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   764
                    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   765
                        // this api only works for IPv4
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   766
                        peekAddress = new InetAddress();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   767
                        peekPort = getImpl().peek(peekAddress);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10701
diff changeset
   768
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    if ((!connectedAddress.equals(peekAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        (connectedPort != peekPort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        // throw the packet away and silently continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        DatagramPacket tmp = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        getImpl().receive(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        stop = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // If the security check succeeds, or the datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            // connected then receive the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            getImpl().receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Gets the local address to which the socket is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>If there is a security manager, its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   789
     * {@code checkConnect} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   790
     * with the host address and {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @return  the local address to which the socket is bound,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   795
     *          {@code null} if the socket is closed, or
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   796
     *          an {@code InetAddress} representing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *          {@link InetAddress#isAnyLocalAddress wildcard}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *          address if either the socket is not bound, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   799
     *          the security manager {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *          method does not allow the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @since   1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        InetAddress in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (in.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                in = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            SecurityManager s = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                s.checkConnect(in.getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            in = InetAddress.anyLocalAddress(); // "0.0.0.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * Returns the port number on the local host to which this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @return  the port number on the local host to which this socket is bound,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   827
                {@code -1} if the socket is closed, or
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   828
                {@code 0} if it is not bound yet.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /** Enable/disable SO_TIMEOUT with the specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *  milliseconds. With this option set to a non-zero timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *  a call to receive() for this DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *  will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *  a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *  DatagramSocket is still valid.  The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *  prior to entering the blocking operation to have effect.  The
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   847
     *  timeout must be {@code > 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *  A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @param timeout the specified timeout in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @return the setting for SO_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    public synchronized int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (getImpl() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Sets the SO_SNDBUF option to the specified value for this
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   886
     * {@code DatagramSocket}. The SO_SNDBUF option is used by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * network implementation as a hint to size the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * network I/O buffers. The SO_SNDBUF setting may also be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * by the network implementation to determine the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * of the packet that can be sent on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * As SO_SNDBUF is a hint, applications that want to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * what size the buffer is should call {@link #getSendBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Increasing the buffer size may allow multiple outgoing packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * to be queued by the network implementation when the send rate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * is high.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Note: If {@link #send(DatagramPacket)} is used to send a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   900
     * {@code DatagramPacket} that is larger than the setting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * of SO_SNDBUF then it is implementation specific if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * packet is sent or discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @param size the size to which to set the send buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @exception IllegalArgumentException if the value is 0 or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @see #getSendBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    public synchronized void setSendBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            throw new IllegalArgumentException("negative send size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   924
     * Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   925
     * buffer size used by the platform for output on this {@code DatagramSocket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   927
     * @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @see #setSendBufferSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    public synchronized int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Sets the SO_RCVBUF option to the specified value for this
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   945
     * {@code DatagramSocket}. The SO_RCVBUF option is used by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * the network implementation as a hint to size the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * network I/O buffers. The SO_RCVBUF setting may also be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * by the network implementation to determine the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * of the packet that can be received on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * Because SO_RCVBUF is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * verify what size the buffers were set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * {@link #getReceiveBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * Increasing SO_RCVBUF may allow the network implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * to buffer multiple packets when packets arrive faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * are being received using {@link #receive(DatagramPacket)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * Note: It is implementation specific if a packet larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * than SO_RCVBUF can be received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @exception IllegalArgumentException if the value is 0 or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @see #getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    public synchronized void setReceiveBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            throw new IllegalArgumentException("invalid receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   982
     * Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   983
     * buffer size used by the platform for input on this {@code DatagramSocket}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   985
     * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @exception SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * Enable/disable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * For UDP sockets it may be necessary to bind more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * socket to the same socket address. This is typically for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * purpose of receiving multicast packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * (See {@link java.net.MulticastSocket}). The
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1008
     * {@code SO_REUSEADDR} socket option allows multiple
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * sockets to be bound to the same socket address if the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1010
     * {@code SO_REUSEADDR} socket option is enabled prior
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * to binding the socket using {@link #bind(SocketAddress)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Note: This functionality is not supported by all existing platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * so it is implementation specific whether this option will be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * or not. However, if it is not supported then
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1016
     * {@link #getReuseAddress()} will always return {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1018
     * When a {@code DatagramSocket} is created the initial setting
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1019
     * of {@code SO_REUSEADDR} is disabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1021
     * The behaviour when {@code SO_REUSEADDR} is enabled or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * disabled after a socket is bound (See {@link #isBound()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @param on  whether to enable or disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @exception SocketException if an error occurs enabling or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1027
     *            disabling the {@code SO_RESUEADDR} socket option,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *            or the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public synchronized void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        // Integer instead of Boolean for compatibility with older DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            getImpl().setOption(SocketOptions.SO_REUSEADDR, new Integer(on?-1:0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Tests if SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1048
     * @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public synchronized boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        Object o = getImpl().getOption(SocketOptions.SO_REUSEADDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        return ((Boolean)o).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * Enable/disable SO_BROADCAST.
1817
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1063
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1064
     * <p> Some operating systems may require that the Java virtual machine be
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1065
     * started with implementation specific privileges to enable this option or
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1066
     * send broadcast datagrams.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1067
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1068
     * @param  on
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1069
     *         whether or not to have broadcast turned on.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1070
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1071
     * @throws  SocketException
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1072
     *          if there is an error in the underlying protocol, such as an UDP
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1073
     *          error.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1074
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @see #getBroadcast()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    public synchronized void setBroadcast(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        getImpl().setOption(SocketOptions.SO_BROADCAST, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Tests if SO_BROADCAST is enabled.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1086
     * @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see #setBroadcast(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public synchronized boolean getBroadcast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return ((Boolean)(getImpl().getOption(SocketOptions.SO_BROADCAST))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Sets traffic class or type-of-service octet in the IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * datagram header for datagrams sent from this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * As the underlying network implementation may ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * value applications should consider it a hint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
  1104
     * <P> The tc <B>must</B> be in the range {@code 0 <= tc <=
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
  1105
     * 255} or an IllegalArgumentException will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <p>Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <p>For Internet Protocol v4 the value consists of an
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1108
     * {@code integer}, the least significant 8 bits of which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * represent the value of the TOS octet in IP packets sent by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * RFC 1349 defines the TOS values as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * The last low order bit is always ignored as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * corresponds to the MBZ (must be zero) bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * Setting bits in the precedence field may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * SocketException indicating that the operation is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1126
     * for Internet Protocol v6 {@code tc} is the value that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * would be placed into the sin6_flowinfo field of the IP header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1129
     * @param tc        an {@code int} value for the bitset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * @throws SocketException if there is an error setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * traffic class or type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * @see #getTrafficClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    public synchronized void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        if (tc < 0 || tc > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            throw new IllegalArgumentException("tc is not in range 0 -- 255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * Gets traffic class or type-of-service in the IP datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * header for packets sent from this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * As the underlying network implementation may ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * traffic class or type-of-service set using {@link #setTrafficClass(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * this method may return a different value than was previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * set using the {@link #setTrafficClass(int)} method on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @return the traffic class or type-of-service already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @throws SocketException if there is an error obtaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * traffic class or type-of-service value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * @see #setTrafficClass(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    public synchronized int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        return ((Integer)(getImpl().getOption(SocketOptions.IP_TOS))).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * Closes this datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Any thread currently blocked in {@link #receive} upon this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * will throw a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * Returns whether the socket is closed or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Returns the unique {@link java.nio.channels.DatagramChannel} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * associated with this datagram socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <p> A datagram socket will have a channel if, and only if, the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * itself was created via the {@link java.nio.channels.DatagramChannel#open
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * DatagramChannel.open} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * @return  the datagram channel associated with this datagram socket,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1208
     *          or {@code null} if this socket was not created for a channel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    public DatagramChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * User defined factory for all datagram sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    static DatagramSocketImplFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * Sets the datagram socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * When an application creates a new datagram socket, the socket
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1227
     * implementation factory's {@code createDatagramSocketImpl} method is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * called to create the actual datagram socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1230
     * Passing {@code null} to the method is a no-op unless the factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * <p>If there is a security manager, this method first calls
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1234
     * the security manager's {@code checkSetFactory} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *              datagram socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @exception  SocketException  if the factory is already defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
  1243
     *             {@code checkSetFactory} method doesn't allow the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * @see       SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    public static synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
       throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
}