home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / MulticastSocket.java < prev    next >
Text File  |  1997-05-20  |  8KB  |  239 lines

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