39 |
|
|
40 |
import java.io.IOException; |
import java.io.IOException; |
41 |
|
|
42 |
/* |
/** |
43 |
* Written using on-line Java Platform 1.2 API Specification, as well |
* Written using on-line Java Platform 1.2 API Specification, as well |
44 |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
45 |
* Status: Believed complete and correct. |
* Status: Believed complete and correct. |
46 |
*/ |
*/ |
47 |
|
|
48 |
/** |
/** |
49 |
* This class models a multicast UDP socket. A multicast address is a |
* This class models a multicast UDP socket. A multicast address is a |
50 |
* class D internet address (one whose most significant bits are 1110). |
* class D internet address (one whose most significant bits are 1110). |
51 |
* A multicast group consists of a multicast address and a well known |
* 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 |
* port number. All members of the group listening on that address and |
53 |
* port will receive all the broadcasts to the group. |
* port will receive all the broadcasts to the group. |
54 |
* <p> |
* <p> |
55 |
* Please note that applets are not allowed to use multicast sockets |
* 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 |
* Written using on-line Java Platform 1.2 API Specification, as well |
58 |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
59 |
* Status: Believed complete and correct. |
* Status: Believed complete and correct. |
60 |
* |
* |
61 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy <warrenl@cygnus.com> |
62 |
* @author Aaron M. Renn (arenn@urbanophile.com) (Documentation comments) |
* @author Aaron M. Renn (arenn@urbanophile.com) (Documentation comments) |
63 |
* @since 1.1 |
* @since 1.1 |
64 |
* @date May 18, 1999. |
* @date May 18, 1999. |
65 |
*/ |
*/ |
66 |
public class MulticastSocket extends DatagramSocket |
public class MulticastSocket extends DatagramSocket |
67 |
{ |
{ |
68 |
// FIXME: the local addr bound to the multicast socket can be reused; |
// FIXME: the local addr bound to the multicast socket can be reused; |
69 |
// unlike unicast sockets. It binds to any available network interface. |
// unlike unicast sockets. It binds to any available network interface. |
70 |
// See p.1159 JCL book. |
// See p.1159 JCL book. |
71 |
|
|
72 |
/** |
/** |
73 |
* Create a MulticastSocket that this not bound to any address |
* Create a MulticastSocket that this not bound to any address |
74 |
* |
* |
75 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
76 |
*/ |
*/ |
77 |
public MulticastSocket() throws IOException |
public MulticastSocket() throws IOException |
78 |
{ |
{ |
79 |
super(0, null); |
super(0, null); |
80 |
} |
} |
81 |
|
|
82 |
/** |
/** |
83 |
* Create a multicast socket bound to the specified port |
* Create a multicast socket bound to the specified port |
84 |
* |
* |
85 |
* @param The port to bind to |
* @param The port to bind to |
86 |
* |
* |
87 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
88 |
*/ |
*/ |
89 |
public MulticastSocket(int port) throws IOException |
public MulticastSocket(int port) throws IOException |
90 |
{ |
{ |
91 |
super(port, null); |
super(port, null); |
92 |
} |
} |
93 |
|
|
94 |
/** |
/** |
95 |
* Returns the interface being used for multicast packets |
* Returns the interface being used for multicast packets |
96 |
* |
* |
97 |
* @return The multicast interface |
* @return The multicast interface |
98 |
* |
* |
99 |
* @exception SocketException If an error occurs |
* @exception SocketException If an error occurs |
100 |
*/ |
*/ |
101 |
public InetAddress getInterface() throws SocketException |
public InetAddress getInterface() throws SocketException |
102 |
{ |
{ |
103 |
// FIXME: Is it possible that an InetAddress wasn't returned from getOption? |
// FIXME: Is it possible that an InetAddress wasn't returned from getOption? |
104 |
return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF); |
return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF); |
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
108 |
* Returns the current value of the "Time to Live" option. This is the |
* Returns the current value of the "Time to Live" option. This is the |
109 |
* number of hops a packet can make before it "expires". This method id |
* number of hops a packet can make before it "expires". This method id |
110 |
* deprecated. Use <code>getTimeToLive</code> instead. |
* deprecated. Use <code>getTimeToLive</code> instead. |
111 |
* |
* |
112 |
* @return The TTL value |
* @return The TTL value |
113 |
* |
* |
114 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
115 |
* |
* |
116 |
* @deprecated Replaced by getTimeToLive() in Java 1.2 |
* @deprecated Replaced by getTimeToLive() in Java 1.2 |
117 |
*/ |
*/ |
118 |
public byte getTTL() throws IOException |
public byte getTTL() throws IOException |
119 |
{ |
{ |
120 |
// Use getTTL here rather than getTimeToLive in case we're using an impl |
// Use getTTL here rather than getTimeToLive in case we're using an impl |
123 |
return impl.getTTL(); |
return impl.getTTL(); |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
127 |
* Returns the current value of the "Time to Live" option. This is the |
* Returns the current value of the "Time to Live" option. This is the |
128 |
* number of hops a packet can make before it "expires". |
* number of hops a packet can make before it "expires". |
129 |
* |
* |
130 |
* @return The TTL value |
* @return The TTL value |
131 |
* |
* |
132 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
133 |
* |
* |
134 |
* @since Java 1.2 |
* @since Java 1.2 |
135 |
*/ |
*/ |
136 |
public int getTimeToLive() throws IOException |
public int getTimeToLive() throws IOException |
137 |
{ |
{ |
138 |
return impl.getTimeToLive(); |
return impl.getTimeToLive(); |
139 |
} |
} |
140 |
|
|
141 |
/** |
/** |
142 |
* Sets the interface to use for multicast packets. |
* Sets the interface to use for multicast packets. |
143 |
* |
* |
144 |
* @param addr The new interface to use |
* @param addr The new interface to use |
145 |
* |
* |
146 |
* @exception SocketException If an error occurs |
* @exception SocketException If an error occurs |
147 |
*/ |
*/ |
148 |
public void setInterface(InetAddress inf) throws SocketException |
public void setInterface(InetAddress inf) throws SocketException |
149 |
{ |
{ |
150 |
impl.setOption(SocketOptions.IP_MULTICAST_IF, inf); |
impl.setOption(SocketOptions.IP_MULTICAST_IF, inf); |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
154 |
* Sets the "Time to Live" value for a socket. The value must be between |
* Sets the "Time to Live" value for a socket. The value must be between |
155 |
* 1 and 255. |
* 1 and 255. |
156 |
* |
* |
157 |
* @param ttl The new TTL value |
* @param ttl The new TTL value |
158 |
* |
* |
159 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
160 |
* |
* |
161 |
* @deprecated Replaced by <code>setTimeToLive</code> in Java 1.2 |
* @deprecated Replaced by <code>setTimeToLive</code> in Java 1.2 |
162 |
*/ |
*/ |
163 |
public void setTTL(byte ttl) throws IOException |
public void setTTL(byte ttl) throws IOException |
164 |
{ |
{ |
165 |
// Use setTTL here rather than setTimeToLive in case we're using an impl |
// Use setTTL here rather than setTimeToLive in case we're using an impl |
168 |
impl.setTTL(ttl); |
impl.setTTL(ttl); |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
172 |
* Sets the "Time to Live" value for a socket. The value must be between |
* Sets the "Time to Live" value for a socket. The value must be between |
173 |
* 1 and 255. |
* 1 and 255. |
174 |
* |
* |
175 |
* @param ttl The new TTL value |
* @param ttl The new TTL value |
176 |
* |
* |
177 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
178 |
* |
* |
179 |
* @since Java 1.2 |
* @since Java 1.2 |
180 |
*/ |
*/ |
181 |
public void setTimeToLive(int ttl) throws IOException |
public void setTimeToLive(int ttl) throws IOException |
182 |
{ |
{ |
183 |
if (ttl <= 0 || ttl > 255) |
if (ttl <= 0 || ttl > 255) |
186 |
impl.setTimeToLive(ttl); |
impl.setTimeToLive(ttl); |
187 |
} |
} |
188 |
|
|
189 |
/** |
/** |
190 |
* Joins the specified mulitcast group. |
* Joins the specified mulitcast group. |
191 |
* |
* |
192 |
* @param addr The address of the group to join |
* @param addr The address of the group to join |
193 |
* |
* |
194 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
195 |
*/ |
*/ |
196 |
public void joinGroup(InetAddress mcastaddr) throws IOException |
public void joinGroup(InetAddress mcastaddr) throws IOException |
197 |
{ |
{ |
198 |
if (! mcastaddr.isMulticastAddress()) |
if (! mcastaddr.isMulticastAddress()) |
205 |
impl.join(mcastaddr); |
impl.join(mcastaddr); |
206 |
} |
} |
207 |
|
|
208 |
/** |
/** |
209 |
* Leaves the specified multicast group |
* Leaves the specified multicast group |
210 |
* |
* |
211 |
* @param addr The address of the group to leave |
* @param addr The address of the group to leave |
212 |
* |
* |
213 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
214 |
*/ |
*/ |
215 |
public void leaveGroup(InetAddress mcastaddr) throws IOException |
public void leaveGroup(InetAddress mcastaddr) throws IOException |
216 |
{ |
{ |
217 |
if (! mcastaddr.isMulticastAddress()) |
if (! mcastaddr.isMulticastAddress()) |
224 |
impl.leave(mcastaddr); |
impl.leave(mcastaddr); |
225 |
} |
} |
226 |
|
|
227 |
/** |
/** |
228 |
* Sends a packet of data to a multicast address with a TTL that is |
* Sends a packet of data to a multicast address with a TTL that is |
229 |
* different from the default TTL on this socket. The default TTL for |
* different from the default TTL on this socket. The default TTL for |
230 |
* the socket is not changed. |
* the socket is not changed. |
231 |
* |
* |
232 |
* @param packet The packet of data to send |
* @param packet The packet of data to send |
233 |
* @param ttl The TTL for this packet |
* @param ttl The TTL for this packet |
234 |
* |
* |
235 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
236 |
*/ |
*/ |
237 |
public synchronized void send(DatagramPacket p, byte ttl) throws IOException |
public synchronized void send(DatagramPacket p, byte ttl) throws IOException |
238 |
{ |
{ |
239 |
SecurityManager s = System.getSecurityManager(); |
SecurityManager s = System.getSecurityManager(); |