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