src/java.base/share/classes/java/net/MulticastSocket.java
author dfuchs
Mon, 18 Nov 2019 16:48:05 +0000
changeset 59124 d01fe40e9cd8
parent 58242 94bb65cb37d3
child 59201 b24f4caa1411
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
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
     2
 * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5180
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
    29
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
    31
import java.util.Set;
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
    32
import java.net.PortUnreachableException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The multicast datagram socket class is useful for sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and receiving IP multicast packets.  A MulticastSocket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * a (UDP) DatagramSocket, with additional capabilities for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * joining "groups" of other multicast hosts on the internet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A multicast group is specified by a class D IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and by a standard UDP port number. Class D IP addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * are in the range <CODE>224.0.0.0</CODE> to <CODE>239.255.255.255</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * inclusive. The address 224.0.0.0 is reserved and should not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * One would join a multicast group by first creating a MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * with the desired port, then invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <CODE>joinGroup(InetAddress groupAddr)</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * // join a Multicast group and send the group salutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * String msg = "Hello";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * InetAddress group = InetAddress.getByName("228.5.6.7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * MulticastSocket s = new MulticastSocket(6789);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * s.joinGroup(group);
54104
954399cb6959 8220585: Incorrect code in MulticastSocket sample code
aeubanks
parents: 47216
diff changeset
    56
 * byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8);
954399cb6959 8220585: Incorrect code in MulticastSocket sample code
aeubanks
parents: 47216
diff changeset
    57
 * DatagramPacket hi = new DatagramPacket(msgBytes, msgBytes.length,
954399cb6959 8220585: Incorrect code in MulticastSocket sample code
aeubanks
parents: 47216
diff changeset
    58
 *                                        group, 6789);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * s.send(hi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * // get their responses!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * byte[] buf = new byte[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * DatagramPacket recv = new DatagramPacket(buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * s.receive(recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * // OK, I'm done talking - leave the group...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * s.leaveGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * When one sends a message to a multicast group, <B>all</B> subscribing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * recipients to that host and port receive the message (within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * time-to-live range of the packet, see below).  The socket needn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * be a member of the multicast group to send messages to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * When a socket subscribes to a multicast group/port, it receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * datagrams sent by other hosts to the group/port, as do all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * members of the group and port.  A socket relinquishes membership
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * in a group by the leaveGroup(InetAddress addr) method.  <B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Multiple MulticastSocket's</B> may subscribe to a multicast group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * and port concurrently, and they will all receive group datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Currently applets are not allowed to use multicast sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Pavani Diwanji
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    84
 * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
class MulticastSocket extends DatagramSocket {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    88
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    89
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    90
     * Used on some platforms to record if an outgoing interface
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    91
     * has been set for this socket.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    92
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    93
    private boolean interfaceSet;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Create a multicast socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
    98
     * <p>
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
    99
     * If there is a security manager, its {@code checkListen} method is first
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   100
     * called with 0 as its argument to ensure the operation is allowed. This
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   101
     * could result in a SecurityException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * When the socket is created the
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   104
     * {@link DatagramSocket#setReuseAddress(boolean)} method is called to
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   105
     * enable the SO_REUSEADDR socket option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   107
     * @throws    IOException if an I/O exception occurs while creating the
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   108
     * MulticastSocket
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   109
     * @throws    SecurityException if a security manager exists and its
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   110
     * {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see java.net.DatagramSocket#setReuseAddress(boolean)
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   113
     * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public MulticastSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Create a multicast socket and bind it to a specific port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   123
     * its {@code checkListen} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   124
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * When the socket is created the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * {@link DatagramSocket#setReuseAddress(boolean)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * called to enable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   132
     * @param     port port to use
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   133
     * @throws    IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   134
     *            while creating the MulticastSocket
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   135
     * @throws    SecurityException  if a security manager exists and its
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   136
     *            {@code checkListen} method doesn't allow the operation.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   137
     * @see       SecurityManager#checkListen
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   138
     * @see       java.net.DatagramSocket#setReuseAddress(boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public MulticastSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this(new InetSocketAddress(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Create a MulticastSocket bound to the specified socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   147
     * Or, if the address is {@code null}, create an unbound socket.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   148
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   150
     * its {@code checkListen} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * with the SocketAddress port as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * When the socket is created the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * {@link DatagramSocket#setReuseAddress(boolean)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * called to enable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   158
     * @param    bindaddr Socket address to bind to, or {@code null} for
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   159
     *           an unbound socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   160
     * @throws   IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   161
     *           while creating the MulticastSocket
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   162
     * @throws   SecurityException  if a security manager exists and its
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   163
     *           {@code checkListen} method doesn't allow the operation.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   164
     * @see      SecurityManager#checkListen
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   165
     * @see      java.net.DatagramSocket#setReuseAddress(boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public MulticastSocket(SocketAddress bindaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        super((SocketAddress) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Enable SO_REUSEADDR before binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (bindaddr != null) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   176
            try {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   177
                bind(bindaddr);
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   178
            } finally {
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   179
                if (!isBound()) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   180
                    close();
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   181
                }
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   182
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * The lock on the socket's TTL. This is for set/getTTL and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * send(packet,ttl).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private Object ttlLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * The lock on the socket's interface - used by setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * and getInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private Object infLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The "last" interface set by setInterface on this MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private InetAddress infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Set the default time-to-live for multicast packets sent out
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   206
     * on this {@code MulticastSocket} in order to control the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14342
diff changeset
   210
     * in the range {@code 0 <= ttl <= 0xFF }.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   212
     * @param      ttl the time-to-live
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   213
     * @throws     IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   214
     *             while setting the default time-to-live value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @deprecated use the setTimeToLive method instead, which uses
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   216
     *             <b>int</b> instead of <b>byte</b> as the type for ttl.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see #getTTL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void setTTL(byte ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Set the default time-to-live for multicast packets sent out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * on this {@code MulticastSocket} in order to control the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <P> The ttl <B>must</B> be in the range {@code  0 <= ttl <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * 255} or an {@code IllegalArgumentException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Multicast packets sent with a TTL of {@code 0} are not transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * on the network but may be delivered locally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param  ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *         the time-to-live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          if an I/O exception occurs while setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *          default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see #getTimeToLive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void setTimeToLive(int ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (ttl < 0 || ttl > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new IllegalArgumentException("ttl out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        getImpl().setTimeToLive(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   258
     * @throws    IOException if an I/O exception occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * while getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @deprecated use the getTimeToLive method instead, which returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * an <b>int</b> instead of a <b>byte</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @see #setTTL(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public byte getTTL() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return getImpl().getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   275
     * @throws    IOException if an I/O exception occurs while
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see #setTimeToLive(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public int getTimeToLive() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return getImpl().getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Joins a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   288
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   291
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   292
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   297
     * @throws    IOException if there is an error joining, or when the address
39138
1e1d98dbb44b 8157166: Update spec to account for platforms that do not support multicast
chegar
parents: 38477
diff changeset
   298
     *            is not a multicast address, or the platform does not support
1e1d98dbb44b 8157166: Update spec to account for platforms that do not support multicast
chegar
parents: 38477
diff changeset
   299
     *            multicasting
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   300
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   301
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public void joinGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   310
        checkAddress(mcastaddr, "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   320
        /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   321
         * required for some platforms where it's not possible to join
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   322
         * a group without setting the interface first.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   323
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   324
        NetworkInterface defaultInterface = NetworkInterface.getDefault();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   325
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   326
        if (!interfaceSet && defaultInterface != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   327
            setNetworkInterface(defaultInterface);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   328
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   329
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        getImpl().join(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Leave a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   335
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   338
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   339
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param mcastaddr is the multicast address to leave
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   343
     * @throws    IOException if there is an error leaving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * or when the address is not a multicast address.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   345
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   346
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public void leaveGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   355
        checkAddress(mcastaddr, "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        getImpl().leave(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Joins the specified multicast group at the specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   372
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   373
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *        datagram packets, or <i>null</i> to defer to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *       {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *       {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   382
     * @throws    IOException if there is an error joining, or when the address
39138
1e1d98dbb44b 8157166: Update spec to account for platforms that do not support multicast
chegar
parents: 38477
diff changeset
   383
     *            is not a multicast address, or the platform does not support
1e1d98dbb44b 8157166: Update spec to account for platforms that do not support multicast
chegar
parents: 38477
diff changeset
   384
     *            multicasting
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   385
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   386
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   404
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        getImpl().joinGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Leave a multicast group on a specified local interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   421
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   422
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param mcastaddr is the multicast address to leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param netIf specifies the local interface or <i>null</i> to defer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *             to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *             {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *             {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   430
     * @throws    IOException if there is an error leaving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * or when the address is not a multicast address.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   432
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   433
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   451
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        getImpl().leaveGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Set the multicast network interface used by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * whose behavior would be affected by the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * network interface. Useful for multihomed hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param inf the InetAddress
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   469
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see #getInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public void setInterface(InetAddress inf) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   477
        checkAddress(inf, "setInterface");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            infAddress = inf;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   481
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Retrieve the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   489
     * @return An {@code InetAddress} representing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *  the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *  multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   493
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see #setInterface(java.net.InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public InetAddress getInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            InetAddress ia =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                (InetAddress)getImpl().getOption(SocketOptions.IP_MULTICAST_IF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             * No previous setInterface or interface can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             * set using setNetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (infAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             * Same interface set with setInterface?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (ia.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                return ia;
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
             * Different InetAddress from what we set with setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             * so enumerate the current interface to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             * address set by setInterface is bound to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   528
                Enumeration<InetAddress> addrs = ni.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                while (addrs.hasMoreElements()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   530
                    InetAddress addr = addrs.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    if (addr.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        return infAddress;
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
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 * No match so reset infAddress to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 * interface has changed via means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Specify the network interface for outgoing multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * sent on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param netIf the interface
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   553
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @see #getNetworkInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public void setNetworkInterface(NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            infAddress = null;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   564
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Get the multicast network interface set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   571
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * the underlying protocol, such as a TCP error.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   573
     * @return the multicast {@code NetworkInterface} currently set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @see #setNetworkInterface(NetworkInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public NetworkInterface getNetworkInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        NetworkInterface ni
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            = (NetworkInterface)getImpl().getOption(SocketOptions.IP_MULTICAST_IF2);
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   580
        if ((ni.getIndex() == 0) || (ni.getIndex() == -1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            InetAddress[] addrs = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            addrs[0] = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return new NetworkInterface(addrs[0].getHostName(), 0, addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Disable/Enable local loopback of multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * The option is used by the platform's networking code as a hint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * for setting whether multicast data will be looped back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * the local socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <p>Because this option is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * verify what loopback mode is set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * {@link #getLoopbackMode()}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   598
     * @param disable {@code true} to disable the LoopbackMode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @throws SocketException if an error occurs while setting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see #getLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public void setLoopbackMode(boolean disable) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Get the setting for local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @throws SocketException  if an error occurs while getting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return true if the LoopbackMode has been disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @see #setLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public boolean getLoopbackMode() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return ((Boolean)getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Sends a datagram packet to the destination, with a TTL (time-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * to-live) other than the default for the socket.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * need only be used in instances where a particular TTL is desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * otherwise it is preferable to set a TTL once on the socket, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * use that default TTL for all packets.  This method does <B>not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * </B> alter the default TTL for the socket. Its behavior may be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   626
     * affected by {@code setInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p>If there is a security manager, this method first performs some
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   629
     * security checks. First, if {@code p.getAddress().isMulticastAddress()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * is true, this method calls the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   631
     * security manager's {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   632
     * with {@code p.getAddress()} and {@code ttl} as its arguments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * If the evaluation of that expression is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * this method instead calls the security manager's
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   635
     * {@code checkConnect} method with arguments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   636
     * {@code p.getAddress().getHostAddress()} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   637
     * {@code p.getPort()}. Each call to a security manager method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * could result in a SecurityException if the operation is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param p is the packet to be sent. The packet should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * the destination multicast ip address and the data to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * One does not need to be the member of the group to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * packets to a destination multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param ttl optional time to live for multicast packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * default ttl is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   647
     * @throws     IOException is raised if an error occurs i.e
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   648
     *             error while setting ttl.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   649
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   650
     *             {@code checkMulticast} or {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *             method doesn't allow the send.
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   652
     * @throws     PortUnreachableException may be thrown if the socket is connected
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   653
     *             to a currently unreachable destination. Note, there is no
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   654
     *             guarantee that the exception will be thrown.
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   655
     * @throws     IllegalArgumentException if the socket is connected,
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   656
     *             and connected address and packet address differ, or
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   657
     *             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: 58242
diff changeset
   658
     *             is not set.
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   659
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @deprecated Use the following code or its equivalent instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *  int ttl = mcastSocket.getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *  mcastSocket.setTimeToLive(newttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *  mcastSocket.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *  mcastSocket.setTimeToLive(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @see DatagramSocket#send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @see DatagramSocket#receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @see SecurityManager#checkMulticast(java.net.InetAddress, byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public void send(DatagramPacket p, byte ttl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            synchronized(ttlLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                synchronized(p) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   681
                    InetAddress packetAddress = p.getAddress();
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   682
                    checkAddress(packetAddress, "send");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    if (connectState == ST_NOT_CONNECTED) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   684
                        if (packetAddress == null) {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   685
                            throw new IllegalArgumentException("Address not set");
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   686
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        // Security manager makes sure that the multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        // is allowed one and that the ttl used is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        // than the allowed maxttl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        if (security != null) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   692
                            if (packetAddress.isMulticastAddress()) {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   693
                                security.checkMulticast(packetAddress, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            } else {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   695
                                security.checkConnect(packetAddress.getHostAddress(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                                      p.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        // we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        if (packetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            p.setAddress(connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            p.setPort(connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        } else if ((!packetAddress.equals(connectedAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                   p.getPort() != connectedPort) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   706
                            throw new IllegalArgumentException("connected address and packet address" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                                        " differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    byte dttl = getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            // set the ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                            getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        // call the datagram method to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        getImpl().send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        // set it back to default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            getImpl().setTTL(dttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                } // synch p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }  //synch ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    } //method
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   727
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   728
    private static Set<SocketOption<?>> options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   729
    private static boolean optionsSet = false;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   730
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   731
    @Override
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   732
    public Set<SocketOption<?>> supportedOptions() {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   733
        synchronized (MulticastSocket.class) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   734
            if (optionsSet) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   735
                return options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   736
            }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   737
            try {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   738
                DatagramSocketImpl impl = getImpl();
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   739
                options = Collections.unmodifiableSet(impl.supportedOptions());
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   740
            } catch (SocketException ex) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   741
                options = Collections.emptySet();
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   742
            }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   743
            optionsSet = true;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   744
            return options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   745
        }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   746
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
}