src/java.base/share/classes/java/net/MulticastSocket.java
changeset 58242 94bb65cb37d3
parent 54104 954399cb6959
child 59124 d01fe40e9cd8
equal deleted inserted replaced
58241:33de7752835c 58242:94bb65cb37d3
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   101      * <p>
   101      * <p>
   102      * When the socket is created the
   102      * When the socket is created the
   103      * {@link DatagramSocket#setReuseAddress(boolean)} method is called to
   103      * {@link DatagramSocket#setReuseAddress(boolean)} method is called to
   104      * enable the SO_REUSEADDR socket option.
   104      * enable the SO_REUSEADDR socket option.
   105      *
   105      *
   106      * @exception IOException if an I/O exception occurs while creating the
   106      * @throws    IOException if an I/O exception occurs while creating the
   107      * MulticastSocket
   107      * MulticastSocket
   108      * @exception SecurityException if a security manager exists and its
   108      * @throws    SecurityException if a security manager exists and its
   109      * {@code checkListen} method doesn't allow the operation.
   109      * {@code checkListen} method doesn't allow the operation.
   110      * @see SecurityManager#checkListen
   110      * @see SecurityManager#checkListen
   111      * @see java.net.DatagramSocket#setReuseAddress(boolean)
   111      * @see java.net.DatagramSocket#setReuseAddress(boolean)
   112      * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
   112      * @see java.net.DatagramSocketImpl#setOption(SocketOption, Object)
   113      */
   113      */
   126      * <p>
   126      * <p>
   127      * When the socket is created the
   127      * When the socket is created the
   128      * {@link DatagramSocket#setReuseAddress(boolean)} method is
   128      * {@link DatagramSocket#setReuseAddress(boolean)} method is
   129      * called to enable the SO_REUSEADDR socket option.
   129      * called to enable the SO_REUSEADDR socket option.
   130      *
   130      *
   131      * @param port port to use
   131      * @param     port port to use
   132      * @exception IOException if an I/O exception occurs
   132      * @throws    IOException if an I/O exception occurs
   133      * while creating the MulticastSocket
   133      *            while creating the MulticastSocket
   134      * @exception  SecurityException  if a security manager exists and its
   134      * @throws    SecurityException  if a security manager exists and its
   135      *             {@code checkListen} method doesn't allow the operation.
   135      *            {@code checkListen} method doesn't allow the operation.
   136      * @see SecurityManager#checkListen
   136      * @see       SecurityManager#checkListen
   137      * @see java.net.DatagramSocket#setReuseAddress(boolean)
   137      * @see       java.net.DatagramSocket#setReuseAddress(boolean)
   138      */
   138      */
   139     public MulticastSocket(int port) throws IOException {
   139     public MulticastSocket(int port) throws IOException {
   140         this(new InetSocketAddress(port));
   140         this(new InetSocketAddress(port));
   141     }
   141     }
   142 
   142 
   152      * <p>
   152      * <p>
   153      * When the socket is created the
   153      * When the socket is created the
   154      * {@link DatagramSocket#setReuseAddress(boolean)} method is
   154      * {@link DatagramSocket#setReuseAddress(boolean)} method is
   155      * called to enable the SO_REUSEADDR socket option.
   155      * called to enable the SO_REUSEADDR socket option.
   156      *
   156      *
   157      * @param bindaddr Socket address to bind to, or {@code null} for
   157      * @param    bindaddr Socket address to bind to, or {@code null} for
   158      *                 an unbound socket.
   158      *           an unbound socket.
   159      * @exception IOException if an I/O exception occurs
   159      * @throws   IOException if an I/O exception occurs
   160      * while creating the MulticastSocket
   160      *           while creating the MulticastSocket
   161      * @exception  SecurityException  if a security manager exists and its
   161      * @throws   SecurityException  if a security manager exists and its
   162      *             {@code checkListen} method doesn't allow the operation.
   162      *           {@code checkListen} method doesn't allow the operation.
   163      * @see SecurityManager#checkListen
   163      * @see      SecurityManager#checkListen
   164      * @see java.net.DatagramSocket#setReuseAddress(boolean)
   164      * @see      java.net.DatagramSocket#setReuseAddress(boolean)
   165      *
   165      *
   166      * @since 1.4
   166      * @since 1.4
   167      */
   167      */
   168     public MulticastSocket(SocketAddress bindaddr) throws IOException {
   168     public MulticastSocket(SocketAddress bindaddr) throws IOException {
   169         super((SocketAddress) null);
   169         super((SocketAddress) null);
   206      * scope of the multicasts.
   206      * scope of the multicasts.
   207      *
   207      *
   208      * <p>The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be
   208      * <p>The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be
   209      * in the range {@code 0 <= ttl <= 0xFF }.
   209      * in the range {@code 0 <= ttl <= 0xFF }.
   210      *
   210      *
   211      * @param ttl the time-to-live
   211      * @param      ttl the time-to-live
   212      * @exception IOException if an I/O exception occurs
   212      * @throws     IOException if an I/O exception occurs
   213      * while setting the default time-to-live value
   213      *             while setting the default time-to-live value
   214      * @deprecated use the setTimeToLive method instead, which uses
   214      * @deprecated use the setTimeToLive method instead, which uses
   215      * <b>int</b> instead of <b>byte</b> as the type for ttl.
   215      *             <b>int</b> instead of <b>byte</b> as the type for ttl.
   216      * @see #getTTL()
   216      * @see #getTTL()
   217      */
   217      */
   218     @Deprecated
   218     @Deprecated
   219     public void setTTL(byte ttl) throws IOException {
   219     public void setTTL(byte ttl) throws IOException {
   220         if (isClosed())
   220         if (isClosed())
   252 
   252 
   253     /**
   253     /**
   254      * Get the default time-to-live for multicast packets sent out on
   254      * Get the default time-to-live for multicast packets sent out on
   255      * the socket.
   255      * the socket.
   256      *
   256      *
   257      * @exception IOException if an I/O exception occurs
   257      * @throws    IOException if an I/O exception occurs
   258      * while getting the default time-to-live value
   258      * while getting the default time-to-live value
   259      * @return the default time-to-live value
   259      * @return the default time-to-live value
   260      * @deprecated use the getTimeToLive method instead, which returns
   260      * @deprecated use the getTimeToLive method instead, which returns
   261      * an <b>int</b> instead of a <b>byte</b>.
   261      * an <b>int</b> instead of a <b>byte</b>.
   262      * @see #setTTL(byte)
   262      * @see #setTTL(byte)
   269     }
   269     }
   270 
   270 
   271     /**
   271     /**
   272      * Get the default time-to-live for multicast packets sent out on
   272      * Get the default time-to-live for multicast packets sent out on
   273      * the socket.
   273      * the socket.
   274      * @exception IOException if an I/O exception occurs while
   274      * @throws    IOException if an I/O exception occurs while
   275      * getting the default time-to-live value
   275      * getting the default time-to-live value
   276      * @return the default time-to-live value
   276      * @return the default time-to-live value
   277      * @see #setTimeToLive(int)
   277      * @see #setTimeToLive(int)
   278      */
   278      */
   279     public int getTimeToLive() throws IOException {
   279     public int getTimeToLive() throws IOException {
   291      * with the {@code mcastaddr} argument
   291      * with the {@code mcastaddr} argument
   292      * as its argument.
   292      * as its argument.
   293      *
   293      *
   294      * @param mcastaddr is the multicast address to join
   294      * @param mcastaddr is the multicast address to join
   295      *
   295      *
   296      * @exception IOException if there is an error joining, or when the address
   296      * @throws    IOException if there is an error joining, or when the address
   297      *            is not a multicast address, or the platform does not support
   297      *            is not a multicast address, or the platform does not support
   298      *            multicasting
   298      *            multicasting
   299      * @exception  SecurityException  if a security manager exists and its
   299      * @throws     SecurityException  if a security manager exists and its
   300      * {@code checkMulticast} method doesn't allow the join.
   300      * {@code checkMulticast} method doesn't allow the join.
   301      *
   301      *
   302      * @see SecurityManager#checkMulticast(InetAddress)
   302      * @see SecurityManager#checkMulticast(InetAddress)
   303      */
   303      */
   304     public void joinGroup(InetAddress mcastaddr) throws IOException {
   304     public void joinGroup(InetAddress mcastaddr) throws IOException {
   337      * calls its {@code checkMulticast} method
   337      * calls its {@code checkMulticast} method
   338      * with the {@code mcastaddr} argument
   338      * with the {@code mcastaddr} argument
   339      * as its argument.
   339      * as its argument.
   340      *
   340      *
   341      * @param mcastaddr is the multicast address to leave
   341      * @param mcastaddr is the multicast address to leave
   342      * @exception IOException if there is an error leaving
   342      * @throws    IOException if there is an error leaving
   343      * or when the address is not a multicast address.
   343      * or when the address is not a multicast address.
   344      * @exception  SecurityException  if a security manager exists and its
   344      * @throws     SecurityException  if a security manager exists and its
   345      * {@code checkMulticast} method doesn't allow the operation.
   345      * {@code checkMulticast} method doesn't allow the operation.
   346      *
   346      *
   347      * @see SecurityManager#checkMulticast(InetAddress)
   347      * @see SecurityManager#checkMulticast(InetAddress)
   348      */
   348      */
   349     public void leaveGroup(InetAddress mcastaddr) throws IOException {
   349     public void leaveGroup(InetAddress mcastaddr) throws IOException {
   376      * @param netIf specifies the local interface to receive multicast
   376      * @param netIf specifies the local interface to receive multicast
   377      *        datagram packets, or <i>null</i> to defer to the interface set by
   377      *        datagram packets, or <i>null</i> to defer to the interface set by
   378      *       {@link MulticastSocket#setInterface(InetAddress)} or
   378      *       {@link MulticastSocket#setInterface(InetAddress)} or
   379      *       {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
   379      *       {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
   380      *
   380      *
   381      * @exception IOException if there is an error joining, or when the address
   381      * @throws    IOException if there is an error joining, or when the address
   382      *            is not a multicast address, or the platform does not support
   382      *            is not a multicast address, or the platform does not support
   383      *            multicasting
   383      *            multicasting
   384      * @exception  SecurityException  if a security manager exists and its
   384      * @throws     SecurityException  if a security manager exists and its
   385      * {@code checkMulticast} method doesn't allow the join.
   385      * {@code checkMulticast} method doesn't allow the join.
   386      * @throws  IllegalArgumentException if mcastaddr is null or is a
   386      * @throws  IllegalArgumentException if mcastaddr is null or is a
   387      *          SocketAddress subclass not supported by this socket
   387      *          SocketAddress subclass not supported by this socket
   388      *
   388      *
   389      * @see SecurityManager#checkMulticast(InetAddress)
   389      * @see SecurityManager#checkMulticast(InetAddress)
   424      * @param mcastaddr is the multicast address to leave
   424      * @param mcastaddr is the multicast address to leave
   425      * @param netIf specifies the local interface or <i>null</i> to defer
   425      * @param netIf specifies the local interface or <i>null</i> to defer
   426      *             to the interface set by
   426      *             to the interface set by
   427      *             {@link MulticastSocket#setInterface(InetAddress)} or
   427      *             {@link MulticastSocket#setInterface(InetAddress)} or
   428      *             {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
   428      *             {@link MulticastSocket#setNetworkInterface(NetworkInterface)}
   429      * @exception IOException if there is an error leaving
   429      * @throws    IOException if there is an error leaving
   430      * or when the address is not a multicast address.
   430      * or when the address is not a multicast address.
   431      * @exception  SecurityException  if a security manager exists and its
   431      * @throws     SecurityException  if a security manager exists and its
   432      * {@code checkMulticast} method doesn't allow the operation.
   432      * {@code checkMulticast} method doesn't allow the operation.
   433      * @throws  IllegalArgumentException if mcastaddr is null or is a
   433      * @throws  IllegalArgumentException if mcastaddr is null or is a
   434      *          SocketAddress subclass not supported by this socket
   434      *          SocketAddress subclass not supported by this socket
   435      *
   435      *
   436      * @see SecurityManager#checkMulticast(InetAddress)
   436      * @see SecurityManager#checkMulticast(InetAddress)
   463     /**
   463     /**
   464      * Set the multicast network interface used by methods
   464      * Set the multicast network interface used by methods
   465      * whose behavior would be affected by the value of the
   465      * whose behavior would be affected by the value of the
   466      * network interface. Useful for multihomed hosts.
   466      * network interface. Useful for multihomed hosts.
   467      * @param inf the InetAddress
   467      * @param inf the InetAddress
   468      * @exception SocketException if there is an error in
   468      * @throws    SocketException if there is an error in
   469      * the underlying protocol, such as a TCP error.
   469      * the underlying protocol, such as a TCP error.
   470      * @see #getInterface()
   470      * @see #getInterface()
   471      */
   471      */
   472     public void setInterface(InetAddress inf) throws SocketException {
   472     public void setInterface(InetAddress inf) throws SocketException {
   473         if (isClosed()) {
   473         if (isClosed()) {
   487      *
   487      *
   488      * @return An {@code InetAddress} representing
   488      * @return An {@code InetAddress} representing
   489      *  the address of the network interface used for
   489      *  the address of the network interface used for
   490      *  multicast packets.
   490      *  multicast packets.
   491      *
   491      *
   492      * @exception SocketException if there is an error in
   492      * @throws    SocketException if there is an error in
   493      * the underlying protocol, such as a TCP error.
   493      * the underlying protocol, such as a TCP error.
   494      *
   494      *
   495      * @see #setInterface(java.net.InetAddress)
   495      * @see #setInterface(java.net.InetAddress)
   496      */
   496      */
   497     public InetAddress getInterface() throws SocketException {
   497     public InetAddress getInterface() throws SocketException {
   547     /**
   547     /**
   548      * Specify the network interface for outgoing multicast datagrams
   548      * Specify the network interface for outgoing multicast datagrams
   549      * sent on this socket.
   549      * sent on this socket.
   550      *
   550      *
   551      * @param netIf the interface
   551      * @param netIf the interface
   552      * @exception SocketException if there is an error in
   552      * @throws    SocketException if there is an error in
   553      * the underlying protocol, such as a TCP error.
   553      * the underlying protocol, such as a TCP error.
   554      * @see #getNetworkInterface()
   554      * @see #getNetworkInterface()
   555      * @since 1.4
   555      * @since 1.4
   556      */
   556      */
   557     public void setNetworkInterface(NetworkInterface netIf)
   557     public void setNetworkInterface(NetworkInterface netIf)
   565     }
   565     }
   566 
   566 
   567     /**
   567     /**
   568      * Get the multicast network interface set.
   568      * Get the multicast network interface set.
   569      *
   569      *
   570      * @exception SocketException if there is an error in
   570      * @throws    SocketException if there is an error in
   571      * the underlying protocol, such as a TCP error.
   571      * the underlying protocol, such as a TCP error.
   572      * @return the multicast {@code NetworkInterface} currently set
   572      * @return the multicast {@code NetworkInterface} currently set
   573      * @see #setNetworkInterface(NetworkInterface)
   573      * @see #setNetworkInterface(NetworkInterface)
   574      * @since 1.4
   574      * @since 1.4
   575      */
   575      */
   641      * One does not need to be the member of the group to send
   641      * One does not need to be the member of the group to send
   642      * packets to a destination multicast address.
   642      * packets to a destination multicast address.
   643      * @param ttl optional time to live for multicast packet.
   643      * @param ttl optional time to live for multicast packet.
   644      * default ttl is 1.
   644      * default ttl is 1.
   645      *
   645      *
   646      * @exception IOException is raised if an error occurs i.e
   646      * @throws    IOException is raised if an error occurs i.e
   647      * error while setting ttl.
   647      * error while setting ttl.
   648      * @exception  SecurityException  if a security manager exists and its
   648      * @throws     SecurityException  if a security manager exists and its
   649      *             {@code checkMulticast} or {@code checkConnect}
   649      *             {@code checkMulticast} or {@code checkConnect}
   650      *             method doesn't allow the send.
   650      *             method doesn't allow the send.
   651      *
   651      *
   652      * @deprecated Use the following code or its equivalent instead:
   652      * @deprecated Use the following code or its equivalent instead:
   653      *  ......
   653      *  ......