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