home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / MulticastSocket.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.4 KB  |  222 lines

  1. /*
  2.  * @(#)MulticastSocket.java    1.20 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.InterruptedIOException;
  19.  
  20. /**
  21.  * The multicast datagram socket class is useful for sending
  22.  * and receiving IP multicast packets.  A MulticastSocket is
  23.  * a (UDP) DatagramSocket, with additional capabilities for
  24.  * joining "groups" of other multicast hosts on the internet.
  25.  * <P>
  26.  * A multicast group is specified by a class D IP address, those
  27.  * in the range <CODE>224.0.0.1</CODE> to <CODE>239.255.255.255</CODE>, 
  28.  * inclusive, and by a standard UDP port number.  One would join a 
  29.  * multicast group by first creating a MulticastSocket with the desired
  30.  * port, then invoking the <CODE>joinGroup(InetAddress groupAddr)</CODE>
  31.  * method:
  32.  * <PRE>
  33.  * // join a Multicast group and send the group salutations
  34.  * ...
  35.  * byte[] msg = {'H', 'e', 'l', 'l', 'o'};
  36.  * InetAddress group = InetAddress.getByName("228.5.6.7");
  37.  * MulticastSocket s = new MulticastSocket(6789);
  38.  * s.joinGroup(group);
  39.  * DatagramPacket hi = new DatagramPacket(msg, msg.length,
  40.  *                             group, 6789);
  41.  * s.send(hi);
  42.  * // get their responses! 
  43.  * byte[] buf = new byte[1000];
  44.  * DatagramPacket recv = new DatagramPacket(buf, buf.length);
  45.  * s.receive(recv);
  46.  * ...
  47.  * // OK, I'm done talking - leave the group...
  48.  * s.leaveGroup(group);
  49.  * </PRE>
  50.  * 
  51.  * When one sends a message to a multicast group, <B>all</B> subscribing
  52.  * recipients to that host and port receive the message (within the
  53.  * time-to-live range of the packet, see below).  The socket needn't
  54.  * be a member of the multicast group to send messages to it.
  55.  * <P>
  56.  * When a socket subscribes to a multicast group/port, it receives
  57.  * datagrams sent by other hosts to the group/port, as do all other
  58.  * members of the group and port.  A socket relinquishes membership
  59.  * in a group by the leaveGroup(InetAddress addr) method.  <B> 
  60.  * Multiple MulticastSocket's</B> may subscribe to a multicast group
  61.  * and port concurrently, and they will all receive group datagrams.
  62.  * <P>
  63.  * Currently applets are not allowed ot use multicast sockets.
  64.  *
  65.  * @author Pavani Diwanji
  66.  * @since  JDK1.1
  67.  */
  68. public
  69. class MulticastSocket extends DatagramSocket {
  70.     /**
  71.      * Create a multicast socket.
  72.      */
  73.     public MulticastSocket() throws IOException {
  74.     super();
  75.     }
  76.  
  77.     /**
  78.      * Create a multicast socket and bind it to a specific port.
  79.      * @param local port to use
  80.      */
  81.     public MulticastSocket(int port) throws IOException {
  82.     super(port);
  83.     }
  84.  
  85.     /* do the work of creating a vanilla multicast socket.  It is
  86.      * important that the signature of this method not change,
  87.      * even though it is package-private, since it is overrides a
  88.      * method from DatagramSocket, which must not set SO_REUSEADDR.
  89.      */
  90.     void create(int port, InetAddress ignore) throws SocketException {
  91.     SecurityManager security = System.getSecurityManager();
  92.     if (security != null) {
  93.         security.checkListen(port);
  94.     }
  95.     try {
  96.         this.impl = (DatagramSocketImpl) implClass.newInstance();
  97.     } catch (Exception e) {
  98.         throw new SocketException("can't instantiate DatagramSocketImpl" + e.toString());
  99.     }
  100.     impl.create();
  101.     impl.setOption(SocketOptions.SO_REUSEADDR, new Integer(-1));
  102.     impl.bind(port, InetAddress.anyLocalAddress);
  103.     }
  104.     
  105.     /**
  106.      * Set the default time-to-live for multicast packets sent out
  107.      * on this socket.  The TTL sets the IP time-to-live for
  108.      * <code>DatagramPackets</code> sent to a MulticastGroup, which
  109.      * specifies how many "hops" that the packet will be forwarded
  110.      * on the network before it expires.
  111.      * <P>
  112.      * The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be
  113.      * in the range <code> 0 < ttl <= 0xFF </code>.
  114.      * @param ttl the time-to-live
  115.      */
  116.     public void setTTL(byte ttl) throws IOException {
  117.     impl.setTTL(ttl);
  118.     }
  119.  
  120.     /**
  121.      * Get the default time-to-live for multicast packets sent out
  122.      * on the socket.
  123.      */
  124.     public byte getTTL() throws IOException {
  125.     return impl.getTTL();
  126.     }
  127.  
  128.     /**
  129.      * Joins a multicast group.
  130.      * @param mcastaddr is the multicast address to join 
  131.      * @exception IOException is raised if there is an error joining
  132.      * or when address is not a multicast address.
  133.      */
  134.     public void joinGroup(InetAddress mcastaddr) throws IOException {
  135.  
  136.     SecurityManager security = System.getSecurityManager();
  137.     if (security != null) {
  138.         security.checkMulticast(mcastaddr);
  139.     }
  140.     impl.join(mcastaddr);
  141.     }
  142.  
  143.     /**
  144.      * Leave a multicast group.
  145.      * @param mcastaddr is the multicast address to leave
  146.      * @exception IOException is raised if there is an error leaving
  147.      * or when address is not a multicast address.
  148.      */
  149.     public void leaveGroup(InetAddress mcastaddr) throws IOException {
  150.  
  151.     SecurityManager security = System.getSecurityManager();
  152.     if (security != null) {
  153.         security.checkMulticast(mcastaddr);
  154.     }
  155.     impl.leave(mcastaddr);
  156.     }
  157.  
  158.     /**
  159.      * Set the outgoing network interface for multicast packets on this
  160.      * socket, to other than the system default.  Useful for multihomed
  161.      * hosts.
  162.      */
  163.     public void setInterface(InetAddress inf) throws SocketException {
  164.     impl.setOption(SocketOptions.IP_MULTICAST_IF, inf);
  165.     }
  166.     
  167.     /**
  168.      * Retrieve the address of the network interface used for
  169.      * multicast packets.
  170.      */
  171.     public InetAddress getInterface() throws SocketException {
  172.     return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
  173.     }
  174.     
  175.     /**
  176.      * Sends a datagram packet to the destination, with a TTL (time-
  177.      * to-live) other than the default for the socket.  This method
  178.      * need only be used in instances where a particular TTL is desired;
  179.      * otherwise it is preferable to set a TTL once on the socket, and
  180.      * use that default TTL for all packets.  This method does <B>not
  181.      * </B> alter the default TTL for the socket.
  182.      * @param p    is the packet to be sent. The packet should contain
  183.      * the destination multicast ip address and the data to be sent.
  184.      * One does not need to be the member of the group to send
  185.      * packets to a destination multicast address.
  186.      * @param ttl optional time to live for multicast packet.
  187.      * default ttl is 1.
  188.      * @exception IOException is raised if an error occurs i.e
  189.      * error while setting ttl.
  190.      * @see DatagramSocket#send
  191.      * @see DatagramSocket#receive
  192.      */
  193.     public synchronized void send(DatagramPacket p, byte ttl)
  194.      throws IOException {
  195.  
  196.         // Security manager makes sure that the multicast address is
  197.     // is allowed one and that the ttl used is less
  198.     // than the allowed maxttl.
  199.     SecurityManager security = System.getSecurityManager();
  200.     if (security != null) {
  201.         if (p.getAddress().isMulticastAddress()) {
  202.         security.checkMulticast(p.getAddress(), ttl);
  203.         } else {
  204.         security.checkConnect(p.getAddress().getHostAddress(), p.getPort());
  205.         }
  206.     }
  207.  
  208.     byte dttl = getTTL();
  209.     
  210.     if (ttl != dttl) {
  211.     // set the ttl
  212.         impl.setTTL(ttl);
  213.     }
  214.     // call the datagram method to send
  215.     impl.send(p);
  216.     // set it back to default
  217.     if (ttl != dttl) {
  218.         impl.setTTL(dttl);
  219.     } 
  220.     }  
  221. }
  222.