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