< prev index next >

src/java.base/share/classes/java/net/MulticastSocket.java

Print this page
rev 57619 : [mq]: MulticastSocketAdaptor
   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.util.Collections;
  30 import java.util.Enumeration;
  31 import java.util.Set;

  32 /**
  33  * The multicast datagram socket class is useful for sending
  34  * and receiving IP multicast packets. A MulticastSocket is
  35  * a (UDP) DatagramSocket, with additional capabilities for
  36  * joining "groups" of other multicast hosts on the internet.
  37  * <P>
  38  * A multicast group is specified by a class D IP address
  39  * and by a standard UDP port number. Class D IP addresses
  40  * are in the range {@code 224.0.0.0} to {@code 239.255.255.255},
  41  * inclusive. The address 224.0.0.0 is reserved and should not be used.
  42  * <P>
  43  * One would join a multicast group by first creating a MulticastSocket
  44  * with the desired port, then invoking the
  45  * <CODE>joinGroup(InetAddress groupAddr)</CODE>
  46  * method:
  47  * <PRE>
  48  * // join a Multicast group and send the group salutations
  49  * ...
  50  * String msg = "Hello";
  51  * InetAddress mcastaddr = InetAddress.getByName("228.5.6.7");


 191      * This could result in a SecurityException.
 192      * <p>
 193      * When the socket is created the
 194      * {@link DatagramSocket#setReuseAddress(boolean)} method is
 195      * called to enable the SO_REUSEADDR socket option.
 196      *
 197      * @param    bindaddr Socket address to bind to, or {@code null} for
 198      *           an unbound socket.
 199      * @throws   IOException if an I/O exception occurs
 200      *           while creating the MulticastSocket
 201      * @throws   SecurityException  if a security manager exists and its
 202      *           {@code checkListen} method doesn't allow the operation.
 203      * @see      SecurityManager#checkListen
 204      * @see      java.net.DatagramSocket#setReuseAddress(boolean)
 205      *
 206      * @since 1.4
 207      */
 208     public MulticastSocket(SocketAddress bindaddr) throws IOException {
 209         super((SocketAddress) null);
 210 




 211         // Enable SO_REUSEADDR before binding
 212         setReuseAddress(true);
 213 
 214         if (bindaddr != null) {
 215             try {
 216                 bind(bindaddr);
 217             } finally {
 218                 if (!isBound()) {
 219                     close();
 220                 }
 221             }
 222         }
 223     }
 224 
 225     /**
 226      * The lock on the socket's TTL. This is for set/getTTL and
 227      * send(packet,ttl).
 228      */
 229     private Object ttlLock = new Object();
 230 


   1 /*
   2  * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.util.Collections;
  30 import java.util.Enumeration;
  31 import java.util.Set;
  32 
  33 /**
  34  * The multicast datagram socket class is useful for sending
  35  * and receiving IP multicast packets. A MulticastSocket is
  36  * a (UDP) DatagramSocket, with additional capabilities for
  37  * joining "groups" of other multicast hosts on the internet.
  38  * <P>
  39  * A multicast group is specified by a class D IP address
  40  * and by a standard UDP port number. Class D IP addresses
  41  * are in the range {@code 224.0.0.0} to {@code 239.255.255.255},
  42  * inclusive. The address 224.0.0.0 is reserved and should not be used.
  43  * <P>
  44  * One would join a multicast group by first creating a MulticastSocket
  45  * with the desired port, then invoking the
  46  * <CODE>joinGroup(InetAddress groupAddr)</CODE>
  47  * method:
  48  * <PRE>
  49  * // join a Multicast group and send the group salutations
  50  * ...
  51  * String msg = "Hello";
  52  * InetAddress mcastaddr = InetAddress.getByName("228.5.6.7");


 192      * This could result in a SecurityException.
 193      * <p>
 194      * When the socket is created the
 195      * {@link DatagramSocket#setReuseAddress(boolean)} method is
 196      * called to enable the SO_REUSEADDR socket option.
 197      *
 198      * @param    bindaddr Socket address to bind to, or {@code null} for
 199      *           an unbound socket.
 200      * @throws   IOException if an I/O exception occurs
 201      *           while creating the MulticastSocket
 202      * @throws   SecurityException  if a security manager exists and its
 203      *           {@code checkListen} method doesn't allow the operation.
 204      * @see      SecurityManager#checkListen
 205      * @see      java.net.DatagramSocket#setReuseAddress(boolean)
 206      *
 207      * @since 1.4
 208      */
 209     public MulticastSocket(SocketAddress bindaddr) throws IOException {
 210         super((SocketAddress) null);
 211 
 212         // No further initialization when this is a DatagramChannel socket adaptor
 213         if (this instanceof sun.nio.ch.DatagramSocketAdaptor)
 214             return;
 215 
 216         // Enable SO_REUSEADDR before binding
 217         setReuseAddress(true);
 218 
 219         if (bindaddr != null) {
 220             try {
 221                 bind(bindaddr);
 222             } finally {
 223                 if (!isBound()) {
 224                     close();
 225                 }
 226             }
 227         }
 228     }
 229 
 230     /**
 231      * The lock on the socket's TTL. This is for set/getTTL and
 232      * send(packet,ttl).
 233      */
 234     private Object ttlLock = new Object();
 235 


< prev index next >