< prev index next >

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

Print this page




  76  * Multiple MulticastSocket's</B> may subscribe to a multicast group
  77  * and port concurrently, and they will all receive group datagrams.
  78  * <P>
  79  * Currently applets are not allowed to use multicast sockets.
  80  *
  81  * @author Pavani Diwanji
  82  * @since  1.1
  83  */
  84 public
  85 class MulticastSocket extends DatagramSocket {
  86 
  87     /**
  88      * Used on some platforms to record if an outgoing interface
  89      * has been set for this socket.
  90      */
  91     private boolean interfaceSet;
  92 
  93     /**
  94      * Create a multicast socket.
  95      *
  96      * <p>If there is a security manager,
  97      * its {@code checkListen} method is first called
  98      * with 0 as its argument to ensure the operation is allowed.
  99      * This could result in a SecurityException.
 100      * <p>
 101      * When the socket is created the
 102      * {@link DatagramSocket#setReuseAddress(boolean)} method is
 103      * called to enable the SO_REUSEADDR socket option. When
 104      * {@link StandardSocketOptions#SO_REUSEPORT SO_REUSEPORT} is
 105      * supported then
 106      * {@link DatagramSocketImpl#setOption(SocketOption, Object)}
 107      * is called to enable the socket option.
 108      *
 109      * @exception IOException if an I/O exception occurs
 110      * while creating the MulticastSocket
 111      * @exception  SecurityException  if a security manager exists and its
 112      *             {@code checkListen} method doesn't allow the operation.
 113      * @see SecurityManager#checkListen
 114      * @see java.net.DatagramSocket#setReuseAddress(boolean)
 115      * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
 116      */
 117     public MulticastSocket() throws IOException {
 118         this(new InetSocketAddress(0));
 119     }
 120 
 121     /**
 122      * Create a multicast socket and bind it to a specific port.
 123      *
 124      * <p>If there is a security manager,
 125      * its {@code checkListen} method is first called
 126      * with the {@code port} argument
 127      * as its argument to ensure the operation is allowed.
 128      * This could result in a SecurityException.
 129      * <p>
 130      * When the socket is created the


 157      * {@link DatagramSocket#setReuseAddress(boolean)} method is
 158      * called to enable the SO_REUSEADDR socket option.
 159      *
 160      * @param bindaddr Socket address to bind to, or {@code null} for
 161      *                 an unbound socket.
 162      * @exception IOException if an I/O exception occurs
 163      * while creating the MulticastSocket
 164      * @exception  SecurityException  if a security manager exists and its
 165      *             {@code checkListen} method doesn't allow the operation.
 166      * @see SecurityManager#checkListen
 167      * @see java.net.DatagramSocket#setReuseAddress(boolean)
 168      *
 169      * @since 1.4
 170      */
 171     public MulticastSocket(SocketAddress bindaddr) throws IOException {
 172         super((SocketAddress) null);
 173 
 174         // Enable SO_REUSEADDR before binding
 175         setReuseAddress(true);
 176 
 177         // Enable SO_REUSEPORT if supported before binding
 178         if (supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
 179             this.setOption(StandardSocketOptions.SO_REUSEPORT, true);
 180         }
 181 
 182         if (bindaddr != null) {
 183             try {
 184                 bind(bindaddr);
 185             } finally {
 186                 if (!isBound())
 187                     close();

 188             }
 189         }
 190     }
 191 
 192     /**
 193      * The lock on the socket's TTL. This is for set/getTTL and
 194      * send(packet,ttl).
 195      */
 196     private Object ttlLock = new Object();
 197 
 198     /**
 199      * The lock on the socket's interface - used by setInterface
 200      * and getInterface
 201      */
 202     private Object infLock = new Object();
 203 
 204     /**
 205      * The "last" interface set by setInterface on this MulticastSocket
 206      */
 207     private InetAddress infAddress = null;




  76  * Multiple MulticastSocket's</B> may subscribe to a multicast group
  77  * and port concurrently, and they will all receive group datagrams.
  78  * <P>
  79  * Currently applets are not allowed to use multicast sockets.
  80  *
  81  * @author Pavani Diwanji
  82  * @since  1.1
  83  */
  84 public
  85 class MulticastSocket extends DatagramSocket {
  86 
  87     /**
  88      * Used on some platforms to record if an outgoing interface
  89      * has been set for this socket.
  90      */
  91     private boolean interfaceSet;
  92 
  93     /**
  94      * Create a multicast socket.
  95      *
  96      * <p>
  97      * If there is a security manager, its {@code checkListen} method is first
  98      * called with 0 as its argument to ensure the operation is allowed. This
  99      * could result in a SecurityException.
 100      * <p>
 101      * When the socket is created the
 102      * {@link DatagramSocket#setReuseAddress(boolean)} method is called to
 103      * enable the SO_REUSEADDR socket option.




 104      *
 105      * @exception IOException if an I/O exception occurs while creating the
 106      * MulticastSocket
 107      * @exception SecurityException if a security manager exists and its
 108      * {@code checkListen} method doesn't allow the operation.
 109      * @see SecurityManager#checkListen
 110      * @see java.net.DatagramSocket#setReuseAddress(boolean)
 111      * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
 112      */
 113     public MulticastSocket() throws IOException {
 114         this(new InetSocketAddress(0));
 115     }
 116 
 117     /**
 118      * Create a multicast socket and bind it to a specific port.
 119      *
 120      * <p>If there is a security manager,
 121      * its {@code checkListen} method is first called
 122      * with the {@code port} argument
 123      * as its argument to ensure the operation is allowed.
 124      * This could result in a SecurityException.
 125      * <p>
 126      * When the socket is created the


 153      * {@link DatagramSocket#setReuseAddress(boolean)} method is
 154      * called to enable the SO_REUSEADDR socket option.
 155      *
 156      * @param bindaddr Socket address to bind to, or {@code null} for
 157      *                 an unbound socket.
 158      * @exception IOException if an I/O exception occurs
 159      * while creating the MulticastSocket
 160      * @exception  SecurityException  if a security manager exists and its
 161      *             {@code checkListen} method doesn't allow the operation.
 162      * @see SecurityManager#checkListen
 163      * @see java.net.DatagramSocket#setReuseAddress(boolean)
 164      *
 165      * @since 1.4
 166      */
 167     public MulticastSocket(SocketAddress bindaddr) throws IOException {
 168         super((SocketAddress) null);
 169 
 170         // Enable SO_REUSEADDR before binding
 171         setReuseAddress(true);
 172 





 173         if (bindaddr != null) {
 174             try {
 175                 bind(bindaddr);
 176             } finally {
 177                 if (!isBound()) {
 178                     close();
 179                 }
 180             }
 181         }
 182     }
 183 
 184     /**
 185      * The lock on the socket's TTL. This is for set/getTTL and
 186      * send(packet,ttl).
 187      */
 188     private Object ttlLock = new Object();
 189 
 190     /**
 191      * The lock on the socket's interface - used by setInterface
 192      * and getInterface
 193      */
 194     private Object infLock = new Object();
 195 
 196     /**
 197      * The "last" interface set by setInterface on this MulticastSocket
 198      */
 199     private InetAddress infAddress = null;


< prev index next >