source: trunk/src/gcc/libjava/java/net/MulticastSocket.java@ 2

Last change on this file since 2 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.6 KB
Line 
1/* MulticastSocket.java -- Class for using multicast sockets
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.net;
39
40import java.io.IOException;
41
42/*
43 * Written using on-line Java Platform 1.2 API Specification, as well
44 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
45 * Status: Believed complete and correct.
46 */
47
48/**
49 * This class models a multicast UDP socket. A multicast address is a
50 * class D internet address (one whose most significant bits are 1110).
51 * A multicast group consists of a multicast address and a well known
52 * port number. All members of the group listening on that address and
53 * port will receive all the broadcasts to the group.
54 * <p>
55 * Please note that applets are not allowed to use multicast sockets
56 *
57 * Written using on-line Java Platform 1.2 API Specification, as well
58 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
59 * Status: Believed complete and correct.
60 *
61 * @author Warren Levy <[email protected]>
62 * @author Aaron M. Renn ([email protected]) (Documentation comments)
63 * @date May 18, 1999.
64 */
65public class MulticastSocket extends DatagramSocket
66{
67 // FIXME: the local addr bound to the multicast socket can be reused;
68 // unlike unicast sockets. It binds to any available network interface.
69 // See p.1159 JCL book.
70
71/**
72 * Create a MulticastSocket that this not bound to any address
73 *
74 * @exception IOException If an error occurs
75 */
76 public MulticastSocket() throws IOException
77 {
78 super(0, null);
79 }
80
81/**
82 * Create a multicast socket bound to the specified port
83 *
84 * @param The port to bind to
85 *
86 * @exception IOException If an error occurs
87 */
88 public MulticastSocket(int port) throws IOException
89 {
90 super(port, null);
91 }
92
93/**
94 * Returns the interface being used for multicast packets
95 *
96 * @return The multicast interface
97 *
98 * @exception SocketException If an error occurs
99 */
100 public InetAddress getInterface() throws SocketException
101 {
102 // FIXME: Is it possible that an InetAddress wasn't returned from getOption?
103 return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
104 }
105
106/**
107 * Returns the current value of the "Time to Live" option. This is the
108 * number of hops a packet can make before it "expires". This method id
109 * deprecated. Use <code>getTimeToLive</code> instead.
110 *
111 * @return The TTL value
112 *
113 * @exception IOException If an error occurs
114 *
115 * @deprecated Replaced by getTimeToLive() in Java 1.2
116 */
117 public byte getTTL() throws IOException
118 {
119 // Use getTTL here rather than getTimeToLive in case we're using an impl
120 // other than the default PlainDatagramSocketImpl and it doesn't have
121 // getTimeToLive yet.
122 return impl.getTTL();
123 }
124
125/**
126 * Returns the current value of the "Time to Live" option. This is the
127 * number of hops a packet can make before it "expires".
128 *
129 * @return The TTL value
130 *
131 * @exception IOException If an error occurs
132 *
133 * @since Java 1.2
134 */
135 public int getTimeToLive() throws IOException
136 {
137 return impl.getTimeToLive();
138 }
139
140/**
141 * Sets the interface to use for multicast packets.
142 *
143 * @param addr The new interface to use
144 *
145 * @exception SocketException If an error occurs
146 */
147 public void setInterface(InetAddress inf) throws SocketException
148 {
149 impl.setOption(SocketOptions.IP_MULTICAST_IF, inf);
150 }
151
152/**
153 * Sets the "Time to Live" value for a socket. The value must be between
154 * 1 and 255.
155 *
156 * @param ttl The new TTL value
157 *
158 * @exception IOException If an error occurs
159 *
160 * @deprecated Replaced by <code>setTimeToLive</code> in Java 1.2
161 */
162 public void setTTL(byte ttl) throws IOException
163 {
164 // Use setTTL here rather than setTimeToLive in case we're using an impl
165 // other than the default PlainDatagramSocketImpl and it doesn't have
166 // setTimeToLive yet.
167 impl.setTTL(ttl);
168 }
169
170/**
171 * Sets the "Time to Live" value for a socket. The value must be between
172 * 1 and 255.
173 *
174 * @param ttl The new TTL value
175 *
176 * @exception IOException If an error occurs
177 *
178 * @since Java 1.2
179 */
180 public void setTimeToLive(int ttl) throws IOException
181 {
182 if (ttl <= 0 || ttl > 255)
183 throw new IllegalArgumentException("Invalid ttl: " + ttl);
184
185 impl.setTimeToLive(ttl);
186 }
187
188/**
189 * Joins the specified mulitcast group.
190 *
191 * @param addr The address of the group to join
192 *
193 * @exception IOException If an error occurs
194 */
195 public void joinGroup(InetAddress mcastaddr) throws IOException
196 {
197 if (! mcastaddr.isMulticastAddress())
198 throw new IOException("Not a Multicast address");
199
200 SecurityManager s = System.getSecurityManager();
201 if (s != null)
202 s.checkMulticast(mcastaddr);
203
204 impl.join(mcastaddr);
205 }
206
207/**
208 * Leaves the specified multicast group
209 *
210 * @param addr The address of the group to leave
211 *
212 * @exception IOException If an error occurs
213 */
214 public void leaveGroup(InetAddress mcastaddr) throws IOException
215 {
216 if (! mcastaddr.isMulticastAddress())
217 throw new IOException("Not a Multicast address");
218
219 SecurityManager s = System.getSecurityManager();
220 if (s != null)
221 s.checkMulticast(mcastaddr);
222
223 impl.leave(mcastaddr);
224 }
225
226/**
227 * Sends a packet of data to a multicast address with a TTL that is
228 * different from the default TTL on this socket. The default TTL for
229 * the socket is not changed.
230 *
231 * @param packet The packet of data to send
232 * @param ttl The TTL for this packet
233 *
234 * @exception IOException If an error occurs
235 */
236 public synchronized void send(DatagramPacket p, byte ttl) throws IOException
237 {
238 SecurityManager s = System.getSecurityManager();
239 if (s != null)
240 {
241 InetAddress addr = p.getAddress();
242 if (addr.isMulticastAddress())
243 s.checkMulticast(addr, ttl);
244 else
245 s.checkConnect(addr.getHostAddress(), p.getPort());
246 }
247
248 int oldttl = impl.getTimeToLive();
249 impl.setTimeToLive(((int) ttl) & 0xFF);
250 impl.send(p);
251 impl.setTimeToLive(oldttl);
252 }
253} // class MulticastSocket
Note: See TracBrowser for help on using the repository browser.