author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58502 | 7cb1218ef4d0 |
child 58680 | 7e9e2f10a050 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.channels.DatagramChannel; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedExceptionAction; |
|
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
32 |
import java.util.Objects; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
33 |
import java.util.Set; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
34 |
import java.util.Collections; |
2 | 35 |
|
36 |
/** |
|
37 |
* This class represents a socket for sending and receiving datagram packets. |
|
38 |
* |
|
39 |
* <p>A datagram socket is the sending or receiving point for a packet |
|
40 |
* delivery service. Each packet sent or received on a datagram socket |
|
41 |
* is individually addressed and routed. Multiple packets sent from |
|
42 |
* one machine to another may be routed differently, and may arrive in |
|
43 |
* any order. |
|
44 |
* |
|
1817
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
45 |
* <p> Where possible, a newly constructed {@code DatagramSocket} has the |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
46 |
* {@link SocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
47 |
* to allow the transmission of broadcast datagrams. In order to receive |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
48 |
* broadcast packets a DatagramSocket should be bound to the wildcard address. |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
49 |
* In some implementations, broadcast packets may also be received when |
2 | 50 |
* a DatagramSocket is bound to a more specific address. |
51 |
* <p> |
|
52 |
* Example: |
|
19069 | 53 |
* {@code |
2 | 54 |
* DatagramSocket s = new DatagramSocket(null); |
55 |
* s.bind(new InetSocketAddress(8888)); |
|
19069 | 56 |
* } |
2 | 57 |
* Which is equivalent to: |
19069 | 58 |
* {@code |
2 | 59 |
* DatagramSocket s = new DatagramSocket(8888); |
19069 | 60 |
* } |
2 | 61 |
* Both cases will create a DatagramSocket able to receive broadcasts on |
62 |
* UDP port 8888. |
|
63 |
* |
|
64 |
* @author Pavani Diwanji |
|
65 |
* @see java.net.DatagramPacket |
|
66 |
* @see java.nio.channels.DatagramChannel |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23927
diff
changeset
|
67 |
* @since 1.0 |
2 | 68 |
*/ |
69 |
public |
|
70 |
class DatagramSocket implements java.io.Closeable { |
|
71 |
/** |
|
72 |
* Various states of this socket. |
|
73 |
*/ |
|
74 |
private boolean created = false; |
|
75 |
private boolean bound = false; |
|
76 |
private boolean closed = false; |
|
77 |
private Object closeLock = new Object(); |
|
78 |
||
79 |
/* |
|
80 |
* The implementation of this DatagramSocket. |
|
81 |
*/ |
|
82 |
DatagramSocketImpl impl; |
|
83 |
||
84 |
/** |
|
85 |
* Are we using an older DatagramSocketImpl? |
|
86 |
*/ |
|
87 |
boolean oldImpl = false; |
|
88 |
||
27078 | 89 |
/** |
90 |
* Set when a socket is ST_CONNECTED until we are certain |
|
91 |
* that any packets which might have been received prior |
|
92 |
* to calling connect() but not read by the application |
|
93 |
* have been read. During this time we check the source |
|
94 |
* address of all packets received to be sure they are from |
|
95 |
* the connected destination. Other packets are read but |
|
96 |
* silently dropped. |
|
97 |
*/ |
|
98 |
private boolean explicitFilter = false; |
|
99 |
private int bytesLeftToFilter; |
|
2 | 100 |
/* |
101 |
* Connection state: |
|
102 |
* ST_NOT_CONNECTED = socket not connected |
|
103 |
* ST_CONNECTED = socket connected |
|
104 |
* ST_CONNECTED_NO_IMPL = socket connected but not at impl level |
|
105 |
*/ |
|
106 |
static final int ST_NOT_CONNECTED = 0; |
|
107 |
static final int ST_CONNECTED = 1; |
|
108 |
static final int ST_CONNECTED_NO_IMPL = 2; |
|
109 |
||
110 |
int connectState = ST_NOT_CONNECTED; |
|
111 |
||
112 |
/* |
|
113 |
* Connected address & port |
|
114 |
*/ |
|
115 |
InetAddress connectedAddress = null; |
|
116 |
int connectedPort = -1; |
|
117 |
||
118 |
/** |
|
119 |
* Connects this socket to a remote socket address (IP address + port number). |
|
120 |
* Binds socket if not already bound. |
|
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
21428
diff
changeset
|
121 |
* |
14014 | 122 |
* @param address The remote address. |
2 | 123 |
* @param port The remote port |
124 |
* @throws SocketException if binding the socket fails. |
|
125 |
*/ |
|
126 |
private synchronized void connectInternal(InetAddress address, int port) throws SocketException { |
|
127 |
if (port < 0 || port > 0xFFFF) { |
|
128 |
throw new IllegalArgumentException("connect: " + port); |
|
129 |
} |
|
130 |
if (address == null) { |
|
131 |
throw new IllegalArgumentException("connect: null address"); |
|
132 |
} |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
133 |
checkAddress (address, "connect"); |
2 | 134 |
if (isClosed()) |
135 |
return; |
|
136 |
SecurityManager security = System.getSecurityManager(); |
|
137 |
if (security != null) { |
|
138 |
if (address.isMulticastAddress()) { |
|
139 |
security.checkMulticast(address); |
|
140 |
} else { |
|
141 |
security.checkConnect(address.getHostAddress(), port); |
|
142 |
security.checkAccept(address.getHostAddress(), port); |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
if (!isBound()) |
|
147 |
bind(new InetSocketAddress(0)); |
|
148 |
||
149 |
// old impls do not support connect/disconnect |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
150 |
if (oldImpl || (impl instanceof AbstractPlainDatagramSocketImpl && |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
151 |
((AbstractPlainDatagramSocketImpl)impl).nativeConnectDisabled())) { |
2 | 152 |
connectState = ST_CONNECTED_NO_IMPL; |
153 |
} else { |
|
154 |
try { |
|
155 |
getImpl().connect(address, port); |
|
156 |
||
157 |
// socket is now connected by the impl |
|
158 |
connectState = ST_CONNECTED; |
|
27078 | 159 |
// Do we need to filter some packets? |
160 |
int avail = getImpl().dataAvailable(); |
|
161 |
if (avail == -1) { |
|
162 |
throw new SocketException(); |
|
163 |
} |
|
164 |
explicitFilter = avail > 0; |
|
165 |
if (explicitFilter) { |
|
166 |
bytesLeftToFilter = getReceiveBufferSize(); |
|
167 |
} |
|
2 | 168 |
} catch (SocketException se) { |
169 |
||
170 |
// connection will be emulated by DatagramSocket |
|
171 |
connectState = ST_CONNECTED_NO_IMPL; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
connectedAddress = address; |
|
176 |
connectedPort = port; |
|
177 |
} |
|
178 |
||
179 |
||
180 |
/** |
|
181 |
* Constructs a datagram socket and binds it to any available port |
|
182 |
* on the local host machine. The socket will be bound to the |
|
183 |
* {@link InetAddress#isAnyLocalAddress wildcard} address, |
|
184 |
* an IP address chosen by the kernel. |
|
185 |
* |
|
186 |
* <p>If there is a security manager, |
|
19069 | 187 |
* its {@code checkListen} method is first called |
2 | 188 |
* with 0 as its argument to ensure the operation is allowed. |
189 |
* This could result in a SecurityException. |
|
190 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
191 |
* @throws SocketException if the socket could not be opened, |
2 | 192 |
* or the socket could not bind to the specified local port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
193 |
* @throws SecurityException if a security manager exists and its |
19069 | 194 |
* {@code checkListen} method doesn't allow the operation. |
2 | 195 |
* |
196 |
* @see SecurityManager#checkListen |
|
197 |
*/ |
|
198 |
public DatagramSocket() throws SocketException { |
|
10437
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
199 |
this(new InetSocketAddress(0)); |
2 | 200 |
} |
201 |
||
202 |
/** |
|
203 |
* Creates an unbound datagram socket with the specified |
|
204 |
* DatagramSocketImpl. |
|
205 |
* |
|
206 |
* @param impl an instance of a <B>DatagramSocketImpl</B> |
|
207 |
* the subclass wishes to use on the DatagramSocket. |
|
208 |
* @since 1.4 |
|
209 |
*/ |
|
210 |
protected DatagramSocket(DatagramSocketImpl impl) { |
|
211 |
if (impl == null) |
|
212 |
throw new NullPointerException(); |
|
213 |
this.impl = impl; |
|
214 |
checkOldImpl(); |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Creates a datagram socket, bound to the specified local |
|
219 |
* socket address. |
|
220 |
* <p> |
|
19069 | 221 |
* If, if the address is {@code null}, creates an unbound socket. |
21334 | 222 |
* |
2 | 223 |
* <p>If there is a security manager, |
19069 | 224 |
* its {@code checkListen} method is first called |
2 | 225 |
* with the port from the socket address |
226 |
* as its argument to ensure the operation is allowed. |
|
227 |
* This could result in a SecurityException. |
|
228 |
* |
|
19069 | 229 |
* @param bindaddr local socket address to bind, or {@code null} |
2 | 230 |
* for an unbound socket. |
231 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
232 |
* @throws SocketException if the socket could not be opened, |
2 | 233 |
* or the socket could not bind to the specified local port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
234 |
* @throws SecurityException if a security manager exists and its |
19069 | 235 |
* {@code checkListen} method doesn't allow the operation. |
2 | 236 |
* |
237 |
* @see SecurityManager#checkListen |
|
238 |
* @since 1.4 |
|
239 |
*/ |
|
240 |
public DatagramSocket(SocketAddress bindaddr) throws SocketException { |
|
241 |
// create a datagram socket. |
|
242 |
createImpl(); |
|
243 |
if (bindaddr != null) { |
|
10437
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
244 |
try { |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
245 |
bind(bindaddr); |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
246 |
} finally { |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
247 |
if (!isBound()) |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
248 |
close(); |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
10134
diff
changeset
|
249 |
} |
2 | 250 |
} |
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Constructs a datagram socket and binds it to the specified port |
|
255 |
* on the local host machine. The socket will be bound to the |
|
256 |
* {@link InetAddress#isAnyLocalAddress wildcard} address, |
|
257 |
* an IP address chosen by the kernel. |
|
258 |
* |
|
259 |
* <p>If there is a security manager, |
|
19069 | 260 |
* its {@code checkListen} method is first called |
261 |
* with the {@code port} argument |
|
2 | 262 |
* as its argument to ensure the operation is allowed. |
263 |
* This could result in a SecurityException. |
|
264 |
* |
|
265 |
* @param port port to use. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
266 |
* @throws SocketException if the socket could not be opened, |
2 | 267 |
* or the socket could not bind to the specified local port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
268 |
* @throws SecurityException if a security manager exists and its |
19069 | 269 |
* {@code checkListen} method doesn't allow the operation. |
2 | 270 |
* |
271 |
* @see SecurityManager#checkListen |
|
272 |
*/ |
|
273 |
public DatagramSocket(int port) throws SocketException { |
|
274 |
this(port, null); |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Creates a datagram socket, bound to the specified local |
|
279 |
* address. The local port must be between 0 and 65535 inclusive. |
|
280 |
* If the IP address is 0.0.0.0, the socket will be bound to the |
|
281 |
* {@link InetAddress#isAnyLocalAddress wildcard} address, |
|
282 |
* an IP address chosen by the kernel. |
|
283 |
* |
|
284 |
* <p>If there is a security manager, |
|
19069 | 285 |
* its {@code checkListen} method is first called |
286 |
* with the {@code port} argument |
|
2 | 287 |
* as its argument to ensure the operation is allowed. |
288 |
* This could result in a SecurityException. |
|
289 |
* |
|
290 |
* @param port local port to use |
|
291 |
* @param laddr local address to bind |
|
292 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
293 |
* @throws SocketException if the socket could not be opened, |
2 | 294 |
* or the socket could not bind to the specified local port. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
295 |
* @throws SecurityException if a security manager exists and its |
19069 | 296 |
* {@code checkListen} method doesn't allow the operation. |
2 | 297 |
* |
298 |
* @see SecurityManager#checkListen |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23927
diff
changeset
|
299 |
* @since 1.1 |
2 | 300 |
*/ |
301 |
public DatagramSocket(int port, InetAddress laddr) throws SocketException { |
|
302 |
this(new InetSocketAddress(laddr, port)); |
|
303 |
} |
|
304 |
||
305 |
private void checkOldImpl() { |
|
306 |
if (impl == null) |
|
307 |
return; |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47478
diff
changeset
|
308 |
// DatagramSocketImpl.peekData() is a protected method, therefore we need to use |
2 | 309 |
// getDeclaredMethod, therefore we need permission to access the member |
310 |
try { |
|
51 | 311 |
AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
28852
diff
changeset
|
312 |
new PrivilegedExceptionAction<>() { |
51 | 313 |
public Void run() throws NoSuchMethodException { |
10701
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
10596
diff
changeset
|
314 |
Class<?>[] cl = new Class<?>[1]; |
2 | 315 |
cl[0] = DatagramPacket.class; |
316 |
impl.getClass().getDeclaredMethod("peekData", cl); |
|
317 |
return null; |
|
318 |
} |
|
319 |
}); |
|
320 |
} catch (java.security.PrivilegedActionException e) { |
|
321 |
oldImpl = true; |
|
322 |
} |
|
323 |
} |
|
324 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10437
diff
changeset
|
325 |
static Class<?> implClass = null; |
2 | 326 |
|
327 |
void createImpl() throws SocketException { |
|
328 |
if (impl == null) { |
|
329 |
if (factory != null) { |
|
330 |
impl = factory.createDatagramSocketImpl(); |
|
331 |
checkOldImpl(); |
|
332 |
} else { |
|
333 |
boolean isMulticast = (this instanceof MulticastSocket) ? true : false; |
|
334 |
impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(isMulticast); |
|
335 |
||
336 |
checkOldImpl(); |
|
337 |
} |
|
338 |
} |
|
339 |
// creates a udp socket |
|
340 |
impl.create(); |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
341 |
impl.setDatagramSocket(this); |
2 | 342 |
created = true; |
343 |
} |
|
344 |
||
345 |
/** |
|
19069 | 346 |
* Get the {@code DatagramSocketImpl} attached to this socket, |
2 | 347 |
* creating it if necessary. |
348 |
* |
|
19069 | 349 |
* @return the {@code DatagramSocketImpl} attached to that |
2 | 350 |
* DatagramSocket |
351 |
* @throws SocketException if creation fails. |
|
352 |
* @since 1.4 |
|
353 |
*/ |
|
354 |
DatagramSocketImpl getImpl() throws SocketException { |
|
355 |
if (!created) |
|
356 |
createImpl(); |
|
357 |
return impl; |
|
358 |
} |
|
359 |
||
360 |
/** |
|
18156 | 361 |
* Binds this DatagramSocket to a specific address and port. |
2 | 362 |
* <p> |
19069 | 363 |
* If the address is {@code null}, then the system will pick up |
2 | 364 |
* an ephemeral port and a valid local address to bind the socket. |
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
21428
diff
changeset
|
365 |
* |
18156 | 366 |
* @param addr The address and port to bind to. |
2 | 367 |
* @throws SocketException if any error happens during the bind, or if the |
368 |
* socket is already bound. |
|
369 |
* @throws SecurityException if a security manager exists and its |
|
19069 | 370 |
* {@code checkListen} method doesn't allow the operation. |
2 | 371 |
* @throws IllegalArgumentException if addr is a SocketAddress subclass |
372 |
* not supported by this socket. |
|
373 |
* @since 1.4 |
|
374 |
*/ |
|
375 |
public synchronized void bind(SocketAddress addr) throws SocketException { |
|
376 |
if (isClosed()) |
|
377 |
throw new SocketException("Socket is closed"); |
|
378 |
if (isBound()) |
|
379 |
throw new SocketException("already bound"); |
|
380 |
if (addr == null) |
|
381 |
addr = new InetSocketAddress(0); |
|
382 |
if (!(addr instanceof InetSocketAddress)) |
|
383 |
throw new IllegalArgumentException("Unsupported address type!"); |
|
384 |
InetSocketAddress epoint = (InetSocketAddress) addr; |
|
385 |
if (epoint.isUnresolved()) |
|
386 |
throw new SocketException("Unresolved address"); |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
387 |
InetAddress iaddr = epoint.getAddress(); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
388 |
int port = epoint.getPort(); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
389 |
checkAddress(iaddr, "bind"); |
2 | 390 |
SecurityManager sec = System.getSecurityManager(); |
391 |
if (sec != null) { |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
392 |
sec.checkListen(port); |
2 | 393 |
} |
394 |
try { |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
395 |
getImpl().bind(port, iaddr); |
2 | 396 |
} catch (SocketException e) { |
397 |
getImpl().close(); |
|
398 |
throw e; |
|
399 |
} |
|
400 |
bound = true; |
|
401 |
} |
|
402 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
403 |
void checkAddress (InetAddress addr, String op) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
404 |
if (addr == null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
405 |
return; |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
406 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
407 |
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
408 |
throw new IllegalArgumentException(op + ": invalid address type"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
409 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
410 |
} |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
411 |
|
2 | 412 |
/** |
413 |
* Connects the socket to a remote address for this socket. When a |
|
414 |
* socket is connected to a remote address, packets may only be |
|
415 |
* sent to or received from that address. By default a datagram |
|
416 |
* socket is not connected. |
|
417 |
* |
|
418 |
* <p>If the remote destination to which the socket is connected does not |
|
419 |
* exist, or is otherwise unreachable, and if an ICMP destination unreachable |
|
420 |
* packet has been received for that address, then a subsequent call to |
|
421 |
* send or receive may throw a PortUnreachableException. Note, there is no |
|
422 |
* guarantee that the exception will be thrown. |
|
423 |
* |
|
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
424 |
* <p> If a security manager has been installed then it is invoked to check |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
425 |
* access to the remote address. Specifically, if the given {@code address} |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
426 |
* is a {@link InetAddress#isMulticastAddress multicast address}, |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
427 |
* the security manager's {@link |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
428 |
* java.lang.SecurityManager#checkMulticast(InetAddress) |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
429 |
* checkMulticast} method is invoked with the given {@code address}. |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
430 |
* Otherwise, the security manager's {@link |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
431 |
* java.lang.SecurityManager#checkConnect(String,int) checkConnect} |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
432 |
* and {@link java.lang.SecurityManager#checkAccept checkAccept} methods |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
433 |
* are invoked, with the given {@code address} and {@code port}, to |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
434 |
* verify that datagrams are permitted to be sent and received |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
435 |
* respectively. |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
436 |
* |
58501
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
437 |
* <p> Care should be taken to ensure that a connected datagram socket |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
438 |
* is not shared with untrusted code. When a socket is connected, |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
439 |
* {@link #receive receive} and {@link #send send} <b>will not perform |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
440 |
* any security checks</b> on incoming and outgoing packets, other than |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
441 |
* matching the packet's and the socket's address and port. On a send |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
442 |
* operation, if the packet's address is set and the packet's address |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
443 |
* and the socket's address do not match, an {@code IllegalArgumentException} |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
444 |
* will be thrown. A socket connected to a multicast address may only |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
445 |
* be used to send packets. |
2 | 446 |
* |
447 |
* @param address the remote address for the socket |
|
448 |
* |
|
449 |
* @param port the remote port for the socket. |
|
450 |
* |
|
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
451 |
* @throws IllegalArgumentException |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
452 |
* if the address is null, or the port is out of range. |
2 | 453 |
* |
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
454 |
* @throws SecurityException |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
455 |
* if a security manager has been installed and it does |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
456 |
* not permit access to the given remote address |
2 | 457 |
* |
458 |
* @see #disconnect |
|
459 |
*/ |
|
460 |
public void connect(InetAddress address, int port) { |
|
461 |
try { |
|
462 |
connectInternal(address, port); |
|
463 |
} catch (SocketException se) { |
|
464 |
throw new Error("connect failed", se); |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
/** |
|
469 |
* Connects this socket to a remote socket address (IP address + port number). |
|
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
470 |
* |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
471 |
* <p> If given an {@link InetSocketAddress InetSocketAddress}, this method |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
472 |
* behaves as if invoking {@link #connect(InetAddress,int) connect(InetAddress,int)} |
23927 | 473 |
* with the given socket addresses IP address and port number. |
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
474 |
* |
2 | 475 |
* @param addr The remote address. |
5465
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
476 |
* |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
477 |
* @throws SocketException |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
478 |
* if the connect fails |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
479 |
* |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
480 |
* @throws IllegalArgumentException |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
481 |
* if {@code addr} is {@code null}, or {@code addr} is a SocketAddress |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
482 |
* subclass not supported by this socket |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
483 |
* |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
484 |
* @throws SecurityException |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
485 |
* if a security manager has been installed and it does |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
486 |
* not permit access to the given remote address |
950789cfc3c5
6946673: DatagramSocket.connect() documentation contradicts the implementation
chegar
parents:
5180
diff
changeset
|
487 |
* |
2 | 488 |
* @since 1.4 |
489 |
*/ |
|
490 |
public void connect(SocketAddress addr) throws SocketException { |
|
491 |
if (addr == null) |
|
492 |
throw new IllegalArgumentException("Address can't be null"); |
|
493 |
if (!(addr instanceof InetSocketAddress)) |
|
494 |
throw new IllegalArgumentException("Unsupported address type"); |
|
495 |
InetSocketAddress epoint = (InetSocketAddress) addr; |
|
496 |
if (epoint.isUnresolved()) |
|
497 |
throw new SocketException("Unresolved address"); |
|
498 |
connectInternal(epoint.getAddress(), epoint.getPort()); |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Disconnects the socket. If the socket is closed or not connected, |
|
503 |
* then this method has no effect. |
|
504 |
* |
|
505 |
* @see #connect |
|
506 |
*/ |
|
507 |
public void disconnect() { |
|
508 |
synchronized (this) { |
|
509 |
if (isClosed()) |
|
510 |
return; |
|
511 |
if (connectState == ST_CONNECTED) { |
|
512 |
impl.disconnect (); |
|
513 |
} |
|
514 |
connectedAddress = null; |
|
515 |
connectedPort = -1; |
|
516 |
connectState = ST_NOT_CONNECTED; |
|
27078 | 517 |
explicitFilter = false; |
2 | 518 |
} |
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* Returns the binding state of the socket. |
|
523 |
* <p> |
|
524 |
* If the socket was bound prior to being {@link #close closed}, |
|
19069 | 525 |
* then this method will continue to return {@code true} |
2 | 526 |
* after the socket is closed. |
527 |
* |
|
528 |
* @return true if the socket successfully bound to an address |
|
529 |
* @since 1.4 |
|
530 |
*/ |
|
531 |
public boolean isBound() { |
|
532 |
return bound; |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* Returns the connection state of the socket. |
|
537 |
* <p> |
|
538 |
* If the socket was connected prior to being {@link #close closed}, |
|
19069 | 539 |
* then this method will continue to return {@code true} |
2 | 540 |
* after the socket is closed. |
541 |
* |
|
542 |
* @return true if the socket successfully connected to a server |
|
543 |
* @since 1.4 |
|
544 |
*/ |
|
545 |
public boolean isConnected() { |
|
546 |
return connectState != ST_NOT_CONNECTED; |
|
547 |
} |
|
548 |
||
549 |
/** |
|
550 |
* Returns the address to which this socket is connected. Returns |
|
19069 | 551 |
* {@code null} if the socket is not connected. |
2 | 552 |
* <p> |
553 |
* If the socket was connected prior to being {@link #close closed}, |
|
554 |
* then this method will continue to return the connected address |
|
555 |
* after the socket is closed. |
|
556 |
* |
|
557 |
* @return the address to which this socket is connected. |
|
558 |
*/ |
|
559 |
public InetAddress getInetAddress() { |
|
560 |
return connectedAddress; |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Returns the port number to which this socket is connected. |
|
19069 | 565 |
* Returns {@code -1} if the socket is not connected. |
2 | 566 |
* <p> |
567 |
* If the socket was connected prior to being {@link #close closed}, |
|
568 |
* then this method will continue to return the connected port number |
|
569 |
* after the socket is closed. |
|
570 |
* |
|
571 |
* @return the port number to which this socket is connected. |
|
572 |
*/ |
|
573 |
public int getPort() { |
|
574 |
return connectedPort; |
|
575 |
} |
|
576 |
||
577 |
/** |
|
578 |
* Returns the address of the endpoint this socket is connected to, or |
|
19069 | 579 |
* {@code null} if it is unconnected. |
2 | 580 |
* <p> |
581 |
* If the socket was connected prior to being {@link #close closed}, |
|
582 |
* then this method will continue to return the connected address |
|
583 |
* after the socket is closed. |
|
584 |
* |
|
19069 | 585 |
* @return a {@code SocketAddress} representing the remote |
586 |
* endpoint of this socket, or {@code null} if it is |
|
2 | 587 |
* not connected yet. |
588 |
* @see #getInetAddress() |
|
589 |
* @see #getPort() |
|
590 |
* @see #connect(SocketAddress) |
|
591 |
* @since 1.4 |
|
592 |
*/ |
|
593 |
public SocketAddress getRemoteSocketAddress() { |
|
594 |
if (!isConnected()) |
|
595 |
return null; |
|
596 |
return new InetSocketAddress(getInetAddress(), getPort()); |
|
597 |
} |
|
598 |
||
599 |
/** |
|
600 |
* Returns the address of the endpoint this socket is bound to. |
|
601 |
* |
|
19069 | 602 |
* @return a {@code SocketAddress} representing the local endpoint of this |
603 |
* socket, or {@code null} if it is closed or not bound yet. |
|
2 | 604 |
* @see #getLocalAddress() |
605 |
* @see #getLocalPort() |
|
606 |
* @see #bind(SocketAddress) |
|
607 |
* @since 1.4 |
|
608 |
*/ |
|
609 |
||
610 |
public SocketAddress getLocalSocketAddress() { |
|
611 |
if (isClosed()) |
|
612 |
return null; |
|
613 |
if (!isBound()) |
|
614 |
return null; |
|
615 |
return new InetSocketAddress(getLocalAddress(), getLocalPort()); |
|
616 |
} |
|
617 |
||
618 |
/** |
|
619 |
* Sends a datagram packet from this socket. The |
|
19069 | 620 |
* {@code DatagramPacket} includes information indicating the |
2 | 621 |
* data to be sent, its length, the IP address of the remote host, |
622 |
* and the port number on the remote host. |
|
623 |
* |
|
624 |
* <p>If there is a security manager, and the socket is not currently |
|
625 |
* connected to a remote address, this method first performs some |
|
19069 | 626 |
* security checks. First, if {@code p.getAddress().isMulticastAddress()} |
2 | 627 |
* is true, this method calls the |
19069 | 628 |
* security manager's {@code checkMulticast} method |
629 |
* with {@code p.getAddress()} as its argument. |
|
2 | 630 |
* If the evaluation of that expression is false, |
631 |
* this method instead calls the security manager's |
|
19069 | 632 |
* {@code checkConnect} method with arguments |
633 |
* {@code p.getAddress().getHostAddress()} and |
|
634 |
* {@code p.getPort()}. Each call to a security manager method |
|
2 | 635 |
* could result in a SecurityException if the operation is not allowed. |
636 |
* |
|
19069 | 637 |
* @param p the {@code DatagramPacket} to be sent. |
2 | 638 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
639 |
* @throws IOException if an I/O error occurs. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
640 |
* @throws SecurityException if a security manager exists and its |
19069 | 641 |
* {@code checkMulticast} or {@code checkConnect} |
2 | 642 |
* method doesn't allow the send. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
643 |
* @throws PortUnreachableException may be thrown if the socket is connected |
2 | 644 |
* to a currently unreachable destination. Note, there is no |
645 |
* guarantee that the exception will be thrown. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
646 |
* @throws java.nio.channels.IllegalBlockingModeException |
2 | 647 |
* if this socket has an associated channel, |
648 |
* and the channel is in non-blocking mode. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
649 |
* @throws IllegalArgumentException if the socket is connected, |
2 | 650 |
* and connected address and packet address differ. |
651 |
* |
|
652 |
* @see java.net.DatagramPacket |
|
653 |
* @see SecurityManager#checkMulticast(InetAddress) |
|
654 |
* @see SecurityManager#checkConnect |
|
655 |
* @revised 1.4 |
|
656 |
* @spec JSR-51 |
|
657 |
*/ |
|
658 |
public void send(DatagramPacket p) throws IOException { |
|
659 |
InetAddress packetAddress = null; |
|
660 |
synchronized (p) { |
|
661 |
if (isClosed()) |
|
662 |
throw new SocketException("Socket is closed"); |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
1817
diff
changeset
|
663 |
checkAddress (p.getAddress(), "send"); |
2 | 664 |
if (connectState == ST_NOT_CONNECTED) { |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47478
diff
changeset
|
665 |
// check the address is ok with the security manager on every send. |
2 | 666 |
SecurityManager security = System.getSecurityManager(); |
667 |
||
668 |
// The reason you want to synchronize on datagram packet |
|
21278 | 669 |
// is because you don't want an applet to change the address |
2 | 670 |
// while you are trying to send the packet for example |
671 |
// after the security check but before the send. |
|
672 |
if (security != null) { |
|
673 |
if (p.getAddress().isMulticastAddress()) { |
|
674 |
security.checkMulticast(p.getAddress()); |
|
675 |
} else { |
|
676 |
security.checkConnect(p.getAddress().getHostAddress(), |
|
677 |
p.getPort()); |
|
678 |
} |
|
679 |
} |
|
680 |
} else { |
|
681 |
// we're connected |
|
682 |
packetAddress = p.getAddress(); |
|
683 |
if (packetAddress == null) { |
|
684 |
p.setAddress(connectedAddress); |
|
685 |
p.setPort(connectedPort); |
|
686 |
} else if ((!packetAddress.equals(connectedAddress)) || |
|
687 |
p.getPort() != connectedPort) { |
|
688 |
throw new IllegalArgumentException("connected address " + |
|
689 |
"and packet address" + |
|
690 |
" differ"); |
|
691 |
} |
|
692 |
} |
|
693 |
// Check whether the socket is bound |
|
694 |
if (!isBound()) |
|
695 |
bind(new InetSocketAddress(0)); |
|
696 |
// call the method to send |
|
697 |
getImpl().send(p); |
|
698 |
} |
|
699 |
} |
|
700 |
||
701 |
/** |
|
702 |
* Receives a datagram packet from this socket. When this method |
|
19069 | 703 |
* returns, the {@code DatagramPacket}'s buffer is filled with |
2 | 704 |
* the data received. The datagram packet also contains the sender's |
705 |
* IP address, and the port number on the sender's machine. |
|
706 |
* <p> |
|
707 |
* This method blocks until a datagram is received. The |
|
19069 | 708 |
* {@code length} field of the datagram packet object contains |
2 | 709 |
* the length of the received message. If the message is longer than |
710 |
* the packet's length, the message is truncated. |
|
711 |
* <p> |
|
58501
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
712 |
* If there is a security manager, and the socket is not currently |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
713 |
* connected to a remote address, a packet cannot be received if the |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
714 |
* security manager's {@code checkAccept} method does not allow it. |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
715 |
* Datagrams that are not permitted by the security manager are silently |
6fc4a729763e
8230946: Clarify security manager behaviour of a connected DatagramSocket and DatagramChannel
pconcannon
parents:
58242
diff
changeset
|
716 |
* discarded. |
2 | 717 |
* |
19069 | 718 |
* @param p the {@code DatagramPacket} into which to place |
2 | 719 |
* the incoming data. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
720 |
* @throws IOException if an I/O error occurs. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
721 |
* @throws SocketTimeoutException if setSoTimeout was previously called |
2 | 722 |
* and the timeout has expired. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
723 |
* @throws PortUnreachableException may be thrown if the socket is connected |
2 | 724 |
* to a currently unreachable destination. Note, there is no guarantee that the |
725 |
* exception will be thrown. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
726 |
* @throws java.nio.channels.IllegalBlockingModeException |
2 | 727 |
* if this socket has an associated channel, |
728 |
* and the channel is in non-blocking mode. |
|
729 |
* @see java.net.DatagramPacket |
|
730 |
* @see java.net.DatagramSocket |
|
731 |
* @revised 1.4 |
|
732 |
* @spec JSR-51 |
|
733 |
*/ |
|
734 |
public synchronized void receive(DatagramPacket p) throws IOException { |
|
735 |
synchronized (p) { |
|
736 |
if (!isBound()) |
|
737 |
bind(new InetSocketAddress(0)); |
|
738 |
if (connectState == ST_NOT_CONNECTED) { |
|
739 |
// check the address is ok with the security manager before every recv. |
|
740 |
SecurityManager security = System.getSecurityManager(); |
|
741 |
if (security != null) { |
|
742 |
while(true) { |
|
743 |
String peekAd = null; |
|
744 |
int peekPort = 0; |
|
745 |
// peek at the packet to see who it is from. |
|
746 |
if (!oldImpl) { |
|
747 |
// We can use the new peekData() API |
|
748 |
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1); |
|
749 |
peekPort = getImpl().peekData(peekPacket); |
|
750 |
peekAd = peekPacket.getAddress().getHostAddress(); |
|
751 |
} else { |
|
752 |
InetAddress adr = new InetAddress(); |
|
753 |
peekPort = getImpl().peek(adr); |
|
754 |
peekAd = adr.getHostAddress(); |
|
755 |
} |
|
756 |
try { |
|
757 |
security.checkAccept(peekAd, peekPort); |
|
758 |
// security check succeeded - so now break |
|
759 |
// and recv the packet. |
|
760 |
break; |
|
761 |
} catch (SecurityException se) { |
|
762 |
// Throw away the offending packet by consuming |
|
763 |
// it in a tmp buffer. |
|
764 |
DatagramPacket tmp = new DatagramPacket(new byte[1], 1); |
|
765 |
getImpl().receive(tmp); |
|
766 |
||
767 |
// silently discard the offending packet |
|
768 |
// and continue: unknown/malicious |
|
769 |
// entities on nets should not make |
|
770 |
// runtime throw security exception and |
|
771 |
// disrupt the applet by sending random |
|
772 |
// datagram packets. |
|
773 |
continue; |
|
774 |
} |
|
775 |
} // end of while |
|
776 |
} |
|
777 |
} |
|
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
778 |
DatagramPacket tmp = null; |
27078 | 779 |
if ((connectState == ST_CONNECTED_NO_IMPL) || explicitFilter) { |
2 | 780 |
// We have to do the filtering the old fashioned way since |
781 |
// the native impl doesn't support connect or the connect |
|
27078 | 782 |
// via the impl failed, or .. "explicitFilter" may be set when |
783 |
// a socket is connected via the impl, for a period of time |
|
784 |
// when packets from other sources might be queued on socket. |
|
2 | 785 |
boolean stop = false; |
786 |
while (!stop) { |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
787 |
InetAddress peekAddress = null; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
788 |
int peekPort = -1; |
2 | 789 |
// peek at the packet to see who it is from. |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
790 |
if (!oldImpl) { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
791 |
// We can use the new peekData() API |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
792 |
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
793 |
peekPort = getImpl().peekData(peekPacket); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
794 |
peekAddress = peekPacket.getAddress(); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
795 |
} else { |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
796 |
// this api only works for IPv4 |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
797 |
peekAddress = new InetAddress(); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
798 |
peekPort = getImpl().peek(peekAddress); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
10701
diff
changeset
|
799 |
} |
2 | 800 |
if ((!connectedAddress.equals(peekAddress)) || |
801 |
(connectedPort != peekPort)) { |
|
802 |
// throw the packet away and silently continue |
|
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
803 |
tmp = new DatagramPacket( |
27078 | 804 |
new byte[1024], 1024); |
2 | 805 |
getImpl().receive(tmp); |
27078 | 806 |
if (explicitFilter) { |
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
807 |
if (checkFiltering(tmp)) { |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
808 |
stop = true; |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
809 |
} |
27078 | 810 |
} |
2 | 811 |
} else { |
812 |
stop = true; |
|
813 |
} |
|
814 |
} |
|
815 |
} |
|
816 |
// If the security check succeeds, or the datagram is |
|
817 |
// connected then receive the packet |
|
818 |
getImpl().receive(p); |
|
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
819 |
if (explicitFilter && tmp == null) { |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
820 |
// packet was not filtered, account for it here |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
821 |
checkFiltering(p); |
27078 | 822 |
} |
2 | 823 |
} |
824 |
} |
|
825 |
||
27724
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
826 |
private boolean checkFiltering(DatagramPacket p) throws SocketException { |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
827 |
bytesLeftToFilter -= p.getLength(); |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
828 |
if (bytesLeftToFilter <= 0 || getImpl().dataAvailable() <= 0) { |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
829 |
explicitFilter = false; |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
830 |
return true; |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
831 |
} |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
832 |
return false; |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
833 |
} |
9a42fe09eb61
8053963: (dc) Use DatagramChannel.receive() instead of read() in connect
michaelm
parents:
27078
diff
changeset
|
834 |
|
2 | 835 |
/** |
836 |
* Gets the local address to which the socket is bound. |
|
837 |
* |
|
838 |
* <p>If there is a security manager, its |
|
19069 | 839 |
* {@code checkConnect} method is first called |
840 |
* with the host address and {@code -1} |
|
2 | 841 |
* as its arguments to see if the operation is allowed. |
842 |
* |
|
843 |
* @see SecurityManager#checkConnect |
|
844 |
* @return the local address to which the socket is bound, |
|
19069 | 845 |
* {@code null} if the socket is closed, or |
846 |
* an {@code InetAddress} representing |
|
2 | 847 |
* {@link InetAddress#isAnyLocalAddress wildcard} |
848 |
* address if either the socket is not bound, or |
|
19069 | 849 |
* the security manager {@code checkConnect} |
2 | 850 |
* method does not allow the operation |
851 |
* @since 1.1 |
|
852 |
*/ |
|
853 |
public InetAddress getLocalAddress() { |
|
854 |
if (isClosed()) |
|
855 |
return null; |
|
856 |
InetAddress in = null; |
|
857 |
try { |
|
858 |
in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); |
|
859 |
if (in.isAnyLocalAddress()) { |
|
860 |
in = InetAddress.anyLocalAddress(); |
|
861 |
} |
|
862 |
SecurityManager s = System.getSecurityManager(); |
|
863 |
if (s != null) { |
|
864 |
s.checkConnect(in.getHostAddress(), -1); |
|
865 |
} |
|
866 |
} catch (Exception e) { |
|
867 |
in = InetAddress.anyLocalAddress(); // "0.0.0.0" |
|
868 |
} |
|
869 |
return in; |
|
870 |
} |
|
871 |
||
872 |
/** |
|
873 |
* Returns the port number on the local host to which this socket |
|
874 |
* is bound. |
|
875 |
* |
|
876 |
* @return the port number on the local host to which this socket is bound, |
|
19069 | 877 |
{@code -1} if the socket is closed, or |
878 |
{@code 0} if it is not bound yet. |
|
2 | 879 |
*/ |
880 |
public int getLocalPort() { |
|
881 |
if (isClosed()) |
|
882 |
return -1; |
|
883 |
try { |
|
884 |
return getImpl().getLocalPort(); |
|
885 |
} catch (Exception e) { |
|
886 |
return 0; |
|
887 |
} |
|
888 |
} |
|
889 |
||
890 |
/** Enable/disable SO_TIMEOUT with the specified timeout, in |
|
891 |
* milliseconds. With this option set to a non-zero timeout, |
|
892 |
* a call to receive() for this DatagramSocket |
|
893 |
* will block for only this amount of time. If the timeout expires, |
|
894 |
* a <B>java.net.SocketTimeoutException</B> is raised, though the |
|
895 |
* DatagramSocket is still valid. The option <B>must</B> be enabled |
|
896 |
* prior to entering the blocking operation to have effect. The |
|
18156 | 897 |
* timeout must be {@code > 0}. |
2 | 898 |
* A timeout of zero is interpreted as an infinite timeout. |
899 |
* |
|
900 |
* @param timeout the specified timeout in milliseconds. |
|
901 |
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error. |
|
58502
7cb1218ef4d0
8222829: DatagramSocket.setSoTimeout does not specify IAE when timeout is negative
pconcannon
parents:
58501
diff
changeset
|
902 |
* @throws IllegalArgumentException if {@code timeout} is negative |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23927
diff
changeset
|
903 |
* @since 1.1 |
2 | 904 |
* @see #getSoTimeout() |
905 |
*/ |
|
906 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
907 |
if (isClosed()) |
|
908 |
throw new SocketException("Socket is closed"); |
|
58502
7cb1218ef4d0
8222829: DatagramSocket.setSoTimeout does not specify IAE when timeout is negative
pconcannon
parents:
58501
diff
changeset
|
909 |
if (timeout < 0) |
7cb1218ef4d0
8222829: DatagramSocket.setSoTimeout does not specify IAE when timeout is negative
pconcannon
parents:
58501
diff
changeset
|
910 |
throw new IllegalArgumentException("timeout < 0"); |
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
911 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout); |
2 | 912 |
} |
913 |
||
914 |
/** |
|
915 |
* Retrieve setting for SO_TIMEOUT. 0 returns implies that the |
|
916 |
* option is disabled (i.e., timeout of infinity). |
|
917 |
* |
|
918 |
* @return the setting for SO_TIMEOUT |
|
919 |
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23927
diff
changeset
|
920 |
* @since 1.1 |
2 | 921 |
* @see #setSoTimeout(int) |
922 |
*/ |
|
923 |
public synchronized int getSoTimeout() throws SocketException { |
|
924 |
if (isClosed()) |
|
925 |
throw new SocketException("Socket is closed"); |
|
926 |
if (getImpl() == null) |
|
927 |
return 0; |
|
928 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
929 |
/* extra type safety */ |
|
930 |
if (o instanceof Integer) { |
|
931 |
return ((Integer) o).intValue(); |
|
932 |
} else { |
|
933 |
return 0; |
|
934 |
} |
|
935 |
} |
|
936 |
||
937 |
/** |
|
938 |
* Sets the SO_SNDBUF option to the specified value for this |
|
19069 | 939 |
* {@code DatagramSocket}. The SO_SNDBUF option is used by the |
2 | 940 |
* network implementation as a hint to size the underlying |
941 |
* network I/O buffers. The SO_SNDBUF setting may also be used |
|
942 |
* by the network implementation to determine the maximum size |
|
943 |
* of the packet that can be sent on this socket. |
|
944 |
* <p> |
|
945 |
* As SO_SNDBUF is a hint, applications that want to verify |
|
946 |
* what size the buffer is should call {@link #getSendBufferSize()}. |
|
947 |
* <p> |
|
948 |
* Increasing the buffer size may allow multiple outgoing packets |
|
949 |
* to be queued by the network implementation when the send rate |
|
950 |
* is high. |
|
951 |
* <p> |
|
952 |
* Note: If {@link #send(DatagramPacket)} is used to send a |
|
19069 | 953 |
* {@code DatagramPacket} that is larger than the setting |
2 | 954 |
* of SO_SNDBUF then it is implementation specific if the |
955 |
* packet is sent or discarded. |
|
956 |
* |
|
957 |
* @param size the size to which to set the send buffer |
|
958 |
* size. This value must be greater than 0. |
|
959 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
960 |
* @throws SocketException if there is an error |
2 | 961 |
* in the underlying protocol, such as an UDP error. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
962 |
* @throws IllegalArgumentException if the value is 0 or is |
2 | 963 |
* negative. |
964 |
* @see #getSendBufferSize() |
|
965 |
*/ |
|
966 |
public synchronized void setSendBufferSize(int size) |
|
967 |
throws SocketException{ |
|
968 |
if (!(size > 0)) { |
|
969 |
throw new IllegalArgumentException("negative send size"); |
|
970 |
} |
|
971 |
if (isClosed()) |
|
972 |
throw new SocketException("Socket is closed"); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
973 |
getImpl().setOption(SocketOptions.SO_SNDBUF, size); |
2 | 974 |
} |
975 |
||
976 |
/** |
|
19069 | 977 |
* Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the |
978 |
* buffer size used by the platform for output on this {@code DatagramSocket}. |
|
2 | 979 |
* |
19069 | 980 |
* @return the value of the SO_SNDBUF option for this {@code DatagramSocket} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
981 |
* @throws SocketException if there is an error in |
2 | 982 |
* the underlying protocol, such as an UDP error. |
983 |
* @see #setSendBufferSize |
|
984 |
*/ |
|
985 |
public synchronized int getSendBufferSize() throws SocketException { |
|
986 |
if (isClosed()) |
|
987 |
throw new SocketException("Socket is closed"); |
|
988 |
int result = 0; |
|
989 |
Object o = getImpl().getOption(SocketOptions.SO_SNDBUF); |
|
990 |
if (o instanceof Integer) { |
|
991 |
result = ((Integer)o).intValue(); |
|
992 |
} |
|
993 |
return result; |
|
994 |
} |
|
995 |
||
996 |
/** |
|
997 |
* Sets the SO_RCVBUF option to the specified value for this |
|
47478 | 998 |
* {@code DatagramSocket}. The SO_RCVBUF option is used by |
2 | 999 |
* the network implementation as a hint to size the underlying |
1000 |
* network I/O buffers. The SO_RCVBUF setting may also be used |
|
1001 |
* by the network implementation to determine the maximum size |
|
1002 |
* of the packet that can be received on this socket. |
|
1003 |
* <p> |
|
1004 |
* Because SO_RCVBUF is a hint, applications that want to |
|
1005 |
* verify what size the buffers were set to should call |
|
1006 |
* {@link #getReceiveBufferSize()}. |
|
1007 |
* <p> |
|
1008 |
* Increasing SO_RCVBUF may allow the network implementation |
|
1009 |
* to buffer multiple packets when packets arrive faster than |
|
1010 |
* are being received using {@link #receive(DatagramPacket)}. |
|
1011 |
* <p> |
|
1012 |
* Note: It is implementation specific if a packet larger |
|
1013 |
* than SO_RCVBUF can be received. |
|
1014 |
* |
|
1015 |
* @param size the size to which to set the receive buffer |
|
1016 |
* size. This value must be greater than 0. |
|
1017 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1018 |
* @throws SocketException if there is an error in |
2 | 1019 |
* the underlying protocol, such as an UDP error. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1020 |
* @throws IllegalArgumentException if the value is 0 or is |
2 | 1021 |
* negative. |
1022 |
* @see #getReceiveBufferSize() |
|
1023 |
*/ |
|
1024 |
public synchronized void setReceiveBufferSize(int size) |
|
1025 |
throws SocketException{ |
|
1026 |
if (size <= 0) { |
|
1027 |
throw new IllegalArgumentException("invalid receive size"); |
|
1028 |
} |
|
1029 |
if (isClosed()) |
|
1030 |
throw new SocketException("Socket is closed"); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1031 |
getImpl().setOption(SocketOptions.SO_RCVBUF, size); |
2 | 1032 |
} |
1033 |
||
1034 |
/** |
|
19069 | 1035 |
* Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the |
1036 |
* buffer size used by the platform for input on this {@code DatagramSocket}. |
|
2 | 1037 |
* |
19069 | 1038 |
* @return the value of the SO_RCVBUF option for this {@code DatagramSocket} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1039 |
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error. |
2 | 1040 |
* @see #setReceiveBufferSize(int) |
1041 |
*/ |
|
1042 |
public synchronized int getReceiveBufferSize() |
|
1043 |
throws SocketException{ |
|
1044 |
if (isClosed()) |
|
1045 |
throw new SocketException("Socket is closed"); |
|
1046 |
int result = 0; |
|
1047 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
1048 |
if (o instanceof Integer) { |
|
1049 |
result = ((Integer)o).intValue(); |
|
1050 |
} |
|
1051 |
return result; |
|
1052 |
} |
|
1053 |
||
1054 |
/** |
|
1055 |
* Enable/disable the SO_REUSEADDR socket option. |
|
1056 |
* <p> |
|
1057 |
* For UDP sockets it may be necessary to bind more than one |
|
1058 |
* socket to the same socket address. This is typically for the |
|
1059 |
* purpose of receiving multicast packets |
|
1060 |
* (See {@link java.net.MulticastSocket}). The |
|
19069 | 1061 |
* {@code SO_REUSEADDR} socket option allows multiple |
2 | 1062 |
* sockets to be bound to the same socket address if the |
19069 | 1063 |
* {@code SO_REUSEADDR} socket option is enabled prior |
2 | 1064 |
* to binding the socket using {@link #bind(SocketAddress)}. |
1065 |
* <p> |
|
1066 |
* Note: This functionality is not supported by all existing platforms, |
|
1067 |
* so it is implementation specific whether this option will be ignored |
|
1068 |
* or not. However, if it is not supported then |
|
19069 | 1069 |
* {@link #getReuseAddress()} will always return {@code false}. |
2 | 1070 |
* <p> |
19069 | 1071 |
* When a {@code DatagramSocket} is created the initial setting |
1072 |
* of {@code SO_REUSEADDR} is disabled. |
|
2 | 1073 |
* <p> |
19069 | 1074 |
* The behaviour when {@code SO_REUSEADDR} is enabled or |
2 | 1075 |
* disabled after a socket is bound (See {@link #isBound()}) |
1076 |
* is not defined. |
|
1077 |
* |
|
1078 |
* @param on whether to enable or disable the |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1079 |
* @throws SocketException if an error occurs enabling or |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47478
diff
changeset
|
1080 |
* disabling the {@code SO_REUSEADDR} socket option, |
2 | 1081 |
* or the socket is closed. |
1082 |
* @since 1.4 |
|
1083 |
* @see #getReuseAddress() |
|
1084 |
* @see #bind(SocketAddress) |
|
1085 |
* @see #isBound() |
|
1086 |
* @see #isClosed() |
|
1087 |
*/ |
|
1088 |
public synchronized void setReuseAddress(boolean on) throws SocketException { |
|
1089 |
if (isClosed()) |
|
1090 |
throw new SocketException("Socket is closed"); |
|
1091 |
// Integer instead of Boolean for compatibility with older DatagramSocketImpl |
|
1092 |
if (oldImpl) |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
24865
diff
changeset
|
1093 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, on?-1:0); |
2 | 1094 |
else |
1095 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
1096 |
} |
|
1097 |
||
1098 |
/** |
|
1099 |
* Tests if SO_REUSEADDR is enabled. |
|
1100 |
* |
|
19069 | 1101 |
* @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1102 |
* @throws SocketException if there is an error |
2 | 1103 |
* in the underlying protocol, such as an UDP error. |
1104 |
* @since 1.4 |
|
1105 |
* @see #setReuseAddress(boolean) |
|
1106 |
*/ |
|
1107 |
public synchronized boolean getReuseAddress() throws SocketException { |
|
1108 |
if (isClosed()) |
|
1109 |
throw new SocketException("Socket is closed"); |
|
1110 |
Object o = getImpl().getOption(SocketOptions.SO_REUSEADDR); |
|
1111 |
return ((Boolean)o).booleanValue(); |
|
1112 |
} |
|
1113 |
||
1114 |
/** |
|
1115 |
* Enable/disable SO_BROADCAST. |
|
1817
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1116 |
* |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1117 |
* <p> Some operating systems may require that the Java virtual machine be |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1118 |
* started with implementation specific privileges to enable this option or |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1119 |
* send broadcast datagrams. |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1120 |
* |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1121 |
* @param on |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1122 |
* whether or not to have broadcast turned on. |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1123 |
* |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1124 |
* @throws SocketException |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1125 |
* if there is an error in the underlying protocol, such as an UDP |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1126 |
* error. |
2deb4cc4d5dc
6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents:
715
diff
changeset
|
1127 |
* |
2 | 1128 |
* @since 1.4 |
1129 |
* @see #getBroadcast() |
|
1130 |
*/ |
|
1131 |
public synchronized void setBroadcast(boolean on) throws SocketException { |
|
1132 |
if (isClosed()) |
|
1133 |
throw new SocketException("Socket is closed"); |
|
1134 |
getImpl().setOption(SocketOptions.SO_BROADCAST, Boolean.valueOf(on)); |
|
1135 |
} |
|
1136 |
||
1137 |
/** |
|
1138 |
* Tests if SO_BROADCAST is enabled. |
|
19069 | 1139 |
* @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1140 |
* @throws SocketException if there is an error |
2 | 1141 |
* in the underlying protocol, such as an UDP error. |
1142 |
* @since 1.4 |
|
1143 |
* @see #setBroadcast(boolean) |
|
1144 |
*/ |
|
1145 |
public synchronized boolean getBroadcast() throws SocketException { |
|
1146 |
if (isClosed()) |
|
1147 |
throw new SocketException("Socket is closed"); |
|
1148 |
return ((Boolean)(getImpl().getOption(SocketOptions.SO_BROADCAST))).booleanValue(); |
|
1149 |
} |
|
1150 |
||
1151 |
/** |
|
1152 |
* Sets traffic class or type-of-service octet in the IP |
|
1153 |
* datagram header for datagrams sent from this DatagramSocket. |
|
1154 |
* As the underlying network implementation may ignore this |
|
1155 |
* value applications should consider it a hint. |
|
1156 |
* |
|
18156 | 1157 |
* <P> The tc <B>must</B> be in the range {@code 0 <= tc <= |
1158 |
* 255} or an IllegalArgumentException will be thrown. |
|
2 | 1159 |
* <p>Notes: |
1160 |
* <p>For Internet Protocol v4 the value consists of an |
|
19069 | 1161 |
* {@code integer}, the least significant 8 bits of which |
2 | 1162 |
* represent the value of the TOS octet in IP packets sent by |
1163 |
* the socket. |
|
1164 |
* RFC 1349 defines the TOS values as follows: |
|
21334 | 1165 |
* |
2 | 1166 |
* <UL> |
1167 |
* <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI> |
|
1168 |
* <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI> |
|
1169 |
* <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI> |
|
1170 |
* <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI> |
|
1171 |
* </UL> |
|
1172 |
* The last low order bit is always ignored as this |
|
1173 |
* corresponds to the MBZ (must be zero) bit. |
|
1174 |
* <p> |
|
1175 |
* Setting bits in the precedence field may result in a |
|
1176 |
* SocketException indicating that the operation is not |
|
1177 |
* permitted. |
|
1178 |
* <p> |
|
19069 | 1179 |
* for Internet Protocol v6 {@code tc} is the value that |
2 | 1180 |
* would be placed into the sin6_flowinfo field of the IP header. |
1181 |
* |
|
19069 | 1182 |
* @param tc an {@code int} value for the bitset. |
2 | 1183 |
* @throws SocketException if there is an error setting the |
1184 |
* traffic class or type-of-service |
|
1185 |
* @since 1.4 |
|
1186 |
* @see #getTrafficClass |
|
1187 |
*/ |
|
1188 |
public synchronized void setTrafficClass(int tc) throws SocketException { |
|
1189 |
if (tc < 0 || tc > 255) |
|
1190 |
throw new IllegalArgumentException("tc is not in range 0 -- 255"); |
|
1191 |
||
1192 |
if (isClosed()) |
|
1193 |
throw new SocketException("Socket is closed"); |
|
30963
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1194 |
try { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1195 |
getImpl().setOption(SocketOptions.IP_TOS, tc); |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1196 |
} catch (SocketException se) { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1197 |
// not supported if socket already connected |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1198 |
// Solaris returns error in such cases |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1199 |
if(!isConnected()) |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1200 |
throw se; |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
1201 |
} |
2 | 1202 |
} |
1203 |
||
1204 |
/** |
|
1205 |
* Gets traffic class or type-of-service in the IP datagram |
|
1206 |
* header for packets sent from this DatagramSocket. |
|
1207 |
* <p> |
|
1208 |
* As the underlying network implementation may ignore the |
|
1209 |
* traffic class or type-of-service set using {@link #setTrafficClass(int)} |
|
1210 |
* this method may return a different value than was previously |
|
1211 |
* set using the {@link #setTrafficClass(int)} method on this |
|
1212 |
* DatagramSocket. |
|
1213 |
* |
|
1214 |
* @return the traffic class or type-of-service already set |
|
1215 |
* @throws SocketException if there is an error obtaining the |
|
1216 |
* traffic class or type-of-service value. |
|
1217 |
* @since 1.4 |
|
1218 |
* @see #setTrafficClass(int) |
|
1219 |
*/ |
|
1220 |
public synchronized int getTrafficClass() throws SocketException { |
|
1221 |
if (isClosed()) |
|
1222 |
throw new SocketException("Socket is closed"); |
|
1223 |
return ((Integer)(getImpl().getOption(SocketOptions.IP_TOS))).intValue(); |
|
1224 |
} |
|
1225 |
||
1226 |
/** |
|
1227 |
* Closes this datagram socket. |
|
1228 |
* <p> |
|
1229 |
* Any thread currently blocked in {@link #receive} upon this socket |
|
1230 |
* will throw a {@link SocketException}. |
|
1231 |
* |
|
1232 |
* <p> If this socket has an associated channel then the channel is closed |
|
1233 |
* as well. |
|
1234 |
* |
|
1235 |
* @revised 1.4 |
|
1236 |
* @spec JSR-51 |
|
1237 |
*/ |
|
1238 |
public void close() { |
|
1239 |
synchronized(closeLock) { |
|
1240 |
if (isClosed()) |
|
1241 |
return; |
|
1242 |
impl.close(); |
|
1243 |
closed = true; |
|
1244 |
} |
|
1245 |
} |
|
1246 |
||
1247 |
/** |
|
1248 |
* Returns whether the socket is closed or not. |
|
1249 |
* |
|
1250 |
* @return true if the socket has been closed |
|
1251 |
* @since 1.4 |
|
1252 |
*/ |
|
1253 |
public boolean isClosed() { |
|
1254 |
synchronized(closeLock) { |
|
1255 |
return closed; |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
/** |
|
1260 |
* Returns the unique {@link java.nio.channels.DatagramChannel} object |
|
1261 |
* associated with this datagram socket, if any. |
|
1262 |
* |
|
1263 |
* <p> A datagram socket will have a channel if, and only if, the channel |
|
1264 |
* itself was created via the {@link java.nio.channels.DatagramChannel#open |
|
1265 |
* DatagramChannel.open} method. |
|
1266 |
* |
|
1267 |
* @return the datagram channel associated with this datagram socket, |
|
19069 | 1268 |
* or {@code null} if this socket was not created for a channel |
2 | 1269 |
* |
1270 |
* @since 1.4 |
|
1271 |
* @spec JSR-51 |
|
1272 |
*/ |
|
1273 |
public DatagramChannel getChannel() { |
|
1274 |
return null; |
|
1275 |
} |
|
1276 |
||
1277 |
/** |
|
1278 |
* User defined factory for all datagram sockets. |
|
1279 |
*/ |
|
1280 |
static DatagramSocketImplFactory factory; |
|
1281 |
||
1282 |
/** |
|
1283 |
* Sets the datagram socket implementation factory for the |
|
1284 |
* application. The factory can be specified only once. |
|
1285 |
* <p> |
|
1286 |
* When an application creates a new datagram socket, the socket |
|
19069 | 1287 |
* implementation factory's {@code createDatagramSocketImpl} method is |
2 | 1288 |
* called to create the actual datagram socket implementation. |
1289 |
* <p> |
|
19069 | 1290 |
* Passing {@code null} to the method is a no-op unless the factory |
2 | 1291 |
* was already set. |
1292 |
* |
|
1293 |
* <p>If there is a security manager, this method first calls |
|
19069 | 1294 |
* the security manager's {@code checkSetFactory} method |
2 | 1295 |
* to ensure the operation is allowed. |
1296 |
* This could result in a SecurityException. |
|
1297 |
* |
|
1298 |
* @param fac the desired factory. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1299 |
* @throws IOException if an I/O error occurs when setting the |
2 | 1300 |
* datagram socket factory. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1301 |
* @throws SocketException if the factory is already defined. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55081
diff
changeset
|
1302 |
* @throws SecurityException if a security manager exists and its |
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
21428
diff
changeset
|
1303 |
* {@code checkSetFactory} method doesn't allow the operation. |
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
21428
diff
changeset
|
1304 |
* @see java.net.DatagramSocketImplFactory#createDatagramSocketImpl() |
2 | 1305 |
* @see SecurityManager#checkSetFactory |
1306 |
* @since 1.3 |
|
1307 |
*/ |
|
1308 |
public static synchronized void |
|
1309 |
setDatagramSocketImplFactory(DatagramSocketImplFactory fac) |
|
1310 |
throws IOException |
|
1311 |
{ |
|
1312 |
if (factory != null) { |
|
1313 |
throw new SocketException("factory already defined"); |
|
1314 |
} |
|
1315 |
SecurityManager security = System.getSecurityManager(); |
|
1316 |
if (security != null) { |
|
1317 |
security.checkSetFactory(); |
|
1318 |
} |
|
1319 |
factory = fac; |
|
1320 |
} |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1321 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1322 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1323 |
* Sets the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1324 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27724
diff
changeset
|
1325 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1326 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1327 |
* @param value The value of the socket option. A value of {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1328 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1329 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1330 |
* @return this DatagramSocket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1331 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1332 |
* @throws UnsupportedOperationException if the datagram socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1333 |
* does not support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1334 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1335 |
* @throws IllegalArgumentException if the value is not valid for |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1336 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1337 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1338 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1339 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1340 |
* @throws SecurityException if a security manager is set and if the socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1341 |
* option requires a security permission and if the caller does |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1342 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1343 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1344 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1345 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1346 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1347 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1348 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1349 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1350 |
public <T> DatagramSocket setOption(SocketOption<T> name, T value) |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1351 |
throws IOException |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1352 |
{ |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1353 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1354 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1355 |
throw new SocketException("Socket is closed"); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1356 |
getImpl().setOption(name, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1357 |
return this; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1358 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1359 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1360 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1361 |
* Returns the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1362 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
27724
diff
changeset
|
1363 |
* @param <T> The type of the socket option value |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1364 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1365 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1366 |
* @return The value of the socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1367 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1368 |
* @throws UnsupportedOperationException if the datagram socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1369 |
* does not support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1370 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1371 |
* @throws IOException if an I/O error occurs, or if the socket is closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1372 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1373 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1374 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1375 |
* @throws SecurityException if a security manager is set and if the socket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1376 |
* option requires a security permission and if the caller does |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1377 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1378 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1379 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1380 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1381 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1382 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1383 |
public <T> T getOption(SocketOption<T> name) throws IOException { |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1384 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1385 |
if (isClosed()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
52499
diff
changeset
|
1386 |
throw new SocketException("Socket is closed"); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1387 |
return getImpl().getOption(name); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1388 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1389 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1390 |
private static Set<SocketOption<?>> options; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1391 |
private static boolean optionsSet = false; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1392 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1393 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1394 |
* Returns a set of the socket options supported by this socket. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1395 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1396 |
* This method will continue to return the set of options even after |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1397 |
* the socket has been closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1398 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1399 |
* @return A set of the socket options supported by this socket. This set |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1400 |
* may be empty if the socket's DatagramSocketImpl cannot be created. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1401 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
30963
diff
changeset
|
1402 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1403 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1404 |
public Set<SocketOption<?>> supportedOptions() { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1405 |
synchronized(DatagramSocket.class) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1406 |
if (optionsSet) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1407 |
return options; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1408 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1409 |
try { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1410 |
DatagramSocketImpl impl = getImpl(); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1411 |
options = Collections.unmodifiableSet(impl.supportedOptions()); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1412 |
} catch (IOException e) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1413 |
options = Collections.emptySet(); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1414 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1415 |
optionsSet = true; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1416 |
return options; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1417 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1418 |
} |
2 | 1419 |
} |