jdk/src/java.base/share/classes/java/net/MulticastSocket.java
author alanb
Tue, 23 Feb 2016 17:41:00 +0000
changeset 36115 0676e37a0b9c
parent 28549 a80ec9ad6324
child 38477 f462865d453d
permissions -rw-r--r--
6432031: Add support for SO_REUSEPORT Reviewed-by: alanb, simonis, chegar Contributed-by: yingqi.lu@intel.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
     2
 * Copyright (c) 1995, 2016, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The multicast datagram socket class is useful for sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and receiving IP multicast packets.  A MulticastSocket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a (UDP) DatagramSocket, with additional capabilities for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * joining "groups" of other multicast hosts on the internet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A multicast group is specified by a class D IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and by a standard UDP port number. Class D IP addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * are in the range <CODE>224.0.0.0</CODE> to <CODE>239.255.255.255</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * inclusive. The address 224.0.0.0 is reserved and should not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * One would join a multicast group by first creating a MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with the desired port, then invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <CODE>joinGroup(InetAddress groupAddr)</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * // join a Multicast group and send the group salutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * String msg = "Hello";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * InetAddress group = InetAddress.getByName("228.5.6.7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * MulticastSocket s = new MulticastSocket(6789);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * s.joinGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *                             group, 6789);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * s.send(hi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * // get their responses!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * byte[] buf = new byte[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * DatagramPacket recv = new DatagramPacket(buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * s.receive(recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * // OK, I'm done talking - leave the group...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * s.leaveGroup(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * When one sends a message to a multicast group, <B>all</B> subscribing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * recipients to that host and port receive the message (within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * time-to-live range of the packet, see below).  The socket needn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * be a member of the multicast group to send messages to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * When a socket subscribes to a multicast group/port, it receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * datagrams sent by other hosts to the group/port, as do all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * members of the group and port.  A socket relinquishes membership
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * in a group by the leaveGroup(InetAddress addr) method.  <B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Multiple MulticastSocket's</B> may subscribe to a multicast group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * and port concurrently, and they will all receive group datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Currently applets are not allowed to use multicast sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @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
    80
 * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
class MulticastSocket extends DatagramSocket {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    84
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    85
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    86
     * Used on some platforms to record if an outgoing interface
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    87
     * has been set for this socket.
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
    private boolean interfaceSet;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
    90
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Create a multicast socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>If there is a security manager,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
    95
     * its {@code checkListen} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * with 0 as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * When the socket is created the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@link DatagramSocket#setReuseAddress(boolean)} method is
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   101
     * called to enable the SO_REUSEADDR socket option. When
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   102
     * {@link StandardSocketOptions#SO_REUSEPORT SO_REUSEPORT} is
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   103
     * supported then
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   104
     * {@link DatagramSocketImpl#setOption(SocketOption, Object)}
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   105
     * is called to enable the socket option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * while creating the MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param port port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * while creating the MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   136
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see java.net.DatagramSocket#setReuseAddress(boolean)
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
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   158
     * @param bindaddr Socket address to bind to, or {@code null} for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                 an unbound socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * while creating the MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   163
     *             {@code checkListen} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see java.net.DatagramSocket#setReuseAddress(boolean)
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
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   175
        // Enable SO_REUSEPORT if supported before binding
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   176
        if (supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   177
            this.setOption(StandardSocketOptions.SO_REUSEPORT, true);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   178
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 28549
diff changeset
   179
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (bindaddr != null) {
10437
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   181
            try {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   182
                bind(bindaddr);
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   183
            } finally {
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   184
                if (!isBound())
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   185
                    close();
dfca69ed7f87 7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents: 5506
diff changeset
   186
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * The lock on the socket's TTL. This is for set/getTTL and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * send(packet,ttl).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private Object ttlLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The lock on the socket's interface - used by setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * and getInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private Object infLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * The "last" interface set by setInterface on this MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private InetAddress infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Set the default time-to-live for multicast packets sent out
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   210
     * on this {@code MulticastSocket} in order to control the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <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
   214
     * in the range {@code 0 <= ttl <= 0xFF }.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param ttl the time-to-live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * while setting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @deprecated use the setTimeToLive method instead, which uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <b>int</b> instead of <b>byte</b> as the type for ttl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see #getTTL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void setTTL(byte ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Set the default time-to-live for multicast packets sent out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * on this {@code MulticastSocket} in order to control the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * scope of the multicasts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <P> The ttl <B>must</B> be in the range {@code  0 <= ttl <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * 255} or an {@code IllegalArgumentException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Multicast packets sent with a TTL of {@code 0} are not transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * on the network but may be delivered locally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param  ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         the time-to-live
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *          if an I/O exception occurs while setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see #getTimeToLive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void setTimeToLive(int ttl) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (ttl < 0 || ttl > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new IllegalArgumentException("ttl out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        getImpl().setTimeToLive(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * while getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @deprecated use the getTimeToLive method instead, which returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * an <b>int</b> instead of a <b>byte</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setTTL(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public byte getTTL() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return getImpl().getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Get the default time-to-live for multicast packets sent out on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception IOException if an I/O exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * getting the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return the default time-to-live value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see #setTimeToLive(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public int getTimeToLive() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return getImpl().getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Joins a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   292
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   295
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   296
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception IOException if there is an error joining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * or when the address is not a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   304
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void joinGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   313
        checkAddress(mcastaddr, "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
12047
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
         * 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
   325
         * a group without setting the interface first.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   326
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   327
        NetworkInterface defaultInterface = NetworkInterface.getDefault();
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
        if (!interfaceSet && defaultInterface != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   330
            setNetworkInterface(defaultInterface);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   331
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   332
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        getImpl().join(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Leave a multicast group. Its behavior may be affected by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   338
     * {@code setInterface} or {@code setNetworkInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   341
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   342
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param mcastaddr is the multicast address to leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @exception IOException if there is an error leaving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * or when the address is not a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   349
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void leaveGroup(InetAddress mcastaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   358
        checkAddress(mcastaddr, "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            security.checkMulticast(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (!mcastaddr.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        getImpl().leave(mcastaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Joins the specified multicast group at the specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   375
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   376
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param mcastaddr is the multicast address to join
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *        datagram packets, or <i>null</i> to defer to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *       {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *       {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @exception IOException if there is an error joining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * or when the address is not a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   388
     * {@code checkMulticast} method doesn't allow the join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   406
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        getImpl().joinGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Leave a multicast group on a specified local interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>If there is a security manager, this method first
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   423
     * calls its {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   424
     * with the {@code mcastaddr} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param mcastaddr is the multicast address to leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param netIf specifies the local interface or <i>null</i> to defer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *             to the interface set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *             {@link MulticastSocket#setInterface(InetAddress)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *             {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @exception IOException if there is an error leaving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * or when the address is not a multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   435
     * {@code checkMulticast} method doesn't allow the operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   453
        checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            throw new SocketException("Not a multicast address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        getImpl().leaveGroup(mcastaddr, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Set the multicast network interface used by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * whose behavior would be affected by the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * network interface. Useful for multihomed hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param inf the InetAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @see #getInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public void setInterface(InetAddress inf) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   479
        checkAddress(inf, "setInterface");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            infAddress = inf;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   483
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Retrieve the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   491
     * @return An {@code InetAddress} representing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *  the address of the network interface used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *  multicast packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @see #setInterface(java.net.InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public InetAddress getInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            InetAddress ia =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                (InetAddress)getImpl().getOption(SocketOptions.IP_MULTICAST_IF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             * No previous setInterface or interface can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             * set using setNetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (infAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             * Same interface set with setInterface?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (ia.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             * Different InetAddress from what we set with setInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             * so enumerate the current interface to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             * address set by setInterface is bound to this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   530
                Enumeration<InetAddress> addrs = ni.getInetAddresses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                while (addrs.hasMoreElements()) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10437
diff changeset
   532
                    InetAddress addr = addrs.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    if (addr.equals(infAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        return infAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 * No match so reset infAddress to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                 * interface has changed via means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                infAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                return ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return ia;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Specify the network interface for outgoing multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * sent on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param netIf the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * the underlying protocol, such as a TCP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @see #getNetworkInterface()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void setNetworkInterface(NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        synchronized (infLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            infAddress = null;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10596
diff changeset
   566
            interfaceSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Get the multicast network interface set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * the underlying protocol, such as a TCP error.
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   575
     * @return the multicast {@code NetworkInterface} currently set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see #setNetworkInterface(NetworkInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public NetworkInterface getNetworkInterface() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        NetworkInterface ni
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            = (NetworkInterface)getImpl().getOption(SocketOptions.IP_MULTICAST_IF2);
28549
a80ec9ad6324 8056264: Multicast support improvements
msheppar
parents: 25859
diff changeset
   582
        if ((ni.getIndex() == 0) || (ni.getIndex() == -1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            InetAddress[] addrs = new InetAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            addrs[0] = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return new NetworkInterface(addrs[0].getHostName(), 0, addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Disable/Enable local loopback of multicast datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * The option is used by the platform's networking code as a hint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * for setting whether multicast data will be looped back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * the local socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>Because this option is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * verify what loopback mode is set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * {@link #getLoopbackMode()}
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   600
     * @param disable {@code true} to disable the LoopbackMode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @throws SocketException if an error occurs while setting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see #getLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public void setLoopbackMode(boolean disable) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Get the setting for local loopback of multicast datagrams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @throws SocketException  if an error occurs while getting the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @return true if the LoopbackMode has been disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see #setLoopbackMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public boolean getLoopbackMode() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return ((Boolean)getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Sends a datagram packet to the destination, with a TTL (time-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * to-live) other than the default for the socket.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * need only be used in instances where a particular TTL is desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * otherwise it is preferable to set a TTL once on the socket, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * use that default TTL for all packets.  This method does <B>not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * </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
   628
     * affected by {@code setInterface}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <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
   631
     * security checks. First, if {@code p.getAddress().isMulticastAddress()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * is true, this method calls the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   633
     * security manager's {@code checkMulticast} method
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   634
     * with {@code p.getAddress()} and {@code ttl} as its arguments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * If the evaluation of that expression is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * this method instead calls the security manager's
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   637
     * {@code checkConnect} method with arguments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   638
     * {@code p.getAddress().getHostAddress()} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   639
     * {@code p.getPort()}. Each call to a security manager method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * could result in a SecurityException if the operation is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param p is the packet to be sent. The packet should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * the destination multicast ip address and the data to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * One does not need to be the member of the group to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * packets to a destination multicast address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param ttl optional time to live for multicast packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * default ttl is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @exception IOException is raised if an error occurs i.e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * error while setting ttl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @exception  SecurityException  if a security manager exists and its
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18156
diff changeset
   652
     *             {@code checkMulticast} or {@code checkConnect}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *             method doesn't allow the send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @deprecated Use the following code or its equivalent instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *  int ttl = mcastSocket.getTimeToLive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *  mcastSocket.setTimeToLive(newttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *  mcastSocket.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *  mcastSocket.setTimeToLive(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *  ......
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @see DatagramSocket#send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @see DatagramSocket#receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @see SecurityManager#checkMulticast(java.net.InetAddress, byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public void send(DatagramPacket p, byte ttl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                throw new SocketException("Socket is closed");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 2
diff changeset
   673
            checkAddress(p.getAddress(), "send");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            synchronized(ttlLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                synchronized(p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    if (connectState == ST_NOT_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        // Security manager makes sure that the multicast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        // is allowed one and that the ttl used is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        // than the allowed maxttl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                            if (p.getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                security.checkMulticast(p.getAddress(), ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                security.checkConnect(p.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                                      p.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        // we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        InetAddress packetAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        packetAddress = p.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        if (packetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            p.setAddress(connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            p.setPort(connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        } else if ((!packetAddress.equals(connectedAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                   p.getPort() != connectedPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            throw new SecurityException("connected address and packet address" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                                        " differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    byte dttl = getTTL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            // set the ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            getImpl().setTTL(ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        // call the datagram method to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        getImpl().send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        // set it back to default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        if (ttl != dttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            getImpl().setTTL(dttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                } // synch p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }  //synch ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    } //method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
}