src/java.base/share/classes/java/net/MulticastSocket.java
author pconcannon
Thu, 28 Nov 2019 16:49:52 +0000
changeset 59313 6f12009ea9d7
parent 59201 b24f4caa1411
permissions -rw-r--r--
8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set Summary: The MulticastSocket method getOption has been changed to conform to the behavior described in StandardSocketOptions.IP_MULTICAST_IF. Reviewed-by: chegar, dfuchs
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
 */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 59124
diff changeset
    86
public class MulticastSocket extends DatagramSocket {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    87
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
     * Used on some platforms to record if an outgoing interface
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    90
     * has been set for this socket.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    91
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    92
    private boolean interfaceSet;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Create a multicast socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
    97
     * <p>
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
    98
     * 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
    99
     * 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
   100
     * could result in a SecurityException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * When the socket is created the
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   103
     * {@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
   104
     * enable the SO_REUSEADDR socket option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   106
     * @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
   107
     * MulticastSocket
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   108
     * @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
   109
     * {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see java.net.DatagramSocket#setReuseAddress(boolean)
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   112
     * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public MulticastSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Create a multicast socket and bind it to a specific port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   122
     * its {@code checkListen} method is first called
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   123
     * with the {@code port} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * When the socket is created the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * {@link DatagramSocket#setReuseAddress(boolean)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * called to enable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   131
     * @param     port port to use
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   132
     * @throws    IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   133
     *            while creating the MulticastSocket
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   134
     * @throws    SecurityException  if a security manager exists and its
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   135
     *            {@code checkListen} method doesn't allow the operation.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   136
     * @see       SecurityManager#checkListen
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   137
     * @see       java.net.DatagramSocket#setReuseAddress(boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public MulticastSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this(new InetSocketAddress(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Create a MulticastSocket bound to the specified socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   146
     * Or, if the address is {@code null}, create an unbound socket.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   147
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   149
     * its {@code checkListen} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * with the SocketAddress port as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * When the socket is created the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * {@link DatagramSocket#setReuseAddress(boolean)} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * called to enable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   157
     * @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
   158
     *           an unbound socket.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   159
     * @throws   IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   160
     *           while creating the MulticastSocket
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   161
     * @throws   SecurityException  if a security manager exists and its
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   162
     *           {@code checkListen} method doesn't allow the operation.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   163
     * @see      SecurityManager#checkListen
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   164
     * @see      java.net.DatagramSocket#setReuseAddress(boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public MulticastSocket(SocketAddress bindaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        super((SocketAddress) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // Enable SO_REUSEADDR before binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (bindaddr != null) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   175
            try {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   176
                bind(bindaddr);
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   177
            } finally {
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   178
                if (!isBound()) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   179
                    close();
41228
bb9385e8c619 8153674: Expected SocketException not thrown when calling bind() with setReuseAddress(false)
vtewari
parents: 39138
diff changeset
   180
                }
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   181
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
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
     * The lock on the socket's TTL. This is for set/getTTL and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * send(packet,ttl).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private Object ttlLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The lock on the socket's interface - used by setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and getInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private Object infLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * The "last" interface set by setInterface on this MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private InetAddress infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Set the default time-to-live for multicast packets sent out
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   205
     * on this {@code MulticastSocket} in order to control the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <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
   209
     * in the range {@code 0 <= ttl <= 0xFF }.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   211
     * @param      ttl the time-to-live
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   212
     * @throws     IOException if an I/O exception occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   213
     *             while setting the default time-to-live value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @deprecated use the setTimeToLive method instead, which uses
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   215
     *             <b>int</b> instead of <b>byte</b> as the type for ttl.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see #getTTL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void setTTL(byte ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Set the default time-to-live for multicast packets sent out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * on this {@code MulticastSocket} in order to control the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <P> The ttl <B>must</B> be in the range {@code  0 <= ttl <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * 255} or an {@code IllegalArgumentException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Multicast packets sent with a TTL of {@code 0} are not transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * on the network but may be delivered locally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param  ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *         the time-to-live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          if an I/O exception occurs while setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see #getTimeToLive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void setTimeToLive(int ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (ttl < 0 || ttl > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new IllegalArgumentException("ttl out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        getImpl().setTimeToLive(ttl);
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
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   257
     * @throws    IOException if an I/O exception occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * while getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @deprecated use the getTimeToLive method instead, which returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * an <b>int</b> instead of a <b>byte</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see #setTTL(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public byte getTTL() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return getImpl().getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   274
     * @throws    IOException if an I/O exception occurs while
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see #setTimeToLive(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public int getTimeToLive() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return getImpl().getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Joins a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   287
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   290
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   291
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   296
     * @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
   297
     *            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
   298
     *            multicasting
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   299
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   300
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void joinGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   309
        checkAddress(mcastaddr, "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   319
        /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   320
         * 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
   321
         * a group without setting the interface first.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   322
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   323
        NetworkInterface defaultInterface = NetworkInterface.getDefault();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   324
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   325
        if (!interfaceSet && defaultInterface != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   326
            setNetworkInterface(defaultInterface);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   327
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   328
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        getImpl().join(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Leave a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   334
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   337
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   338
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param mcastaddr is the multicast address to leave
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   342
     * @throws    IOException if there is an error leaving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * 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
   344
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   345
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void leaveGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   354
        checkAddress(mcastaddr, "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        getImpl().leave(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Joins the specified multicast group at the specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   371
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   372
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *        datagram packets, or <i>null</i> to defer to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *       {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *       {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   381
     * @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
   382
     *            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
   383
     *            multicasting
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   384
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   385
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   403
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        getImpl().joinGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Leave a multicast group on a specified local interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   420
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   421
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param mcastaddr is the multicast address to leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param netIf specifies the local interface or <i>null</i> to defer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *             to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *             {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *             {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   429
     * @throws    IOException if there is an error leaving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * 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
   431
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   432
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   450
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        getImpl().leaveGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Set the multicast network interface used by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * whose behavior would be affected by the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * network interface. Useful for multihomed hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @param inf the InetAddress
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   468
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @see #getInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public void setInterface(InetAddress inf) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   476
        checkAddress(inf, "setInterface");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            infAddress = inf;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   480
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
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
     * Retrieve the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   488
     * @return An {@code InetAddress} representing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *  the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *  multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   492
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see #setInterface(java.net.InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public InetAddress getInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            InetAddress ia =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                (InetAddress)getImpl().getOption(SocketOptions.IP_MULTICAST_IF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             * No previous setInterface or interface can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             * set using setNetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (infAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             * Same interface set with setInterface?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (ia.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                return ia;
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
             * Different InetAddress from what we set with setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
             * so enumerate the current interface to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             * address set by setInterface is bound to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   527
                Enumeration<InetAddress> addrs = ni.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                while (addrs.hasMoreElements()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   529
                    InetAddress addr = addrs.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    if (addr.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        return infAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    }
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
                 * No match so reset infAddress to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 * interface has changed via means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
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
     * Specify the network interface for outgoing multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * sent on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param netIf the interface
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   552
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @see #getNetworkInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public void setNetworkInterface(NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            infAddress = null;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   563
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
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
     * Get the multicast network interface set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   570
     * @throws    SocketException if there is an error in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * the underlying protocol, such as a TCP error.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   572
     * @return the multicast {@code NetworkInterface} currently set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @see #setNetworkInterface(NetworkInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public NetworkInterface getNetworkInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        NetworkInterface ni
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            = (NetworkInterface)getImpl().getOption(SocketOptions.IP_MULTICAST_IF2);
59313
6f12009ea9d7 8233307: MulticastSocket getOption(IP_MULTICAST_IF) returns interface when not set
pconcannon
parents: 59201
diff changeset
   579
        if (ni == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            InetAddress[] addrs = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            addrs[0] = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return new NetworkInterface(addrs[0].getHostName(), 0, addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
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
     * Disable/Enable local loopback of multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * The option is used by the platform's networking code as a hint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * for setting whether multicast data will be looped back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * the local socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p>Because this option is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * verify what loopback mode is set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * {@link #getLoopbackMode()}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   597
     * @param disable {@code true} to disable the LoopbackMode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @throws SocketException if an error occurs while setting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see #getLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public void setLoopbackMode(boolean disable) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Get the setting for local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @throws SocketException  if an error occurs while getting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @return true if the LoopbackMode has been disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @see #setLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public boolean getLoopbackMode() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return ((Boolean)getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Sends a datagram packet to the destination, with a TTL (time-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * to-live) other than the default for the socket.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * need only be used in instances where a particular TTL is desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * otherwise it is preferable to set a TTL once on the socket, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * use that default TTL for all packets.  This method does <B>not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * </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
   625
     * affected by {@code setInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <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
   628
     * security checks. First, if {@code p.getAddress().isMulticastAddress()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * is true, this method calls the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   630
     * security manager's {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   631
     * with {@code p.getAddress()} and {@code ttl} as its arguments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * If the evaluation of that expression is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * this method instead calls the security manager's
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   634
     * {@code checkConnect} method with arguments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   635
     * {@code p.getAddress().getHostAddress()} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   636
     * {@code p.getPort()}. Each call to a security manager method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * could result in a SecurityException if the operation is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param p is the packet to be sent. The packet should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * the destination multicast ip address and the data to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * One does not need to be the member of the group to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * packets to a destination multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param ttl optional time to live for multicast packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * default ttl is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   646
     * @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
   647
     *             error while setting ttl.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54104
diff changeset
   648
     * @throws     SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   649
     *             {@code checkMulticast} or {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *             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
   651
     * @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
   652
     *             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
   653
     *             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
   654
     * @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
   655
     *             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
   656
     *             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
   657
     *             is not set.
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   658
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @deprecated Use the following code or its equivalent instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *  int ttl = mcastSocket.getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *  mcastSocket.setTimeToLive(newttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *  mcastSocket.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *  mcastSocket.setTimeToLive(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @see DatagramSocket#send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @see DatagramSocket#receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @see SecurityManager#checkMulticast(java.net.InetAddress, byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public void send(DatagramPacket p, byte ttl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            synchronized(ttlLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                synchronized(p) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   680
                    InetAddress packetAddress = p.getAddress();
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   681
                    checkAddress(packetAddress, "send");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    if (connectState == ST_NOT_CONNECTED) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   683
                        if (packetAddress == null) {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   684
                            throw new IllegalArgumentException("Address not set");
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   685
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        // Security manager makes sure that the multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        // is allowed one and that the ttl used is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        // than the allowed maxttl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        if (security != null) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   691
                            if (packetAddress.isMulticastAddress()) {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   692
                                security.checkMulticast(packetAddress, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            } else {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   694
                                security.checkConnect(packetAddress.getHostAddress(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                                      p.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        // we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        if (packetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            p.setAddress(connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            p.setPort(connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        } else if ((!packetAddress.equals(connectedAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                   p.getPort() != connectedPort) {
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 58242
diff changeset
   705
                            throw new IllegalArgumentException("connected address and packet address" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                                        " differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    byte dttl = getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            // set the ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        // call the datagram method to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        getImpl().send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        // set it back to default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            getImpl().setTTL(dttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                } // synch p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }  //synch ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    } //method
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   726
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   727
    private static Set<SocketOption<?>> options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   728
    private static boolean optionsSet = false;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   729
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   730
    @Override
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   731
    public Set<SocketOption<?>> supportedOptions() {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   732
        synchronized (MulticastSocket.class) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   733
            if (optionsSet) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   734
                return options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   735
            }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   736
            try {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   737
                DatagramSocketImpl impl = getImpl();
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   738
                options = Collections.unmodifiableSet(impl.supportedOptions());
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   739
            } catch (SocketException ex) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   740
                options = Collections.emptySet();
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   741
            }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   742
            optionsSet = true;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   743
            return options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   744
        }
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   745
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
}