src/java.base/share/classes/java/net/MulticastSocket.java
changeset 59124 d01fe40e9cd8
parent 58242 94bb65cb37d3
child 59201 b24f4caa1411
equal deleted inserted replaced
59122:5d73255c2d52 59124:d01fe40e9cd8
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.util.Collections;
    29 import java.util.Collections;
    30 import java.util.Enumeration;
    30 import java.util.Enumeration;
    31 import java.util.Set;
    31 import java.util.Set;
       
    32 import java.net.PortUnreachableException;
    32 
    33 
    33 /**
    34 /**
    34  * The multicast datagram socket class is useful for sending
    35  * The multicast datagram socket class is useful for sending
    35  * and receiving IP multicast packets.  A MulticastSocket is
    36  * and receiving IP multicast packets.  A MulticastSocket is
    36  * a (UDP) DatagramSocket, with additional capabilities for
    37  * a (UDP) DatagramSocket, with additional capabilities for
   641      * One does not need to be the member of the group to send
   642      * One does not need to be the member of the group to send
   642      * packets to a destination multicast address.
   643      * packets to a destination multicast address.
   643      * @param ttl optional time to live for multicast packet.
   644      * @param ttl optional time to live for multicast packet.
   644      * default ttl is 1.
   645      * default ttl is 1.
   645      *
   646      *
   646      * @throws    IOException is raised if an error occurs i.e
   647      * @throws     IOException is raised if an error occurs i.e
   647      * error while setting ttl.
   648      *             error while setting ttl.
   648      * @throws     SecurityException  if a security manager exists and its
   649      * @throws     SecurityException  if a security manager exists and its
   649      *             {@code checkMulticast} or {@code checkConnect}
   650      *             {@code checkMulticast} or {@code checkConnect}
   650      *             method doesn't allow the send.
   651      *             method doesn't allow the send.
       
   652      * @throws     PortUnreachableException may be thrown if the socket is connected
       
   653      *             to a currently unreachable destination. Note, there is no
       
   654      *             guarantee that the exception will be thrown.
       
   655      * @throws     IllegalArgumentException if the socket is connected,
       
   656      *             and connected address and packet address differ, or
       
   657      *             if the socket is not connected and the packet address
       
   658      *             is not set.
       
   659      *
   651      *
   660      *
   652      * @deprecated Use the following code or its equivalent instead:
   661      * @deprecated Use the following code or its equivalent instead:
   653      *  ......
   662      *  ......
   654      *  int ttl = mcastSocket.getTimeToLive();
   663      *  int ttl = mcastSocket.getTimeToLive();
   655      *  mcastSocket.setTimeToLive(newttl);
   664      *  mcastSocket.setTimeToLive(newttl);
   665     @Deprecated
   674     @Deprecated
   666     public void send(DatagramPacket p, byte ttl)
   675     public void send(DatagramPacket p, byte ttl)
   667         throws IOException {
   676         throws IOException {
   668             if (isClosed())
   677             if (isClosed())
   669                 throw new SocketException("Socket is closed");
   678                 throw new SocketException("Socket is closed");
   670             checkAddress(p.getAddress(), "send");
       
   671             synchronized(ttlLock) {
   679             synchronized(ttlLock) {
   672                 synchronized(p) {
   680                 synchronized(p) {
       
   681                     InetAddress packetAddress = p.getAddress();
       
   682                     checkAddress(packetAddress, "send");
   673                     if (connectState == ST_NOT_CONNECTED) {
   683                     if (connectState == ST_NOT_CONNECTED) {
       
   684                         if (packetAddress == null) {
       
   685                             throw new IllegalArgumentException("Address not set");
       
   686                         }
   674                         // Security manager makes sure that the multicast address
   687                         // Security manager makes sure that the multicast address
   675                         // is allowed one and that the ttl used is less
   688                         // is allowed one and that the ttl used is less
   676                         // than the allowed maxttl.
   689                         // than the allowed maxttl.
   677                         SecurityManager security = System.getSecurityManager();
   690                         SecurityManager security = System.getSecurityManager();
   678                         if (security != null) {
   691                         if (security != null) {
   679                             if (p.getAddress().isMulticastAddress()) {
   692                             if (packetAddress.isMulticastAddress()) {
   680                                 security.checkMulticast(p.getAddress(), ttl);
   693                                 security.checkMulticast(packetAddress, ttl);
   681                             } else {
   694                             } else {
   682                                 security.checkConnect(p.getAddress().getHostAddress(),
   695                                 security.checkConnect(packetAddress.getHostAddress(),
   683                                                       p.getPort());
   696                                                       p.getPort());
   684                             }
   697                             }
   685                         }
   698                         }
   686                     } else {
   699                     } else {
   687                         // we're connected
   700                         // we're connected
   688                         InetAddress packetAddress = null;
       
   689                         packetAddress = p.getAddress();
       
   690                         if (packetAddress == null) {
   701                         if (packetAddress == null) {
   691                             p.setAddress(connectedAddress);
   702                             p.setAddress(connectedAddress);
   692                             p.setPort(connectedPort);
   703                             p.setPort(connectedPort);
   693                         } else if ((!packetAddress.equals(connectedAddress)) ||
   704                         } else if ((!packetAddress.equals(connectedAddress)) ||
   694                                    p.getPort() != connectedPort) {
   705                                    p.getPort() != connectedPort) {
   695                             throw new SecurityException("connected address and packet address" +
   706                             throw new IllegalArgumentException("connected address and packet address" +
   696                                                         " differ");
   707                                                         " differ");
   697                         }
   708                         }
   698                     }
   709                     }
   699                     byte dttl = getTTL();
   710                     byte dttl = getTTL();
   700                     try {
   711                     try {