1 /* MulticastSocket.java -- Class for using multicast sockets
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
41 import java
.io
.IOException
;
42 import java
.util
.Enumeration
;
45 * Written using on-line Java Platform 1.2 API Specification, as well
46 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
47 * Status: Believed complete and correct.
51 * This class models a multicast UDP socket. A multicast address is a
52 * class D internet address (one whose most significant bits are 1110).
53 * A multicast group consists of a multicast address and a well known
54 * port number. All members of the group listening on that address and
55 * port will receive all the broadcasts to the group.
57 * Please note that applets are not allowed to use multicast sockets
59 * Written using on-line Java Platform 1.2 API Specification, as well
60 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
61 * Status: Believed complete and correct.
63 * @author Warren Levy <warrenl@cygnus.com>
64 * @author Aaron M. Renn (arenn@urbanophile.com) (Documentation comments)
68 public class MulticastSocket
extends DatagramSocket
71 * Create a MulticastSocket that this not bound to any address
73 * @exception IOException If an error occurs
74 * @exception SecurityException If a security manager exists and its
75 * checkListen method doesn't allow the operation
77 public MulticastSocket() throws IOException
79 this(new InetSocketAddress(0));
83 * Create a multicast socket bound to the specified port
85 * @param port The port to bind to
87 * @exception IOException If an error occurs
88 * @exception SecurityException If a security manager exists and its
89 * checkListen method doesn't allow the operation
91 public MulticastSocket(int port
) throws IOException
93 this(new InetSocketAddress(port
));
97 * Create a multicast socket bound to the specified SocketAddress.
99 * @param address The SocketAddress the multicast socket will be bound to
101 * @exception IOException If an error occurs
102 * @exception SecurityException If a security manager exists and its
103 * checkListen method doesn't allow the operation
107 public MulticastSocket(SocketAddress address
) throws IOException
109 super((SocketAddress
) null);
110 setReuseAddress(true);
116 * Returns the interface being used for multicast packets
118 * @return The multicast interface
120 * @exception SocketException If an error occurs
122 public InetAddress
getInterface() throws SocketException
125 throw new SocketException("socket is closed");
127 return (InetAddress
) getImpl().getOption(SocketOptions
.IP_MULTICAST_IF
);
131 * Returns the current value of the "Time to Live" option. This is the
132 * number of hops a packet can make before it "expires". This method id
133 * deprecated. Use <code>getTimeToLive</code> instead.
135 * @return The TTL value
137 * @exception IOException If an error occurs
139 * @deprecated 1.2 Replaced by getTimeToLive()
141 * @see MulticastSocket#getTimeToLive()
143 public byte getTTL() throws IOException
146 throw new SocketException("socket is closed");
148 // Use getTTL here rather than getTimeToLive in case we're using an impl
149 // other than the default PlainDatagramSocketImpl and it doesn't have
150 // getTimeToLive yet.
151 return getImpl().getTTL();
155 * Returns the current value of the "Time to Live" option. This is the
156 * number of hops a packet can make before it "expires".
158 * @return The TTL value
160 * @exception IOException If an error occurs
164 public int getTimeToLive() throws IOException
167 throw new SocketException("socket is closed");
169 return getImpl().getTimeToLive();
173 * Sets the interface to use for sending multicast packets.
175 * @param addr The new interface to use.
177 * @exception SocketException If an error occurs.
181 public void setInterface(InetAddress addr
) throws SocketException
184 throw new SocketException("socket is closed");
186 getImpl().setOption(SocketOptions
.IP_MULTICAST_IF
, addr
);
190 * Sets the local network interface used to send multicast messages
192 * @param netIF The local network interface used to send multicast messages
194 * @exception SocketException If an error occurs
196 * @see MulticastSocket#getNetworkInterface()
200 public void setNetworkInterface(NetworkInterface netIf
)
201 throws SocketException
204 throw new SocketException("socket is closed");
206 Enumeration e
= netIf
.getInetAddresses ();
208 if (!e
.hasMoreElements ())
209 throw new SocketException("no network devices found");
211 InetAddress address
= (InetAddress
) e
.nextElement ();
212 getImpl().setOption (SocketOptions
.IP_MULTICAST_IF
, address
);
216 * Gets the local network interface which is used to send multicast messages
218 * @return The local network interface to send multicast messages
220 * @exception SocketException If an error occurs
222 * @see MulticastSocket#setNetworkInterface(NetworkInterface netIf)
226 public NetworkInterface
getNetworkInterface()
227 throws SocketException
230 throw new SocketException("socket is closed");
232 InetAddress address
=
233 (InetAddress
) getImpl().getOption (SocketOptions
.IP_MULTICAST_IF
);
234 NetworkInterface netIf
= NetworkInterface
.getByInetAddress (address
);
240 * Disable/Enable local loopback of multicast packets. The option is used by
241 * the platform's networking code as a hint for setting whether multicast
242 * data will be looped back to the local socket.
244 * Because this option is a hint, applications that want to verify what
245 * loopback mode is set to should call #getLoopbackMode
247 * @param disable True to disable loopback mode
249 * @exception SocketException If an error occurs
253 public void setLoopbackMode(boolean disable
) throws SocketException
256 throw new SocketException("socket is closed");
258 getImpl().setOption (SocketOptions
.IP_MULTICAST_LOOP
, new Boolean (disable
));
262 * Checks if local loopback mode is enabled or not
264 * @exception SocketException If an error occurs
268 public boolean getLoopbackMode() throws SocketException
271 throw new SocketException("socket is closed");
273 Object buf
= getImpl().getOption (SocketOptions
.IP_MULTICAST_LOOP
);
275 if (buf
instanceof Boolean
)
276 return ((Boolean
) buf
).booleanValue();
278 throw new SocketException("unexpected type");
282 * Sets the "Time to Live" value for a socket. The value must be between
285 * @param ttl The new TTL value
287 * @exception IOException If an error occurs
289 * @deprecated 1.2 Replaced by <code>setTimeToLive</code>
291 * @see MulticastSocket#setTimeToLive(int ttl)
293 public void setTTL(byte ttl
) throws IOException
296 throw new SocketException("socket is closed");
298 // Use setTTL here rather than setTimeToLive in case we're using an impl
299 // other than the default PlainDatagramSocketImpl and it doesn't have
300 // setTimeToLive yet.
301 getImpl().setTTL(ttl
);
305 * Sets the "Time to Live" value for a socket. The value must be between
308 * @param ttl The new TTL value
310 * @exception IOException If an error occurs
314 public void setTimeToLive(int ttl
) throws IOException
317 throw new SocketException("socket is closed");
319 if (ttl
<= 0 || ttl
> 255)
320 throw new IllegalArgumentException("Invalid ttl: " + ttl
);
322 getImpl().setTimeToLive(ttl
);
326 * Joins the specified mulitcast group.
328 * @param addr The address of the group to join
330 * @exception IOException If an error occurs
331 * @exception SecurityException If a security manager exists and its
332 * checkMulticast method doesn't allow the operation
334 public void joinGroup(InetAddress mcastaddr
) throws IOException
337 throw new SocketException("socket is closed");
339 if (! mcastaddr
.isMulticastAddress())
340 throw new IOException("Not a Multicast address");
342 SecurityManager s
= System
.getSecurityManager();
344 s
.checkMulticast(mcastaddr
);
346 getImpl().join(mcastaddr
);
350 * Leaves the specified multicast group
352 * @param addr The address of the group to leave
354 * @exception IOException If an error occurs
355 * @exception SecurityException If a security manager exists and its
356 * checkMulticast method doesn't allow the operation
358 public void leaveGroup(InetAddress mcastaddr
) throws IOException
361 throw new SocketException("socket is closed");
363 if (! mcastaddr
.isMulticastAddress())
364 throw new IOException("Not a Multicast address");
366 SecurityManager s
= System
.getSecurityManager();
368 s
.checkMulticast(mcastaddr
);
370 getImpl().leave(mcastaddr
);
374 * Joins the specified mulitcast group on a specified interface.
376 * @param mcastaddr The multicast address to join
377 * @param netIf The local network interface to receive the multicast
378 * messages on or null to defer the interface set by #setInterface or
379 * #setNetworkInterface
381 * @exception IOException If an error occurs
382 * @exception IllegalArgumentException If address type is not supported
383 * @exception SecurityException If a security manager exists and its
384 * checkMulticast method doesn't allow the operation
386 * @see MulticastSocket#setInterface(InetAddress addr)
387 * @see MulticastSocket#setNetworkInterface(NetworkInterface netIf)
391 public void joinGroup(SocketAddress mcastaddr
, NetworkInterface netIf
)
395 throw new SocketException("socket is closed");
397 if (! (mcastaddr
instanceof InetSocketAddress
))
398 throw new IllegalArgumentException ("SocketAddress type not supported");
400 InetSocketAddress tmp
= (InetSocketAddress
) mcastaddr
;
402 if (! tmp
.getAddress ().isMulticastAddress ())
403 throw new IOException ("Not a Multicast address");
405 SecurityManager s
= System
.getSecurityManager ();
407 s
.checkMulticast (tmp
.getAddress ());
409 getImpl().joinGroup (mcastaddr
, netIf
);
413 * Leaves the specified mulitcast group on a specified interface.
415 * @param mcastaddr The multicast address to leave
416 * @param netIf The local networki interface or null to defer to the
417 * interface set by setInterface or setNetworkInterface
419 * @exception IOException If an error occurs
420 * @exception IllegalArgumentException If address type is not supported
421 * @exception SecurityException If a security manager exists and its
422 * checkMulticast method doesn't allow the operation
424 * @see MulticastSocket#setInterface(InetAddress addr)
425 * @see MulticastSocket#setNetworkInterface(NetworkInterface netIf)
429 public void leaveGroup(SocketAddress mcastaddr
, NetworkInterface netIf
)
433 throw new SocketException("socket is closed");
435 InetSocketAddress tmp
= (InetSocketAddress
) mcastaddr
;
437 if (! tmp
.getAddress ().isMulticastAddress ())
438 throw new IOException ("Not a Multicast address");
440 SecurityManager s
= System
.getSecurityManager ();
442 s
.checkMulticast (tmp
.getAddress ());
444 getImpl().leaveGroup (mcastaddr
, netIf
);
448 * Sends a packet of data to a multicast address with a TTL that is
449 * different from the default TTL on this socket. The default TTL for
450 * the socket is not changed.
452 * @param packet The packet of data to send
453 * @param ttl The TTL for this packet
455 * @exception IOException If an error occurs
456 * @exception SecurityException If a security manager exists and its
457 * checkConnect or checkMulticast method doesn't allow the operation
461 public synchronized void send(DatagramPacket p
, byte ttl
) throws IOException
464 throw new SocketException("socket is closed");
466 SecurityManager s
= System
.getSecurityManager();
469 InetAddress addr
= p
.getAddress();
470 if (addr
.isMulticastAddress())
471 s
.checkPermission (new SocketPermission
472 (addr
.getHostName () + p
.getPort (),
475 s
.checkConnect(addr
.getHostAddress(), p
.getPort());
478 int oldttl
= getImpl().getTimeToLive();
479 getImpl().setTimeToLive(((int) ttl
) & 0xFF);
481 getImpl().setTimeToLive(oldttl
);
483 } // class MulticastSocket