Produced by Araxis Merge on 9/25/2018 2:13:09 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\java\net | DatagramSocket.java | Mon Jan 22 14:46:52 2018 UTC |
2 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\java\net | DatagramSocket.java | Wed Sep 12 17:09:34 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 2626 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | /* | |
2 | * Copyrig ht (c) 199 5, 2013, O racle and/ or its aff iliates. A ll rights reserved. | |
3 | * DO NOT ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER. | |
4 | * | |
5 | * This co de is free software; you can r edistribut e it and/o r modify i t | |
6 | * under t he terms o f the GNU General Pu blic Licen se version 2 only, a s | |
7 | * publish ed by the Free Softw are Founda tion. Ora cle design ates this | |
8 | * particu lar file a s subject to the "Cl asspath" e xception a s provided | |
9 | * by Orac le in the LICENSE fi le that ac companied this code. | |
10 | * | |
11 | * This co de is dist ributed in the hope that it wi ll be usef ul, but WI THOUT | |
12 | * ANY WAR RANTY; wit hout even the implie d warranty of MERCHA NTABILITY or | |
13 | * FITNESS FOR A PAR TICULAR PU RPOSE. Se e the GNU General Pu blic Licen se | |
14 | * version 2 for mor e details (a copy is included in the LIC ENSE file that | |
15 | * accompa nied this code). | |
16 | * | |
17 | * You sho uld have r eceived a copy of th e GNU Gene ral Public License v ersion | |
18 | * 2 along with this work; if not, write to the Fr ee Softwar e Foundati on, | |
19 | * Inc., 5 1 Franklin St, Fifth Floor, Bo ston, MA 0 2110-1301 USA. | |
20 | * | |
21 | * Please contact Or acle, 500 Oracle Par kway, Redw ood Shores , CA 94065 USA | |
22 | * or visi t www.orac le.com if you need a dditional informatio n or have any | |
23 | * questio ns. | |
24 | */ | |
25 | ||
26 | package ja va.net; | |
27 | ||
28 | import jav a.io.IOExc eption; | |
29 | import jav a.nio.chan nels.Datag ramChannel ; | |
30 | import jav a.security .AccessCon troller; | |
31 | import jav a.security .Privilege dException Action; | |
32 | ||
33 | /** | |
34 | * This cl ass repres ents a soc ket for se nding and receiving datagram p ackets. | |
35 | * | |
36 | * <p>A da tagram soc ket is the sending o r receivin g point fo r a packet | |
37 | * deliver y service. Each pack et sent or received on a datag ram socket | |
38 | * is indi vidually a ddressed a nd routed. Multiple packets se nt from | |
39 | * one mac hine to an other may be routed differentl y, and may arrive in | |
40 | * any ord er. | |
41 | * | |
42 | * <p> Whe re possibl e, a newly construct ed {@code DatagramSo cket} has the | |
43 | * {@link SocketOpti ons#SO_BRO ADCAST SO_ BROADCAST} socket op tion enabl ed so as | |
44 | * to allo w the tran smission o f broadcas t datagram s. In orde r to recei ve | |
45 | * broadca st packets a Datagra mSocket sh ould be bo und to the wildcard address. | |
46 | * In some implement ations, br oadcast pa ckets may also be re ceived whe n | |
47 | * a Datag ramSocket is bound t o a more s pecific ad dress. | |
48 | * <p> | |
49 | * Example : | |
50 | * {@code | |
51 | * Data gramSocket s = new D atagramSoc ket(null); | |
52 | * s.bi nd(new Ine tSocketAdd ress(8888) ); | |
53 | * } | |
54 | * Which i s equivale nt to: | |
55 | * {@code | |
56 | * Data gramSocket s = new D atagramSoc ket(8888); | |
57 | * } | |
58 | * Both ca ses will c reate a Da tagramSock et able to receive b roadcasts on | |
59 | * UDP por t 8888. | |
60 | * | |
61 | * @author Pavani D iwanji | |
62 | * @see java.net .DatagramP acket | |
63 | * @see java.nio .channels. DatagramCh annel | |
64 | * @since JDK1.0 | |
65 | */ | |
66 | public | |
67 | class Data gramSocket implement s java.io. Closeable { | |
68 | /** | |
69 | * Var ious state s of this socket. | |
70 | */ | |
71 | privat e boolean created = false; | |
72 | privat e boolean bound = fa lse; | |
73 | privat e boolean closed = f alse; | |
74 | privat e Object c loseLock = new Objec t(); | |
75 | ||
76 | /* | |
77 | * The implement ation of t his Datagr amSocket. | |
78 | */ | |
79 | Datagr amSocketIm pl impl; | |
80 | ||
81 | /** | |
82 | * Are we using an older D atagramSoc ketImpl? | |
83 | */ | |
84 | boolea n oldImpl = false; | |
85 | ||
86 | /** | |
87 | * Set when a so cket is ST _CONNECTED until we are certai n | |
88 | * tha t any pack ets which might have been rece ived prior | |
89 | * to calling co nnect() bu t not read by the ap plication | |
90 | * hav e been rea d. During this time we check t he source | |
91 | * add ress of al l packets received t o be sure they are f rom | |
92 | * the connected destinati on. Other packets ar e read but | |
93 | * sil ently drop ped. | |
94 | */ | |
95 | privat e boolean explicitFi lter = fal se; | |
96 | privat e int byte sLeftToFil ter; | |
97 | /* | |
98 | * Con nection st ate: | |
99 | * ST_ NOT_CONNEC TED = sock et not con nected | |
100 | * ST_ CONNECTED = socket c onnected | |
101 | * ST_ CONNECTED_ NO_IMPL = socket con nected but not at im pl level | |
102 | */ | |
103 | static final int ST_NOT_CO NNECTED = 0; | |
104 | static final int ST_CONNEC TED = 1; | |
105 | static final int ST_CONNEC TED_NO_IMP L = 2; | |
106 | ||
107 | int co nnectState = ST_NOT_ CONNECTED; | |
108 | ||
109 | /* | |
110 | * Con nected add ress & por t | |
111 | */ | |
112 | InetAd dress conn ectedAddre ss = null; | |
113 | int co nnectedPor t = -1; | |
114 | ||
115 | /** | |
116 | * Con nects this socket to a remote socket add ress (IP a ddress + p ort number ). | |
117 | * Bin ds socket if not alr eady bound . | |
118 | * <p> | |
119 | * @pa ram addr ess The re mote addre ss. | |
120 | * @pa ram port The re mote port | |
121 | * @th rows Sock etExceptio n if bindi ng the soc ket fails. | |
122 | */ | |
123 | privat e synchron ized void connectInt ernal(Inet Address ad dress, int port) thr ows Socket Exception { | |
124 | if (port < 0 || port > 0xFFFF) { | |
125 | throw ne w IllegalA rgumentExc eption("co nnect: " + port); | |
126 | } | |
127 | if (address == null) { | |
128 | throw ne w IllegalA rgumentExc eption("co nnect: nul l address" ); | |
129 | } | |
130 | ch eckAddress (address, "connect" ); | |
131 | if (isClosed ()) | |
132 | return; | |
133 | Se curityMana ger securi ty = Syste m.getSecur ityManager (); | |
134 | if (security != null) { | |
135 | if (addr ess.isMult icastAddre ss()) { | |
136 | secu rity.check Multicast( address); | |
137 | } else { | |
138 | secu rity.check Connect(ad dress.getH ostAddress (), port); | |
139 | secu rity.check Accept(add ress.getHo stAddress( ), port); | |
140 | } | |
141 | } | |
142 | ||
143 | if (!isBound ()) | |
144 | bind(new I netSocketA ddress(0)) ; | |
145 | ||
146 | // old impls do not su pport conn ect/discon nect | |
147 | if (oldImpl || (impl i nstanceof AbstractPl ainDatagra mSocketImp l && | |
148 | ((Abstr actPlainDa tagramSock etImpl)imp l).nativeC onnectDisa bled())) { | |
149 | connectS tate = ST_ CONNECTED_ NO_IMPL; | |
150 | } else { | |
151 | try { | |
152 | getI mpl().conn ect(addres s, port); | |
153 | ||
154 | // s ocket is n ow connect ed by the impl | |
155 | conn ectState = ST_CONNEC TED; | |
156 | // D o we need to filter some packe ts? | |
157 | int avail = ge tImpl().da taAvailabl e(); | |
158 | if ( avail == - 1) { | |
159 | throw new SocketExce ption(); | |
160 | } | |
161 | expl icitFilter = avail > 0; | |
162 | if ( explicitFi lter) { | |
163 | bytesLeftT oFilter = getReceive BufferSize (); | |
164 | } | |
165 | } catch (SocketExc eption se) { | |
166 | ||
167 | // c onnection will be em ulated by DatagramSo cket | |
168 | conn ectState = ST_CONNEC TED_NO_IMP L; | |
169 | } | |
170 | } | |
171 | ||
172 | co nnectedAdd ress = add ress; | |
173 | co nnectedPor t = port; | |
174 | } | |
175 | ||
176 | ||
177 | /** | |
178 | * Con structs a datagram s ocket and binds it t o any avai lable port | |
179 | * on the local host machi ne. The s ocket will be bound to the | |
180 | * {@l ink InetAd dress#isAn yLocalAddr ess wildca rd} addres s, | |
181 | * an IP address chosen by the kerne l. | |
182 | * | |
183 | * <p> If there i s a securi ty manager , | |
184 | * its {@code ch eckListen} method is first cal led | |
185 | * wit h 0 as its argument to ensure the operat ion is all owed. | |
186 | * Thi s could re sult in a SecurityEx ception. | |
187 | * | |
188 | * @ex ception S ocketExcep tion if t he socket could not be opened, | |
189 | * or the so cket could not bind to the spe cified loc al port. | |
190 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
191 | * { @code chec kListen} m ethod does n't allow the operat ion. | |
192 | * | |
193 | * @se e Security Manager#ch eckListen | |
194 | */ | |
195 | public DatagramS ocket() th rows Socke tException { | |
196 | th is(new Ine tSocketAdd ress(0)); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Cre ates an un bound data gram socke t with the specified | |
201 | * Dat agramSocke tImpl. | |
202 | * | |
203 | * @pa ram impl a n instance of a <B>D atagramSoc ketImpl</B > | |
204 | * the su bclass wis hes to use on the Da tagramSock et. | |
205 | * @si nce 1.4 | |
206 | */ | |
207 | protec ted Datagr amSocket(D atagramSoc ketImpl im pl) { | |
208 | if (impl == null) | |
209 | throw ne w NullPoin terExcepti on(); | |
210 | th is.impl = impl; | |
211 | ch eckOldImpl (); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Cre ates a dat agram sock et, bound to the spe cified loc al | |
216 | * soc ket addres s. | |
217 | * <p> | |
218 | * If, if the ad dress is { @code null }, creates an unboun d socket. | |
219 | * | |
220 | * <p> If there i s a securi ty manager , | |
221 | * its {@code ch eckListen} method is first cal led | |
222 | * wit h the port from the socket add ress | |
223 | * as its argume nt to ensu re the ope ration is allowed. | |
224 | * Thi s could re sult in a SecurityEx ception. | |
225 | * | |
226 | * @pa ram bindad dr local s ocket addr ess to bin d, or {@co de null} | |
227 | * for an unbound so cket. | |
228 | * | |
229 | * @ex ception S ocketExcep tion if t he socket could not be opened, | |
230 | * or the so cket could not bind to the spe cified loc al port. | |
231 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
232 | * { @code chec kListen} m ethod does n't allow the operat ion. | |
233 | * | |
234 | * @se e Security Manager#ch eckListen | |
235 | * @si nce 1.4 | |
236 | */ | |
237 | public DatagramS ocket(Sock etAddress bindaddr) throws Soc ketExcepti on { | |
238 | // create a datagram s ocket. | |
239 | cr eateImpl() ; | |
240 | if (bindaddr != null) { | |
241 | try { | |
242 | bind (bindaddr) ; | |
243 | } finall y { | |
244 | if ( !isBound() ) | |
245 | close(); | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | /** | |
251 | * Con structs a datagram s ocket and binds it t o the spec ified port | |
252 | * on the local host machi ne. The s ocket will be bound to the | |
253 | * {@l ink InetAd dress#isAn yLocalAddr ess wildca rd} addres s, | |
254 | * an IP address chosen by the kerne l. | |
255 | * | |
256 | * <p> If there i s a securi ty manager , | |
257 | * its {@code ch eckListen} method is first cal led | |
258 | * wit h the {@co de port} a rgument | |
259 | * as its argume nt to ensu re the ope ration is allowed. | |
260 | * Thi s could re sult in a SecurityEx ception. | |
261 | * | |
262 | * @pa ram p ort port t o use. | |
263 | * @ex ception S ocketExcep tion if t he socket could not be opened, | |
264 | * or the so cket could not bind to the spe cified loc al port. | |
265 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
266 | * { @code chec kListen} m ethod does n't allow the operat ion. | |
267 | * | |
268 | * @se e Security Manager#ch eckListen | |
269 | */ | |
270 | public DatagramS ocket(int port) thro ws SocketE xception { | |
271 | th is(port, n ull); | |
272 | } | |
273 | ||
274 | /** | |
275 | * Cre ates a dat agram sock et, bound to the spe cified loc al | |
276 | * add ress. The local por t must be between 0 and 65535 inclusive. | |
277 | * If the IP add ress is 0. 0.0.0, the socket wi ll be boun d to the | |
278 | * {@l ink InetAd dress#isAn yLocalAddr ess wildca rd} addres s, | |
279 | * an IP address chosen by the kerne l. | |
280 | * | |
281 | * <p> If there i s a securi ty manager , | |
282 | * its {@code ch eckListen} method is first cal led | |
283 | * wit h the {@co de port} a rgument | |
284 | * as its argume nt to ensu re the ope ration is allowed. | |
285 | * Thi s could re sult in a SecurityEx ception. | |
286 | * | |
287 | * @pa ram port l ocal port to use | |
288 | * @pa ram laddr local addr ess to bin d | |
289 | * | |
290 | * @ex ception S ocketExcep tion if t he socket could not be opened, | |
291 | * or the so cket could not bind to the spe cified loc al port. | |
292 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
293 | * { @code chec kListen} m ethod does n't allow the operat ion. | |
294 | * | |
295 | * @se e Security Manager#ch eckListen | |
296 | * @si nce JDK1 .1 | |
297 | */ | |
298 | public DatagramS ocket(int port, Inet Address la ddr) throw s SocketEx ception { | |
299 | th is(new Ine tSocketAdd ress(laddr , port)); | |
300 | } | |
301 | ||
302 | privat e void che ckOldImpl( ) { | |
303 | if (impl == null) | |
304 | return; | |
305 | // DatagramS ocketImpl. peekdata() is a prot ected meth od, theref ore we nee d to use | |
306 | // getDeclar edMethod, therefore we need pe rmission t o access t he member | |
307 | tr y { | |
308 | AccessCo ntroller.d oPrivilege d( | |
309 | new Privileged ExceptionA ction<Void >() { | |
310 | public Voi d run() th rows NoSuc hMethodExc eption { | |
311 | Class< ?>[] cl = new Class< ?>[1]; | |
312 | cl[0] = Datagram Packet.cla ss; | |
313 | impl.g etClass(). getDeclare dMethod("p eekData", cl); | |
314 | return null; | |
315 | } | |
316 | }); | |
317 | } catch (jav a.security .Privilege dActionExc eption e) { | |
318 | oldImpl = true; | |
319 | } | |
320 | } | |
321 | ||
322 | static Class<?> implClass = null; | |
323 | ||
324 | void c reateImpl( ) throws S ocketExcep tion { | |
325 | if (impl == null) { | |
326 | if (fact ory != nul l) { | |
327 | impl = factory .createDat agramSocke tImpl(); | |
328 | chec kOldImpl() ; | |
329 | } else { | |
330 | bool ean isMult icast = (t his instan ceof Multi castSocket ) ? true : false; | |
331 | impl = Default DatagramSo cketImplFa ctory.crea teDatagram SocketImpl (isMultica st); | |
332 | ||
333 | chec kOldImpl() ; | |
334 | } | |
335 | } | |
336 | // creates a udp socke t | |
337 | im pl.create( ); | |
338 | im pl.setData gramSocket (this); | |
339 | cr eated = tr ue; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Get the {@cod e Datagram SocketImpl } attached to this s ocket, | |
344 | * cre ating it i f necessar y. | |
345 | * | |
346 | * @re turn the {@code Dat agramSocke tImpl} att ached to t hat | |
347 | * Data gramSocket | |
348 | * @th rows Socke tException if creati on fails. | |
349 | * @si nce 1.4 | |
350 | */ | |
351 | Datagr amSocketIm pl getImpl () throws SocketExce ption { | |
352 | if (!created ) | |
353 | createIm pl(); | |
354 | re turn impl; | |
355 | } | |
356 | ||
357 | /** | |
358 | * Bin ds this Da tagramSock et to a sp ecific add ress and p ort. | |
359 | * <p> | |
360 | * If the addres s is {@cod e null}, t hen the sy stem will pick up | |
361 | * an ephemeral port and a valid loc al address to bind t he socket. | |
362 | *<p> | |
363 | * @pa ram addr The addre ss and por t to bind to. | |
364 | * @th rows Sock etExceptio n if any e rror happe ns during the bind, or if the | |
365 | * sock et is alre ady bound. | |
366 | * @th rows Secu rityExcept ion if a security m anager exi sts and it s | |
367 | * { @code chec kListen} m ethod does n't allow the operat ion. | |
368 | * @th rows Illeg alArgument Exception if addr is a SocketA ddress sub class | |
369 | * not s upported b y this soc ket. | |
370 | * @si nce 1.4 | |
371 | */ | |
372 | public synchroni zed void b ind(Socket Address ad dr) throws SocketExc eption { | |
373 | if (isClosed ()) | |
374 | throw ne w SocketEx ception("S ocket is c losed"); | |
375 | if (isBound( )) | |
376 | throw ne w SocketEx ception("a lready bou nd"); | |
377 | if (addr == null) | |
378 | addr = n ew InetSoc ketAddress (0); | |
379 | if (!(addr i nstanceof InetSocket Address)) | |
380 | throw ne w IllegalA rgumentExc eption("Un supported address ty pe!"); | |
381 | In etSocketAd dress epoi nt = (Inet SocketAddr ess) addr; | |
382 | if (epoint.i sUnresolve d()) | |
383 | throw ne w SocketEx ception("U nresolved address"); | |
384 | In etAddress iaddr = ep oint.getAd dress(); | |
385 | in t port = e point.getP ort(); | |
386 | ch eckAddress (iaddr, "b ind"); | |
387 | Se curityMana ger sec = System.get SecurityMa nager(); | |
388 | if (sec != n ull) { | |
389 | sec.chec kListen(po rt); | |
390 | } | |
391 | tr y { | |
392 | getImpl( ).bind(por t, iaddr); | |
393 | } catch (Soc ketExcepti on e) { | |
394 | getImpl( ).close(); | |
395 | throw e; | |
396 | } | |
397 | bo und = true ; | |
398 | } | |
399 | ||
400 | void c heckAddres s (InetAdd ress addr, String op ) { | |
401 | if (addr == null) { | |
402 | return; | |
403 | } | |
404 | if (!(addr i nstanceof Inet4Addre ss || addr instanceo f Inet6Add ress)) { | |
405 | throw ne w IllegalA rgumentExc eption(op + ": inval id address type"); | |
406 | } | |
407 | } | |
408 | ||
409 | /** | |
410 | * Con nects the socket to a remote a ddress for this sock et. When a | |
411 | * soc ket is con nected to a remote a ddress, pa ckets may only be | |
412 | * sen t to or re ceived fro m that add ress. By d efault a d atagram | |
413 | * soc ket is not connected . | |
414 | * | |
415 | * <p> If the rem ote destin ation to w hich the s ocket is c onnected d oes not | |
416 | * exi st, or is otherwise unreachabl e, and if an ICMP de stination unreachabl e | |
417 | * pac ket has be en receive d for that address, then a sub sequent ca ll to | |
418 | * sen d or recei ve may thr ow a PortU nreachable Exception. Note, the re is no | |
419 | * gua rantee tha t the exce ption will be thrown . | |
420 | * | |
421 | * <p> If a secu rity manag er has bee n installe d then it is invoked to check | |
422 | * acc ess to the remote ad dress. Spe cifically, if the gi ven {@code address} | |
423 | * is a {@link I netAddress #isMultica stAddress multicast address}, | |
424 | * the security manager's {@link | |
425 | * jav a.lang.Sec urityManag er#checkMu lticast(In etAddress) | |
426 | * che ckMulticas t} method is invoked with the given {@co de address }. | |
427 | * Oth erwise, th e security manager's {@link | |
428 | * jav a.lang.Sec urityManag er#checkCo nnect(Stri ng,int) ch eckConnect } | |
429 | * and {@link ja va.lang.Se curityMana ger#checkA ccept chec kAccept} m ethods | |
430 | * are invoked, with the g iven {@cod e address} and {@cod e port}, t o | |
431 | * ver ify that d atagrams a re permitt ed to be s ent and re ceived | |
432 | * res pectively. | |
433 | * | |
434 | * <p> When a so cket is co nnected, { @link #rec eive recei ve} and | |
435 | * {@l ink #send send} <b>w ill not pe rform any security c hecks</b> | |
436 | * on incoming a nd outgoin g packets, other tha n matching the packe t's | |
437 | * and the socke t's addres s and port . On a sen d operatio n, if the | |
438 | * pac ket's addr ess is set and the p acket's ad dress and the socket 's | |
439 | * add ress do no t match, a n {@code I llegalArgu mentExcept ion} will be | |
440 | * thr own. A soc ket connec ted to a m ulticast a ddress may only be u sed | |
441 | * to send packe ts. | |
442 | * | |
443 | * @pa ram addres s the remo te address for the s ocket | |
444 | * | |
445 | * @pa ram port t he remote port for t he socket. | |
446 | * | |
447 | * @th rows Illeg alArgument Exception | |
448 | * if th e address is null, o r the port is out of range. | |
449 | * | |
450 | * @th rows Secur ityExcepti on | |
451 | * if a security m anager has been inst alled and it does | |
452 | * not p ermit acce ss to the given remo te address | |
453 | * | |
454 | * @se e #disconn ect | |
455 | */ | |
456 | public void conn ect(InetAd dress addr ess, int p ort) { | |
457 | tr y { | |
458 | connectI nternal(ad dress, por t); | |
459 | } catch (Soc ketExcepti on se) { | |
460 | throw ne w Error("c onnect fai led", se); | |
461 | } | |
462 | } | |
463 | ||
464 | /** | |
465 | * Con nects this socket to a remote socket add ress (IP a ddress + p ort number ). | |
466 | * | |
467 | * <p> If given an {@link InetSocket Address In etSocketAd dress}, th is method | |
468 | * beh aves as if invoking {@link #co nnect(Inet Address,in t) connect (InetAddre ss,int)} | |
469 | * wit h the the given sock et address es IP addr ess and po rt number. | |
470 | * | |
471 | * @pa ram addr The re mote addre ss. | |
472 | * | |
473 | * @th rows Sock etExceptio n | |
474 | * if t he connect fails | |
475 | * | |
476 | * @th rows Illeg alArgument Exception | |
477 | * if {@ code addr} is {@code null}, or {@code ad dr} is a S ocketAddre ss | |
478 | * subcl ass not su pported by this sock et | |
479 | * | |
480 | * @th rows Secur ityExcepti on | |
481 | * if a security m anager has been inst alled and it does | |
482 | * not p ermit acce ss to the given remo te address | |
483 | * | |
484 | * @si nce 1.4 | |
485 | */ | |
486 | public void conn ect(Socket Address ad dr) throws SocketExc eption { | |
487 | if (addr == null) | |
488 | throw ne w IllegalA rgumentExc eption("Ad dress can' t be null" ); | |
489 | if (!(addr i nstanceof InetSocket Address)) | |
490 | throw ne w IllegalA rgumentExc eption("Un supported address ty pe"); | |
491 | In etSocketAd dress epoi nt = (Inet SocketAddr ess) addr; | |
492 | if (epoint.i sUnresolve d()) | |
493 | throw ne w SocketEx ception("U nresolved address"); | |
494 | co nnectInter nal(epoint .getAddres s(), epoin t.getPort( )); | |
495 | } | |
496 | ||
497 | /** | |
498 | * Dis connects t he socket. If the so cket is cl osed or no t connecte d, | |
499 | * the n this met hod has no effect. | |
500 | * | |
501 | * @se e #connect | |
502 | */ | |
503 | public void disc onnect() { | |
504 | sy nchronized (this) { | |
505 | if (isCl osed()) | |
506 | retu rn; | |
507 | if (conn ectState = = ST_CONNE CTED) { | |
508 | impl .disconnec t (); | |
509 | } | |
510 | connecte dAddress = null; | |
511 | connecte dPort = -1 ; | |
512 | connectS tate = ST_ NOT_CONNEC TED; | |
513 | explicit Filter = f alse; | |
514 | } | |
515 | } | |
516 | ||
517 | /** | |
518 | * Ret urns the b inding sta te of the socket. | |
519 | * <p> | |
520 | * If the socket was bound prior to being {@li nk #close closed}, | |
521 | * the n this met hod will c ontinue to return {@ code true} | |
522 | * aft er the soc ket is clo sed. | |
523 | * | |
524 | * @re turn true if the soc ket succes sfully bou nd to an a ddress | |
525 | * @si nce 1.4 | |
526 | */ | |
527 | public boolean i sBound() { | |
528 | re turn bound ; | |
529 | } | |
530 | ||
531 | /** | |
532 | * Ret urns the c onnection state of t he socket. | |
533 | * <p> | |
534 | * If the socket was conne cted prior to being {@link #cl ose closed }, | |
535 | * the n this met hod will c ontinue to return {@ code true} | |
536 | * aft er the soc ket is clo sed. | |
537 | * | |
538 | * @re turn true if the soc ket succes sfully con nected to a server | |
539 | * @si nce 1.4 | |
540 | */ | |
541 | public boolean i sConnected () { | |
542 | re turn conne ctState != ST_NOT_CO NNECTED; | |
543 | } | |
544 | ||
545 | /** | |
546 | * Ret urns the a ddress to which this socket is connected . Returns | |
547 | * {@c ode null} if the soc ket is not connected . | |
548 | * <p> | |
549 | * If the socket was conne cted prior to being {@link #cl ose closed }, | |
550 | * the n this met hod will c ontinue to return th e connecte d address | |
551 | * aft er the soc ket is clo sed. | |
552 | * | |
553 | * @re turn the a ddress to which this socket is connected . | |
554 | */ | |
555 | public InetAddre ss getInet Address() { | |
556 | re turn conne ctedAddres s; | |
557 | } | |
558 | ||
559 | /** | |
560 | * Ret urns the p ort number to which this socke t is conne cted. | |
561 | * Ret urns {@cod e -1} if t he socket is not con nected. | |
562 | * <p> | |
563 | * If the socket was conne cted prior to being {@link #cl ose closed }, | |
564 | * the n this met hod will c ontinue to return th e connecte d port num ber | |
565 | * aft er the soc ket is clo sed. | |
566 | * | |
567 | * @re turn the p ort number to which this socke t is conne cted. | |
568 | */ | |
569 | public int getPo rt() { | |
570 | re turn conne ctedPort; | |
571 | } | |
572 | ||
573 | /** | |
574 | * Ret urns the a ddress of the endpoi nt this so cket is co nnected to , or | |
575 | * {@c ode null} if it is u nconnected . | |
576 | * <p> | |
577 | * If the socket was conne cted prior to being {@link #cl ose closed }, | |
578 | * the n this met hod will c ontinue to return th e connecte d address | |
579 | * aft er the soc ket is clo sed. | |
580 | * | |
581 | * @re turn a {@c ode Socket Address} r epresentin g the remo te | |
582 | * endpo int of thi s socket, or {@code null} if i t is | |
583 | * not c onnected y et. | |
584 | * @se e #getInet Address() | |
585 | * @se e #getPort () | |
586 | * @se e #connect (SocketAdd ress) | |
587 | * @si nce 1.4 | |
588 | */ | |
589 | public SocketAdd ress getRe moteSocket Address() { | |
590 | if (!isConne cted()) | |
591 | return n ull; | |
592 | re turn new I netSocketA ddress(get InetAddres s(), getPo rt()); | |
593 | } | |
594 | ||
595 | /** | |
596 | * Ret urns the a ddress of the endpoi nt this so cket is bo und to. | |
597 | * | |
598 | * @re turn a {@c ode Socket Address} r epresentin g the loca l endpoint of this | |
599 | * socke t, or {@co de null} i f it is cl osed or no t bound ye t. | |
600 | * @se e #getLoca lAddress() | |
601 | * @se e #getLoca lPort() | |
602 | * @se e #bind(So cketAddres s) | |
603 | * @si nce 1.4 | |
604 | */ | |
605 | ||
606 | public SocketAdd ress getLo calSocketA ddress() { | |
607 | if (isClosed ()) | |
608 | return n ull; | |
609 | if (!isBound ()) | |
610 | return n ull; | |
611 | re turn new I netSocketA ddress(get LocalAddre ss(), getL ocalPort() ); | |
612 | } | |
613 | ||
614 | /** | |
615 | * Sen ds a datag ram packet from this socket. T he | |
616 | * {@c ode Datagr amPacket} includes i nformation indicatin g the | |
617 | * dat a to be se nt, its le ngth, the IP address of the re mote host, | |
618 | * and the port number on the remote host. | |
619 | * | |
620 | * <p> If there i s a securi ty manager , and the socket is not curren tly | |
621 | * con nected to a remote a ddress, th is method first perf orms some | |
622 | * sec urity chec ks. First, if {@code p.getAddr ess().isMu lticastAdd ress()} | |
623 | * is true, this method ca lls the | |
624 | * sec urity mana ger's {@co de checkMu lticast} m ethod | |
625 | * wit h {@code p .getAddres s()} as it s argument . | |
626 | * If the evalua tion of th at express ion is fal se, | |
627 | * thi s method i nstead cal ls the sec urity mana ger's | |
628 | * {@c ode checkC onnect} me thod with arguments | |
629 | * {@c ode p.getA ddress().g etHostAddr ess()} and | |
630 | * {@c ode p.getP ort()}. Ea ch call to a securit y manager method | |
631 | * cou ld result in a Secur ityExcepti on if the operation is not all owed. | |
632 | * | |
633 | * @pa ram p the {@c ode Datagr amPacket} to be sent . | |
634 | * | |
635 | * @ex ception I OException if an I/ O error oc curs. | |
636 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
637 | * { @code chec kMulticast } or {@cod e checkCon nect} | |
638 | * m ethod does n't allow the send. | |
639 | * @ex ception P ortUnreach ableExcept ion may be thrown if the socke t is conne cted | |
640 | * t o a curren tly unreac hable dest ination. N ote, there is no | |
641 | * g uarantee t hat the ex ception wi ll be thro wn. | |
642 | * @ex ception j ava.nio.ch annels.Ill egalBlocki ngModeExce ption | |
643 | * i f this soc ket has an associate d channel, | |
644 | * a nd the cha nnel is in non-block ing mode. | |
645 | * @ex ception I llegalArgu mentExcept ion if the socket is connected , | |
646 | * a nd connect ed address and packe t address differ. | |
647 | * | |
648 | * @se e j ava.net.Da tagramPack et | |
649 | * @se e S ecurityMan ager#check Multicast( InetAddres s) | |
650 | * @se e S ecurityMan ager#check Connect | |
651 | * @re vised 1.4 | |
652 | * @sp ec JSR-51 | |
653 | */ | |
654 | public void send (DatagramP acket p) t hrows IOEx ception { | |
655 | In etAddress packetAddr ess = null ; | |
656 | sy nchronized (p) { | |
657 | if (isCl osed()) | |
658 | thro w new Sock etExceptio n("Socket is closed" ); | |
659 | checkAdd ress (p.ge tAddress() , "send"); | |
660 | if (conn ectState = = ST_NOT_C ONNECTED) { | |
661 | // c heck the a ddress is ok wiht th e security manager o n every se nd. | |
662 | Secu rityManage r security = System. getSecurit yManager() ; | |
663 | ||
664 | // T he reason you want t o synchron ize on dat agram pack et | |
665 | // i s because you don't want an ap plet to ch ange the a ddress | |
666 | // w hile you a re trying to send th e packet f or example | |
667 | // a fter the s ecurity ch eck but be fore the s end. | |
668 | if ( security ! = null) { | |
669 | if (p.getA ddress().i sMulticast Address()) { | |
670 | securi ty.checkMu lticast(p. getAddress ()); | |
671 | } else { | |
672 | securi ty.checkCo nnect(p.ge tAddress() .getHostAd dress(), | |
673 | p.ge tPort()); | |
674 | } | |
675 | } | |
676 | } else { | |
677 | // w e're conne cted | |
678 | pack etAddress = p.getAdd ress(); | |
679 | if ( packetAddr ess == nul l) { | |
680 | p.setAddre ss(connect edAddress) ; | |
681 | p.setPort( connectedP ort); | |
682 | } el se if ((!p acketAddre ss.equals( connectedA ddress)) | | | |
683 | p.g etPort() ! = connecte dPort) { | |
684 | throw new IllegalArg umentExcep tion("conn ected addr ess " + | |
685 | "and packet add ress" + | |
686 | " dif fer"); | |
687 | } | |
688 | } | |
689 | // Check whether t he socket is bound | |
690 | if (!isB ound()) | |
691 | bind (new InetS ocketAddre ss(0)); | |
692 | // call the metho d to send | |
693 | getImpl( ).send(p); | |
694 | } | |
695 | } | |
696 | ||
697 | /** | |
698 | * Rec eives a da tagram pac ket from t his socket . When thi s method | |
699 | * ret urns, the {@code Dat agramPacke t}'s buffe r is fille d with | |
700 | * the data rece ived. The datagram p acket also contains the sender 's | |
701 | * IP address, a nd the por t number o n the send er's machi ne. | |
702 | * <p> | |
703 | * Thi s method b locks unti l a datagr am is rece ived. The | |
704 | * {@c ode length } field of the datag ram packet object co ntains | |
705 | * the length of the recei ved messag e. If the message is longer th an | |
706 | * the packet's length, th e message is truncat ed. | |
707 | * <p> | |
708 | * If there is a security manager, a packet ca nnot be re ceived if the | |
709 | * sec urity mana ger's {@co de checkAc cept} meth od | |
710 | * doe s not allo w it. | |
711 | * | |
712 | * @pa ram p the {@c ode Datagr amPacket} into which to place | |
713 | * the inc oming data . | |
714 | * @ex ception I OException if an I/ O error oc curs. | |
715 | * @ex ception S ocketTimeo utExceptio n if setS oTimeout w as previou sly called | |
716 | * and the timeout h as expired . | |
717 | * @ex ception P ortUnreach ableExcept ion may be thrown if the socke t is conne cted | |
718 | * t o a curren tly unreac hable dest ination. N ote, there is no gua rantee tha t the | |
719 | * e xception w ill be thr own. | |
720 | * @ex ception j ava.nio.ch annels.Ill egalBlocki ngModeExce ption | |
721 | * i f this soc ket has an associate d channel, | |
722 | * a nd the cha nnel is in non-block ing mode. | |
723 | * @se e j ava.net.Da tagramPack et | |
724 | * @se e j ava.net.Da tagramSock et | |
725 | * @re vised 1.4 | |
726 | * @sp ec JSR-51 | |
727 | */ | |
728 | public synchroni zed void r eceive(Dat agramPacke t p) throw s IOExcept ion { | |
729 | sy nchronized (p) { | |
730 | if (!isB ound()) | |
731 | bind (new InetS ocketAddre ss(0)); | |
732 | if (conn ectState = = ST_NOT_C ONNECTED) { | |
733 | // c heck the a ddress is ok with th e security manager b efore ever y recv. | |
734 | Secu rityManage r security = System. getSecurit yManager() ; | |
735 | if ( security ! = null) { | |
736 | while(true ) { | |
737 | String peekAd = null; | |
738 | int peekP ORT
|
|
739 | // pee k at the p acket to s ee who it is from. | |
740 | if (!o ldImpl) { | |
741 | // We can us e the new peekData() API | |
742 | Da tagramPack et peekPac ket = new DatagramPa cket(new b yte[1], 1) ; | |
743 | pe ekPort = g etImpl().p eekData(pe ekPacket); | |
744 | pe ekAd = pee kPacket.ge tAddress() .getHostAd dress(); | |
745 | } else { | |
746 | In etAddress adr = new InetAddres s(); | |
747 | pe ekPort = g etImpl().p eek(adr); | |
748 | pe ekAd = adr .getHostAd dress(); | |
749 | } | |
750 | try { | |
751 | se curity.che ckAccept(p eekAd, pee kPort); | |
752 | // security check succ eeded - so now break | |
753 | // and recv the packet . | |
754 | br eak; | |
755 | } catc h (Securit yException se) { | |
756 | // Throw awa y the offe nding pack et by cons uming | |
757 | // it in a t mp buffer. | |
758 | Da tagramPack et tmp = n ew Datagra mPacket(ne w byte[1], 1); | |
759 | ge tImpl().re ceive(tmp) ; | |
760 | ||
761 | // silently discard th e offendin g packet | |
762 | // and conti nue: unkno wn/malicio us | |
763 | // entities on nets sh ould not m ake | |
764 | // runtime t hrow secur ity except ion and | |
765 | // disrupt t he applet by sending random | |
766 | // datagram packets. | |
767 | co ntinue; | |
768 | } | |
769 | } // end o f while | |
770 | } | |
771 | } | |
772 | Datagram Packet tmp = null; | |
773 | if ((con nectState == ST_CONN ECTED_NO_I MPL) || ex plicitFilt er) { | |
774 | // W e have to do the fil tering the old fashi oned way s ince | |
775 | // t he native impl doesn 't support connect o r the conn ect | |
776 | // v ia the imp l failed, or .. "exp licitFilte r" may be set when | |
777 | // a socket is connected via the i mpl, for a period of time | |
778 | // w hen packet s from oth er sources might be queued on socket. | |
779 | bool ean stop = false; | |
780 | whil e (!stop) { | |
781 | InetAddres s peekAddr ess = null ; | |
782 | int peekPo rt = -1; | |
783 | // peek at the packe t to see w ho it is f rom. | |
784 | if (!oldIm pl) { | |
785 | // We can use th e new peek Data() API | |
786 | Datagr amPacket p eekPacket = new Data gramPacket (new byte[ 1], 1); | |
787 | peekPo rt = getIm pl().peekD ata(peekPa cket); | |
788 | peekAd dress = pe ekPacket.g etAddress( ); | |
789 | } else { | |
790 | // thi s api only works for IPv4 | |
791 | peekAd dress = ne w InetAddr ess(); | |
792 | peekPo rt = getIm pl().peek( peekAddres s); | |
793 | } | |
794 | if ((!conn ectedAddre ss.equals( peekAddres s)) || | |
795 | (conne ctedPort ! = peekPort )) { | |
796 | // thr ow the pac ket away a nd silentl y continue | |
797 | tmp = new Datagr amPacket( | |
798 | ne w byte[102 4], 1024); | |
799 | getImp l().receiv e(tmp); | |
800 | if (ex plicitFilt er) { | |
801 | if (checkFil tering(tmp )) { | |
802 | stop = t rue; | |
803 | } | |
804 | } | |
805 | } else { | |
806 | stop = true; | |
807 | } | |
808 | } | |
809 | } | |
810 | // If th e security check suc ceeds, or the datagr am is | |
811 | // conne cted then receive th e packet | |
812 | getImpl( ).receive( p); | |
813 | if (expl icitFilter && tmp == null) { | |
814 | // p acket was not filter ed, accoun t for it h ere | |
815 | chec kFiltering (p); | |
816 | } | |
817 | } | |
818 | } | |
819 | ||
820 | privat e boolean checkFilte ring(Datag ramPacket p) throws SocketExce ption { | |
821 | by tesLeftToF ilter -= p .getLength (); | |
822 | if (bytesLef tToFilter <= 0 || ge tImpl().da taAvailabl e() <= 0) { | |
823 | explicit Filter = f alse; | |
824 | return t rue; | |
825 | } | |
826 | re turn false ; | |
827 | } | |
828 | ||
829 | /** | |
830 | * Get s the loca l address to which t he socket is bound. | |
831 | * | |
832 | * <p> If there i s a securi ty manager , its | |
833 | * {@c ode checkC onnect} me thod is fi rst called | |
834 | * wit h the host address a nd {@code -1} | |
835 | * as its argume nts to see if the op eration is allowed. | |
836 | * | |
837 | * @se e Security Manager#ch eckConnect | |
838 | * @re turn the local addr ess to whi ch the soc ket is bou nd, | |
839 | * {@co de null} i f the sock et is clos ed, or | |
840 | * an { @code Inet Address} r epresentin g | |
841 | * {@li nk InetAdd ress#isAny LocalAddre ss wildcar d} | |
842 | * addr ess if eit her the so cket is no t bound, o r | |
843 | * the security m anager {@c ode checkC onnect} | |
844 | * meth od does no t allow th e operatio n | |
845 | * @si nce 1.1 | |
846 | */ | |
847 | public InetAddre ss getLoca lAddress() { | |
848 | if (isClosed ()) | |
849 | return n ull; | |
850 | In etAddress in = null; | |
851 | tr y { | |
852 | in = (In etAddress) getImpl() .getOption (SocketOpt ions.SO_BI NDADDR); | |
853 | if (in.i sAnyLocalA ddress()) { | |
854 | in = InetAddre ss.anyLoca lAddress() ; | |
855 | } | |
856 | Security Manager s = System.g etSecurity Manager(); | |
857 | if (s != null) { | |
858 | s.ch eckConnect (in.getHos tAddress() , -1); | |
859 | } | |
860 | } catch (Exc eption e) { | |
861 | in = Ine tAddress.a nyLocalAdd ress(); // "0.0.0.0" | |
862 | } | |
863 | re turn in; | |
864 | } | |
865 | ||
866 | /** | |
867 | * Ret urns the p ort number on the lo cal host t o which th is socket | |
868 | * is bound. | |
869 | * | |
870 | * @re turn the port numbe r on the l ocal host to which t his socket is bound, | |
871 | {@co de -1} if the socket is closed , or | |
872 | {@co de 0} if i t is not b ound yet. | |
873 | */ | |
874 | public int getLo calPort() { | |
875 | if (isClosed ()) | |
876 | return - 1; | |
877 | tr y { | |
878 | return g etImpl().g etLocalPor t(); | |
879 | } catch (Exc eption e) { | |
880 | return 0 ; | |
881 | } | |
882 | } | |
883 | ||
884 | /** En able/disab le SO_TIME OUT with t he specifi ed timeout , in | |
885 | * mi lliseconds . With thi s option s et to a no n-zero tim eout, | |
886 | * a call to re ceive() fo r this Dat agramSocke t | |
887 | * wi ll block f or only th is amount of time. If the tim eout expir es, | |
888 | * a <B>java.ne t.SocketTi meoutExcep tion</B> i s raised, though the | |
889 | * Da tagramSock et is stil l valid. The option <B>must</ B> be enab led | |
890 | * pr ior to ent ering the blocking o peration t o have eff ect. The | |
891 | * ti meout must be {@code > 0}. | |
892 | * A timeout of zero is i nterpreted as an inf inite time out. | |
893 | * | |
894 | * @pa ram timeou t the spec ified time out in mil liseconds. | |
895 | * @th rows Socke tException if there is an erro r in the u nderlying protocol, such as an UDP error . | |
896 | * @si nce JDK1 .1 | |
897 | * @se e #getSoTi meout() | |
898 | */ | |
899 | public synchroni zed void s etSoTimeou t(int time out) throw s SocketEx ception { | |
900 | if (isClosed ()) | |
901 | throw ne w SocketEx ception("S ocket is c losed"); | |
902 | ge tImpl().se tOption(So cketOption s.SO_TIMEO UT, new In teger(time out)); | |
903 | } | |
904 | ||
905 | /** | |
906 | * Ret rieve sett ing for SO _TIMEOUT. 0 returns implies t hat the | |
907 | * opt ion is dis abled (i.e ., timeout of infini ty). | |
908 | * | |
909 | * @re turn the s etting for SO_TIMEOU T | |
910 | * @th rows Socke tException if there is an erro r in the u nderlying protocol, such as an UDP error . | |
911 | * @si nce JDK1 .1 | |
912 | * @se e #setSoTi meout(int) | |
913 | */ | |
914 | public synchroni zed int ge tSoTimeout () throws SocketExce ption { | |
915 | if (isClosed ()) | |
916 | throw ne w SocketEx ception("S ocket is c losed"); | |
917 | if (getImpl( ) == null) | |
918 | return 0 ; | |
919 | Ob ject o = g etImpl().g etOption(S ocketOptio ns.SO_TIME OUT); | |
920 | /* extra typ e safety * / | |
921 | if (o instan ceof Integ er) { | |
922 | return ( (Integer) o).intValu e(); | |
923 | } else { | |
924 | return 0 ; | |
925 | } | |
926 | } | |
927 | ||
928 | /** | |
929 | * Set s the SO_S NDBUF opti on to the specified value for this | |
930 | * {@c ode Datagr amSocket}. The SO_SN DBUF optio n is used by the | |
931 | * net work imple mentation as a hint to size th e underlyi ng | |
932 | * net work I/O b uffers. Th e SO_SNDBU F setting may also b e used | |
933 | * by the networ k implemen tation to determine the maximu m size | |
934 | * of the packet that can be sent on this sock et. | |
935 | * <p> | |
936 | * As SO_SNDBUF is a hint, applicati ons that w ant to ver ify | |
937 | * wha t size the buffer is should ca ll {@link #getSendBu fferSize() }. | |
938 | * <p> | |
939 | * Inc reasing th e buffer s ize may al low multip le outgoin g packets | |
940 | * to be queued by the net work imple mentation when the s end rate | |
941 | * is high. | |
942 | * <p> | |
943 | * Not e: If {@li nk #send(D atagramPac ket)} is u sed to sen d a | |
944 | * {@c ode Datagr amPacket} that is la rger than the settin g | |
945 | * of SO_SNDBUF then it is implement ation spec ific if th e | |
946 | * pac ket is sen t or disca rded. | |
947 | * | |
948 | * @pa ram size t he size to which to set the se nd buffer | |
949 | * siz e. This va lue must b e greater than 0. | |
950 | * | |
951 | * @ex ception So cketExcept ion if the re is an e rror | |
952 | * in the underl ying proto col, such as an UDP error. | |
953 | * @ex ception Il legalArgum entExcepti on if the value is 0 or is | |
954 | * neg ative. | |
955 | * @se e #getSend BufferSize () | |
956 | */ | |
957 | public synchroni zed void s etSendBuff erSize(int size) | |
958 | throws SocketExc eption{ | |
959 | if (!(size > 0)) { | |
960 | throw ne w IllegalA rgumentExc eption("ne gative sen d size"); | |
961 | } | |
962 | if (isClosed ()) | |
963 | throw ne w SocketEx ception("S ocket is c losed"); | |
964 | ge tImpl().se tOption(So cketOption s.SO_SNDBU F, new Int eger(size) ); | |
965 | } | |
966 | ||
967 | /** | |
968 | * Get value of the SO_SND BUF option for this {@code Dat agramSocke t}, that i s the | |
969 | * buf fer size u sed by the platform for output on this { @code Data gramSocket }. | |
970 | * | |
971 | * @re turn the v alue of th e SO_SNDBU F option f or this {@ code Datag ramSocket} | |
972 | * @ex ception So cketExcept ion if the re is an e rror in | |
973 | * the underlyin g protocol , such as an UDP err or. | |
974 | * @se e #setSend BufferSize | |
975 | */ | |
976 | public synchroni zed int ge tSendBuffe rSize() th rows Socke tException { | |
977 | if (isClosed ()) | |
978 | throw ne w SocketEx ception("S ocket is c losed"); | |
979 | in t result = 0; | |
980 | Ob ject o = g etImpl().g etOption(S ocketOptio ns.SO_SNDB UF); | |
981 | if (o instan ceof Integ er) { | |
982 | result = ((Integer )o).intVal ue(); | |
983 | } | |
984 | re turn resul t; | |
985 | } | |
986 | ||
987 | /** | |
988 | * Set s the SO_R CVBUF opti on to the specified value for this | |
989 | * {@c ode Datagr amSocket}. The SO_RC VBUF optio n is used by the | |
990 | * the network i mplementat ion as a h int to siz e the unde rlying | |
991 | * net work I/O b uffers. Th e SO_RCVBU F setting may also b e used | |
992 | * by the networ k implemen tation to determine the maximu m size | |
993 | * of the packet that can be receive d on this socket. | |
994 | * <p> | |
995 | * Bec ause SO_RC VBUF is a hint, appl ications t hat want t o | |
996 | * ver ify what s ize the bu ffers were set to sh ould call | |
997 | * {@l ink #getRe ceiveBuffe rSize()}. | |
998 | * <p> | |
999 | * Inc reasing SO _RCVBUF ma y allow th e network implementa tion | |
1000 | * to buffer mul tiple pack ets when p ackets arr ive faster than | |
1001 | * are being rec eived usin g {@link # receive(Da tagramPack et)}. | |
1002 | * <p> | |
1003 | * Not e: It is i mplementat ion specif ic if a pa cket large r | |
1004 | * tha n SO_RCVBU F can be r eceived. | |
1005 | * | |
1006 | * @pa ram size t he size to which to set the re ceive buff er | |
1007 | * siz e. This va lue must b e greater than 0. | |
1008 | * | |
1009 | * @ex ception So cketExcept ion if the re is an e rror in | |
1010 | * the underlyin g protocol , such as an UDP err or. | |
1011 | * @ex ception Il legalArgum entExcepti on if the value is 0 or is | |
1012 | * neg ative. | |
1013 | * @se e #getRece iveBufferS ize() | |
1014 | */ | |
1015 | public synchroni zed void s etReceiveB ufferSize( int size) | |
1016 | throws SocketExc eption{ | |
1017 | if (size <= 0) { | |
1018 | throw ne w IllegalA rgumentExc eption("in valid rece ive size") ; | |
1019 | } | |
1020 | if (isClosed ()) | |
1021 | throw ne w SocketEx ception("S ocket is c losed"); | |
1022 | ge tImpl().se tOption(So cketOption s.SO_RCVBU F, new Int eger(size) ); | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * Get value of the SO_RCV BUF option for this {@code Dat agramSocke t}, that i s the | |
1027 | * buf fer size u sed by the platform for input on this {@ code Datag ramSocket} . | |
1028 | * | |
1029 | * @re turn the v alue of th e SO_RCVBU F option f or this {@ code Datag ramSocket} | |
1030 | * @ex ception So cketExcept ion if the re is an e rror in th e underlyi ng protoco l, such as an UDP er ror. | |
1031 | * @se e #setRece iveBufferS ize(int) | |
1032 | */ | |
1033 | public synchroni zed int ge tReceiveBu fferSize() | |
1034 | throws SocketExc eption{ | |
1035 | if (isClosed ()) | |
1036 | throw ne w SocketEx ception("S ocket is c losed"); | |
1037 | in t result = 0; | |
1038 | Ob ject o = g etImpl().g etOption(S ocketOptio ns.SO_RCVB UF); | |
1039 | if (o instan ceof Integ er) { | |
1040 | result = ((Integer )o).intVal ue(); | |
1041 | } | |
1042 | re turn resul t; | |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Ena ble/disabl e the SO_R EUSEADDR s ocket opti on. | |
1047 | * <p> | |
1048 | * For UDP socke ts it may be necessa ry to bind more than one | |
1049 | * soc ket to the same sock et address . This is typically for the | |
1050 | * pur pose of re ceiving mu lticast pa ckets | |
1051 | * (Se e {@link j ava.net.Mu lticastSoc ket}). The | |
1052 | * {@c ode SO_REU SEADDR} so cket optio n allows m ultiple | |
1053 | * soc kets to be bound to the same s ocket addr ess if the | |
1054 | * {@c ode SO_REU SEADDR} so cket optio n is enabl ed prior | |
1055 | * to binding th e socket u sing {@lin k #bind(So cketAddres s)}. | |
1056 | * <p> | |
1057 | * Not e: This fu nctionalit y is not s upported b y all exis ting platf orms, | |
1058 | * so it is impl ementation specific whether th is option will be ig nored | |
1059 | * or not. Howev er, if it is not sup ported the n | |
1060 | * {@l ink #getRe useAddress ()} will a lways retu rn {@code false}. | |
1061 | * <p> | |
1062 | * Whe n a {@code DatagramS ocket} is created th e initial setting | |
1063 | * of {@code SO_ REUSEADDR} is disabl ed. | |
1064 | * <p> | |
1065 | * The behaviour when {@co de SO_REUS EADDR} is enabled or | |
1066 | * dis abled afte r a socket is bound (See {@lin k #isBound ()}) | |
1067 | * is not define d. | |
1068 | * | |
1069 | * @pa ram on wh ether to e nable or d isable the | |
1070 | * @ex ception So cketExcept ion if an error occu rs enablin g or | |
1071 | * di sabling th e {@code S O_RESUEADD R} socket option, | |
1072 | * or the socke t is close d. | |
1073 | * @si nce 1.4 | |
1074 | * @se e #getReus eAddress() | |
1075 | * @se e #bind(So cketAddres s) | |
1076 | * @se e #isBound () | |
1077 | * @se e #isClose d() | |
1078 | */ | |
1079 | public synchroni zed void s etReuseAdd ress(boole an on) thr ows Socket Exception { | |
1080 | if (isClosed ()) | |
1081 | throw ne w SocketEx ception("S ocket is c losed"); | |
1082 | // Integer i nstead of Boolean fo r compatib ility with older Dat agramSocke tImpl | |
1083 | if (oldImpl) | |
1084 | getImpl( ).setOptio n(SocketOp tions.SO_R EUSEADDR, new Intege r(on?-1:0) ); | |
1085 | el se | |
1086 | getImpl( ).setOptio n(SocketOp tions.SO_R EUSEADDR, Boolean.va lueOf(on)) ; | |
1087 | } | |
1088 | ||
1089 | /** | |
1090 | * Tes ts if SO_R EUSEADDR i s enabled. | |
1091 | * | |
1092 | * @re turn a {@c ode boolea n} indicat ing whethe r or not S O_REUSEADD R is enabl ed. | |
1093 | * @ex ception So cketExcept ion if the re is an e rror | |
1094 | * in the underl ying proto col, such as an UDP error. | |
1095 | * @si nce 1.4 | |
1096 | * @se e #setReus eAddress(b oolean) | |
1097 | */ | |
1098 | public synchroni zed boolea n getReuse Address() throws Soc ketExcepti on { | |
1099 | if (isClosed ()) | |
1100 | throw ne w SocketEx ception("S ocket is c losed"); | |
1101 | Ob ject o = g etImpl().g etOption(S ocketOptio ns.SO_REUS EADDR); | |
1102 | re turn ((Boo lean)o).bo oleanValue (); | |
1103 | } | |
1104 | ||
1105 | /** | |
1106 | * Ena ble/disabl e SO_BROAD CAST. | |
1107 | * | |
1108 | * <p> Some oper ating syst ems may re quire that the Java virtual ma chine be | |
1109 | * sta rted with implementa tion speci fic privil eges to en able this option or | |
1110 | * sen d broadcas t datagram s. | |
1111 | * | |
1112 | * @pa ram on | |
1113 | * wheth er or not to have br oadcast tu rned on. | |
1114 | * | |
1115 | * @th rows Sock etExceptio n | |
1116 | * if t here is an error in the underl ying proto col, such as an UDP | |
1117 | * erro r. | |
1118 | * | |
1119 | * @si nce 1.4 | |
1120 | * @se e #getBroa dcast() | |
1121 | */ | |
1122 | public synchroni zed void s etBroadcas t(boolean on) throws SocketExc eption { | |
1123 | if (isClosed ()) | |
1124 | throw ne w SocketEx ception("S ocket is c losed"); | |
1125 | ge tImpl().se tOption(So cketOption s.SO_BROAD CAST, Bool ean.valueO f(on)); | |
1126 | } | |
1127 | ||
1128 | /** | |
1129 | * Tes ts if SO_B ROADCAST i s enabled. | |
1130 | * @re turn a {@c ode boolea n} indicat ing whethe r or not S O_BROADCAS T is enabl ed. | |
1131 | * @ex ception So cketExcept ion if the re is an e rror | |
1132 | * in the underl ying proto col, such as an UDP error. | |
1133 | * @si nce 1.4 | |
1134 | * @se e #setBroa dcast(bool ean) | |
1135 | */ | |
1136 | public synchroni zed boolea n getBroad cast() thr ows Socket Exception { | |
1137 | if (isClosed ()) | |
1138 | throw ne w SocketEx ception("S ocket is c losed"); | |
1139 | re turn ((Boo lean)(getI mpl().getO ption(Sock etOptions. SO_BROADCA ST))).bool eanValue() ; | |
1140 | } | |
1141 | ||
1142 | /** | |
1143 | * Set s traffic class or t ype-of-ser vice octet in the IP | |
1144 | * dat agram head er for dat agrams sen t from thi s Datagram Socket. | |
1145 | * As the underl ying netwo rk impleme ntation ma y ignore t his | |
1146 | * val ue applica tions shou ld conside r it a hin t. | |
1147 | * | |
1148 | * <P> The tc <B >must</B> be in the range {@co de 0 <= tc <= | |
1149 | * 255 } or an Il legalArgum entExcepti on will be thrown. | |
1150 | * <p> Notes: | |
1151 | * <p> For Intern et Protoco l v4 the v alue consi sts of an | |
1152 | * {@c ode intege r}, the le ast signif icant 8 bi ts of whic h | |
1153 | * rep resent the value of the TOS oc tet in IP packets se nt by | |
1154 | * the socket. | |
1155 | * RFC 1349 defi nes the TO S values a s follows: | |
1156 | * | |
1157 | * <UL > | |
1158 | * <LI ><CODE>IPT OS_LOWCOST (0x02)</C ODE></LI> | |
1159 | * <LI ><CODE>IPT OS_RELIABI LITY (0x04 )</CODE></ LI> | |
1160 | * <LI ><CODE>IPT OS_THROUGH PUT (0x08) </CODE></L I> | |
1161 | * <LI ><CODE>IPT OS_LOWDELA Y (0x10)</ CODE></LI> | |
1162 | * </U L> | |
1163 | * The last low order bit is always ignored as this | |
1164 | * cor responds t o the MBZ (must be z ero) bit. | |
1165 | * <p> | |
1166 | * Set ting bits in the pre cedence fi eld may re sult in a | |
1167 | * Soc ketExcepti on indicat ing that t he operati on is not | |
1168 | * per mitted. | |
1169 | * <p> | |
1170 | * for Internet Protocol v 6 {@code t c} is the value that | |
1171 | * wou ld be plac ed into th e sin6_flo winfo fiel d of the I P header. | |
1172 | * | |
1173 | * @pa ram tc an {@c ode int} v alue for t he bitset. | |
1174 | * @th rows Socke tException if there is an erro r setting the | |
1175 | * tra ffic class or type-o f-service | |
1176 | * @si nce 1.4 | |
1177 | * @se e #getTraf ficClass | |
1178 | */ | |
1179 | public synchroni zed void s etTrafficC lass(int t c) throws SocketExce ption { | |
1180 | if (tc < 0 | | tc > 255 ) | |
1181 | throw ne w IllegalA rgumentExc eption("tc is not in range 0 - - 255"); | |
1182 | ||
1183 | if (isClosed ()) | |
1184 | throw ne w SocketEx ception("S ocket is c losed"); | |
1185 | tr y { | |
1186 | getImpl( ).setOptio n(SocketOp tions.IP_T OS, tc); | |
1187 | } catch (Soc ketExcepti on se) { | |
1188 | // not s upported i f socket a lready con nected | |
1189 | // Solar is returns error in such cases | |
1190 | if(!isCo nnected()) | |
1191 | thro w se; | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | /** | |
1196 | * Get s traffic class or t ype-of-ser vice in th e IP datag ram | |
1197 | * hea der for pa ckets sent from this DatagramS ocket. | |
1198 | * <p> | |
1199 | * As the underl ying netwo rk impleme ntation ma y ignore t he | |
1200 | * tra ffic class or type-o f-service set using {@link #se tTrafficCl ass(int)} | |
1201 | * thi s method m ay return a differen t value th an was pre viously | |
1202 | * set using the {@link #s etTrafficC lass(int)} method on this | |
1203 | * Dat agramSocke t. | |
1204 | * | |
1205 | * @re turn the t raffic cla ss or type -of-servic e already set | |
1206 | * @th rows Socke tException if there is an erro r obtainin g the | |
1207 | * tra ffic class or type-o f-service value. | |
1208 | * @si nce 1.4 | |
1209 | * @se e #setTraf ficClass(i nt) | |
1210 | */ | |
1211 | public synchroni zed int ge tTrafficCl ass() thro ws SocketE xception { | |
1212 | if (isClosed ()) | |
1213 | throw ne w SocketEx ception("S ocket is c losed"); | |
1214 | re turn ((Int eger)(getI mpl().getO ption(Sock etOptions. IP_TOS))). intValue() ; | |
1215 | } | |
1216 | ||
1217 | /** | |
1218 | * Clo ses this d atagram so cket. | |
1219 | * <p> | |
1220 | * Any thread cu rrently bl ocked in { @link #rec eive} upon this sock et | |
1221 | * wil l throw a {@link Soc ketExcepti on}. | |
1222 | * | |
1223 | * <p> If this s ocket has an associa ted channe l then the channel i s closed | |
1224 | * as well. | |
1225 | * | |
1226 | * @re vised 1.4 | |
1227 | * @sp ec JSR-51 | |
1228 | */ | |
1229 | public void clos e() { | |
1230 | sy nchronized (closeLock ) { | |
1231 | if (isCl osed()) | |
1232 | retu rn; | |
1233 | impl.clo se(); | |
1234 | closed = true; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | /** | |
1239 | * Ret urns wheth er the soc ket is clo sed or not . | |
1240 | * | |
1241 | * @re turn true if the soc ket has be en closed | |
1242 | * @si nce 1.4 | |
1243 | */ | |
1244 | public boolean i sClosed() { | |
1245 | sy nchronized (closeLock ) { | |
1246 | return c losed; | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | /** | |
1251 | * Ret urns the u nique {@li nk java.ni o.channels .DatagramC hannel} ob ject | |
1252 | * ass ociated wi th this da tagram soc ket, if an y. | |
1253 | * | |
1254 | * <p> A datagra m socket w ill have a channel i f, and onl y if, the channel | |
1255 | * its elf was cr eated via the {@link java.nio. channels.D atagramCha nnel#open | |
1256 | * Dat agramChann el.open} m ethod. | |
1257 | * | |
1258 | * @re turn the datagram c hannel ass ociated wi th this da tagram soc ket, | |
1259 | * or { @code null } if this socket was not creat ed for a c hannel | |
1260 | * | |
1261 | * @si nce 1.4 | |
1262 | * @sp ec JSR-51 | |
1263 | */ | |
1264 | public DatagramC hannel get Channel() { | |
1265 | re turn null; | |
1266 | } | |
1267 | ||
1268 | /** | |
1269 | * Use r defined factory fo r all data gram socke ts. | |
1270 | */ | |
1271 | static DatagramS ocketImplF actory fac tory; | |
1272 | ||
1273 | /** | |
1274 | * Set s the data gram socke t implemen tation fac tory for t he | |
1275 | * app lication. The factor y can be s pecified o nly once. | |
1276 | * <p> | |
1277 | * Whe n an appli cation cre ates a new datagram socket, th e socket | |
1278 | * imp lementatio n factory' s {@code c reateDatag ramSocketI mpl} metho d is | |
1279 | * cal led to cre ate the ac tual datag ram socket implement ation. | |
1280 | * <p> | |
1281 | * Pas sing {@cod e null} to the metho d is a no- op unless the factor y | |
1282 | * was already s et. | |
1283 | * | |
1284 | * <p> If there i s a securi ty manager , this met hod first calls | |
1285 | * the security manager's {@code che ckSetFacto ry} method | |
1286 | * to ensure the operation is allowe d. | |
1287 | * Thi s could re sult in a SecurityEx ception. | |
1288 | * | |
1289 | * @pa ram f ac the d esired fac tory. | |
1290 | * @ex ception I OException if an I/ O error oc curs when setting th e | |
1291 | * datagram s ocket fact ory. | |
1292 | * @ex ception S ocketExcep tion if t he factory is alread y defined. | |
1293 | * @ex ception S ecurityExc eption if a securit y manager exists and its | |
1294 | * { @code chec kSetFactor y} method doesn't al low the | |
1295 | opera tion. | |
1296 | * @se e | |
1297 | java. net.Datagr amSocketIm plFactory# createData gramSocket Impl() | |
1298 | * @se e Se curityMana ger#checkS etFactory | |
1299 | * @si nce 1.3 | |
1300 | */ | |
1301 | public static sy nchronized void | |
1302 | setDat agramSocke tImplFacto ry(Datagra mSocketImp lFactory f ac) | |
1303 | thr ows IOExce ption | |
1304 | { | |
1305 | if (factory != null) { | |
1306 | throw ne w SocketEx ception("f actory alr eady defin ed"); | |
1307 | } | |
1308 | Se curityMana ger securi ty = Syste m.getSecur ityManager (); | |
1309 | if (security != null) { | |
1310 | security .checkSetF actory(); | |
1311 | } | |
1312 | fa ctory = fa c; | |
1313 | } | |
1314 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.