author | michaelm |
Mon, 11 Feb 2019 15:17:31 +0000 | |
branch | niosocketimpl-branch |
changeset 57176 | 726630bc6a4c |
parent 57175 | 7eb6cdd1204a |
child 57178 | 738431fa8fb2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1995, 2018, 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 |
||
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52371
diff
changeset
|
28 |
import jdk.internal.access.JavaNetSocketAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
52371
diff
changeset
|
29 |
import jdk.internal.access.SharedSecrets; |
40696
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
30 |
|
2 | 31 |
import java.io.FileDescriptor; |
32 |
import java.io.IOException; |
|
40696
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
33 |
import java.lang.reflect.Constructor; |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
34 |
import java.lang.reflect.InvocationTargetException; |
2 | 35 |
import java.nio.channels.ServerSocketChannel; |
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedExceptionAction; |
|
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
38 |
import java.util.Set; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
39 |
import java.util.Collections; |
57174 | 40 |
|
57176 | 41 |
import sun.net.PlatformSocketImpl; |
2 | 42 |
|
43 |
/** |
|
44 |
* This class implements server sockets. A server socket waits for |
|
45 |
* requests to come in over the network. It performs some operation |
|
46 |
* based on that request, and then possibly returns a result to the requester. |
|
47 |
* <p> |
|
48 |
* The actual work of the server socket is performed by an instance |
|
19069 | 49 |
* of the {@code SocketImpl} class. An application can |
2 | 50 |
* change the socket factory that creates the socket |
51 |
* implementation to configure itself to create sockets |
|
52 |
* appropriate to the local firewall. |
|
53 |
* |
|
54 |
* @author unascribed |
|
55 |
* @see java.net.SocketImpl |
|
56 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
57 |
* @see java.nio.channels.ServerSocketChannel |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23879
diff
changeset
|
58 |
* @since 1.0 |
2 | 59 |
*/ |
60 |
public |
|
61 |
class ServerSocket implements java.io.Closeable { |
|
62 |
/** |
|
63 |
* Various states of this socket. |
|
64 |
*/ |
|
65 |
private boolean created = false; |
|
66 |
private boolean bound = false; |
|
67 |
private boolean closed = false; |
|
68 |
private Object closeLock = new Object(); |
|
69 |
||
70 |
/** |
|
71 |
* The implementation of this Socket. |
|
72 |
*/ |
|
73 |
private SocketImpl impl; |
|
74 |
||
75 |
/** |
|
76 |
* Are we using an older SocketImpl? |
|
77 |
*/ |
|
78 |
private boolean oldImpl = false; |
|
79 |
||
80 |
/** |
|
52371
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
81 |
* Creates a server socket with a user-specified {@code SocketImpl}. |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
82 |
* |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
83 |
* @param impl an instance of a SocketImpl to use on the ServerSocket. |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
84 |
* |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
85 |
* @throws NullPointerException if impl is {@code null}. |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
86 |
* |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
87 |
* @since 12 |
6525 | 88 |
*/ |
52371
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
89 |
protected ServerSocket(SocketImpl impl) { |
6525 | 90 |
this.impl = impl; |
91 |
impl.setServerSocket(this); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
2 | 95 |
* Creates an unbound server socket. |
96 |
* |
|
97 |
* @exception IOException IO error when opening the socket. |
|
98 |
* @revised 1.4 |
|
99 |
*/ |
|
100 |
public ServerSocket() throws IOException { |
|
101 |
setImpl(); |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Creates a server socket, bound to the specified port. A port number |
|
19069 | 106 |
* of {@code 0} means that the port number is automatically |
2 | 107 |
* allocated, typically from an ephemeral port range. This port |
108 |
* number can then be retrieved by calling {@link #getLocalPort getLocalPort}. |
|
109 |
* <p> |
|
110 |
* The maximum queue length for incoming connection indications (a |
|
19069 | 111 |
* request to connect) is set to {@code 50}. If a connection |
2 | 112 |
* indication arrives when the queue is full, the connection is refused. |
113 |
* <p> |
|
114 |
* If the application has specified a server socket factory, that |
|
19069 | 115 |
* factory's {@code createSocketImpl} method is called to create |
2 | 116 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
117 |
* <p> |
|
118 |
* If there is a security manager, |
|
19069 | 119 |
* its {@code checkListen} method is called |
120 |
* with the {@code port} argument |
|
2 | 121 |
* as its argument to ensure the operation is allowed. |
122 |
* This could result in a SecurityException. |
|
123 |
* |
|
124 |
* |
|
19069 | 125 |
* @param port the port number, or {@code 0} to use a port |
2 | 126 |
* number that is automatically allocated. |
127 |
* |
|
128 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
129 |
* @exception SecurityException |
|
19069 | 130 |
* if a security manager exists and its {@code checkListen} |
2 | 131 |
* method doesn't allow the operation. |
132 |
* @exception IllegalArgumentException if the port parameter is outside |
|
133 |
* the specified range of valid port values, which is between |
|
134 |
* 0 and 65535, inclusive. |
|
135 |
* |
|
136 |
* @see java.net.SocketImpl |
|
137 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
138 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
139 |
* @see SecurityManager#checkListen |
|
140 |
*/ |
|
141 |
public ServerSocket(int port) throws IOException { |
|
142 |
this(port, 50, null); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Creates a server socket and binds it to the specified local port |
|
147 |
* number, with the specified backlog. |
|
19069 | 148 |
* A port number of {@code 0} means that the port number is |
2 | 149 |
* automatically allocated, typically from an ephemeral port range. |
150 |
* This port number can then be retrieved by calling |
|
151 |
* {@link #getLocalPort getLocalPort}. |
|
152 |
* <p> |
|
153 |
* The maximum queue length for incoming connection indications (a |
|
19069 | 154 |
* request to connect) is set to the {@code backlog} parameter. If |
2 | 155 |
* a connection indication arrives when the queue is full, the |
156 |
* connection is refused. |
|
157 |
* <p> |
|
158 |
* If the application has specified a server socket factory, that |
|
19069 | 159 |
* factory's {@code createSocketImpl} method is called to create |
2 | 160 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
161 |
* <p> |
|
162 |
* If there is a security manager, |
|
19069 | 163 |
* its {@code checkListen} method is called |
164 |
* with the {@code port} argument |
|
2 | 165 |
* as its argument to ensure the operation is allowed. |
166 |
* This could result in a SecurityException. |
|
167 |
* |
|
19069 | 168 |
* The {@code backlog} argument is the requested maximum number of |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
169 |
* pending connections on the socket. Its exact semantics are implementation |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
170 |
* specific. In particular, an implementation may impose a maximum length |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
171 |
* or may choose to ignore the parameter altogether. The value provided |
19069 | 172 |
* should be greater than {@code 0}. If it is less than or equal to |
173 |
* {@code 0}, then an implementation specific default will be used. |
|
2 | 174 |
* |
19069 | 175 |
* @param port the port number, or {@code 0} to use a port |
2 | 176 |
* number that is automatically allocated. |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
177 |
* @param backlog requested maximum length of the queue of incoming |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
178 |
* connections. |
2 | 179 |
* |
180 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
181 |
* @exception SecurityException |
|
19069 | 182 |
* if a security manager exists and its {@code checkListen} |
2 | 183 |
* method doesn't allow the operation. |
184 |
* @exception IllegalArgumentException if the port parameter is outside |
|
185 |
* the specified range of valid port values, which is between |
|
186 |
* 0 and 65535, inclusive. |
|
187 |
* |
|
188 |
* @see java.net.SocketImpl |
|
189 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
190 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
191 |
* @see SecurityManager#checkListen |
|
192 |
*/ |
|
193 |
public ServerSocket(int port, int backlog) throws IOException { |
|
194 |
this(port, backlog, null); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Create a server with the specified port, listen backlog, and |
|
199 |
* local IP address to bind to. The <i>bindAddr</i> argument |
|
200 |
* can be used on a multi-homed host for a ServerSocket that |
|
201 |
* will only accept connect requests to one of its addresses. |
|
202 |
* If <i>bindAddr</i> is null, it will default accepting |
|
203 |
* connections on any/all local addresses. |
|
204 |
* The port must be between 0 and 65535, inclusive. |
|
19069 | 205 |
* A port number of {@code 0} means that the port number is |
2 | 206 |
* automatically allocated, typically from an ephemeral port range. |
207 |
* This port number can then be retrieved by calling |
|
208 |
* {@link #getLocalPort getLocalPort}. |
|
209 |
* |
|
210 |
* <P>If there is a security manager, this method |
|
19069 | 211 |
* calls its {@code checkListen} method |
212 |
* with the {@code port} argument |
|
2 | 213 |
* as its argument to ensure the operation is allowed. |
214 |
* This could result in a SecurityException. |
|
215 |
* |
|
19069 | 216 |
* The {@code backlog} argument is the requested maximum number of |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
217 |
* pending connections on the socket. Its exact semantics are implementation |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
218 |
* specific. In particular, an implementation may impose a maximum length |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
219 |
* or may choose to ignore the parameter altogether. The value provided |
19069 | 220 |
* should be greater than {@code 0}. If it is less than or equal to |
221 |
* {@code 0}, then an implementation specific default will be used. |
|
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
21278
diff
changeset
|
222 |
* |
19069 | 223 |
* @param port the port number, or {@code 0} to use a port |
2 | 224 |
* number that is automatically allocated. |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
225 |
* @param backlog requested maximum length of the queue of incoming |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
226 |
* connections. |
2 | 227 |
* @param bindAddr the local InetAddress the server will bind to |
228 |
* |
|
229 |
* @throws SecurityException if a security manager exists and |
|
19069 | 230 |
* its {@code checkListen} method doesn't allow the operation. |
2 | 231 |
* |
232 |
* @throws IOException if an I/O error occurs when opening the socket. |
|
233 |
* @exception IllegalArgumentException if the port parameter is outside |
|
234 |
* the specified range of valid port values, which is between |
|
235 |
* 0 and 65535, inclusive. |
|
236 |
* |
|
237 |
* @see SocketOptions |
|
238 |
* @see SocketImpl |
|
239 |
* @see SecurityManager#checkListen |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23879
diff
changeset
|
240 |
* @since 1.1 |
2 | 241 |
*/ |
242 |
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { |
|
243 |
setImpl(); |
|
244 |
if (port < 0 || port > 0xFFFF) |
|
245 |
throw new IllegalArgumentException( |
|
246 |
"Port value out of range: " + port); |
|
247 |
if (backlog < 1) |
|
248 |
backlog = 50; |
|
249 |
try { |
|
250 |
bind(new InetSocketAddress(bindAddr, port), backlog); |
|
251 |
} catch(SecurityException e) { |
|
252 |
close(); |
|
253 |
throw e; |
|
254 |
} catch(IOException e) { |
|
255 |
close(); |
|
256 |
throw e; |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
/** |
|
19069 | 261 |
* Get the {@code SocketImpl} attached to this socket, creating |
2 | 262 |
* it if necessary. |
263 |
* |
|
19069 | 264 |
* @return the {@code SocketImpl} attached to that ServerSocket. |
2 | 265 |
* @throws SocketException if creation fails. |
266 |
* @since 1.4 |
|
267 |
*/ |
|
268 |
SocketImpl getImpl() throws SocketException { |
|
269 |
if (!created) |
|
270 |
createImpl(); |
|
271 |
return impl; |
|
272 |
} |
|
273 |
||
274 |
private void checkOldImpl() { |
|
275 |
if (impl == null) |
|
276 |
return; |
|
277 |
// SocketImpl.connect() is a protected method, therefore we need to use |
|
278 |
// getDeclaredMethod, therefore we need permission to access the member |
|
279 |
try { |
|
51 | 280 |
AccessController.doPrivileged( |
281 |
new PrivilegedExceptionAction<Void>() { |
|
282 |
public Void run() throws NoSuchMethodException { |
|
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
283 |
impl.getClass().getDeclaredMethod("connect", |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
284 |
SocketAddress.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
285 |
int.class); |
2 | 286 |
return null; |
287 |
} |
|
288 |
}); |
|
289 |
} catch (java.security.PrivilegedActionException e) { |
|
290 |
oldImpl = true; |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
private void setImpl() { |
|
295 |
if (factory != null) { |
|
296 |
impl = factory.createSocketImpl(); |
|
297 |
checkOldImpl(); |
|
298 |
} else { |
|
57173 | 299 |
impl = SocketImpl.createSocketImpl(true); |
2 | 300 |
} |
301 |
if (impl != null) |
|
302 |
impl.setServerSocket(this); |
|
303 |
} |
|
304 |
||
305 |
/** |
|
306 |
* Creates the socket implementation. |
|
307 |
* |
|
308 |
* @throws IOException if creation fails |
|
309 |
* @since 1.4 |
|
310 |
*/ |
|
311 |
void createImpl() throws SocketException { |
|
312 |
if (impl == null) |
|
313 |
setImpl(); |
|
314 |
try { |
|
315 |
impl.create(true); |
|
316 |
created = true; |
|
317 |
} catch (IOException e) { |
|
318 |
throw new SocketException(e.getMessage()); |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* |
|
19069 | 324 |
* Binds the {@code ServerSocket} to a specific address |
2 | 325 |
* (IP address and port number). |
326 |
* <p> |
|
19069 | 327 |
* If the address is {@code null}, then the system will pick up |
2 | 328 |
* 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:
21278
diff
changeset
|
329 |
* |
18156 | 330 |
* @param endpoint The IP address and port number to bind to. |
2 | 331 |
* @throws IOException if the bind operation fails, or if the socket |
332 |
* is already bound. |
|
19069 | 333 |
* @throws SecurityException if a {@code SecurityManager} is present and |
334 |
* its {@code checkListen} method doesn't allow the operation. |
|
2 | 335 |
* @throws IllegalArgumentException if endpoint is a |
336 |
* SocketAddress subclass not supported by this socket |
|
337 |
* @since 1.4 |
|
338 |
*/ |
|
339 |
public void bind(SocketAddress endpoint) throws IOException { |
|
340 |
bind(endpoint, 50); |
|
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* |
|
19069 | 345 |
* Binds the {@code ServerSocket} to a specific address |
2 | 346 |
* (IP address and port number). |
347 |
* <p> |
|
19069 | 348 |
* If the address is {@code null}, then the system will pick up |
2 | 349 |
* an ephemeral port and a valid local address to bind the socket. |
350 |
* <P> |
|
19069 | 351 |
* The {@code backlog} argument is the requested maximum number of |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
352 |
* pending connections on the socket. Its exact semantics are implementation |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
353 |
* specific. In particular, an implementation may impose a maximum length |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
354 |
* or may choose to ignore the parameter altogether. The value provided |
19069 | 355 |
* should be greater than {@code 0}. If it is less than or equal to |
356 |
* {@code 0}, then an implementation specific default will be used. |
|
18156 | 357 |
* @param endpoint The IP address and port number to bind to. |
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
358 |
* @param backlog requested maximum length of the queue of |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
359 |
* incoming connections. |
2 | 360 |
* @throws IOException if the bind operation fails, or if the socket |
361 |
* is already bound. |
|
19069 | 362 |
* @throws SecurityException if a {@code SecurityManager} is present and |
363 |
* its {@code checkListen} method doesn't allow the operation. |
|
2 | 364 |
* @throws IllegalArgumentException if endpoint is a |
365 |
* SocketAddress subclass not supported by this socket |
|
366 |
* @since 1.4 |
|
367 |
*/ |
|
368 |
public void bind(SocketAddress endpoint, int backlog) throws IOException { |
|
369 |
if (isClosed()) |
|
370 |
throw new SocketException("Socket is closed"); |
|
371 |
if (!oldImpl && isBound()) |
|
372 |
throw new SocketException("Already bound"); |
|
373 |
if (endpoint == null) |
|
374 |
endpoint = new InetSocketAddress(0); |
|
375 |
if (!(endpoint instanceof InetSocketAddress)) |
|
376 |
throw new IllegalArgumentException("Unsupported address type"); |
|
377 |
InetSocketAddress epoint = (InetSocketAddress) endpoint; |
|
378 |
if (epoint.isUnresolved()) |
|
379 |
throw new SocketException("Unresolved address"); |
|
380 |
if (backlog < 1) |
|
381 |
backlog = 50; |
|
382 |
try { |
|
383 |
SecurityManager security = System.getSecurityManager(); |
|
384 |
if (security != null) |
|
385 |
security.checkListen(epoint.getPort()); |
|
386 |
getImpl().bind(epoint.getAddress(), epoint.getPort()); |
|
387 |
getImpl().listen(backlog); |
|
388 |
bound = true; |
|
389 |
} catch(SecurityException e) { |
|
390 |
bound = false; |
|
391 |
throw e; |
|
392 |
} catch(IOException e) { |
|
393 |
bound = false; |
|
394 |
throw e; |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Returns the local address of this server socket. |
|
400 |
* <p> |
|
401 |
* If the socket was bound prior to being {@link #close closed}, |
|
402 |
* then this method will continue to return the local address |
|
403 |
* after the socket is closed. |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
404 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
405 |
* If there is a security manager set, its {@code checkConnect} method is |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
406 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
407 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
408 |
* the {@link InetAddress#getLoopbackAddress loopback} address is returned. |
2 | 409 |
* |
410 |
* @return the address to which this socket is bound, |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
411 |
* or the loopback address if denied by the security manager, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
412 |
* or {@code null} if the socket is unbound. |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
413 |
* |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
414 |
* @see SecurityManager#checkConnect |
2 | 415 |
*/ |
416 |
public InetAddress getInetAddress() { |
|
417 |
if (!isBound()) |
|
418 |
return null; |
|
419 |
try { |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
420 |
InetAddress in = getImpl().getInetAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
421 |
SecurityManager sm = System.getSecurityManager(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
422 |
if (sm != null) |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
423 |
sm.checkConnect(in.getHostAddress(), -1); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
424 |
return in; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
425 |
} catch (SecurityException e) { |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
426 |
return InetAddress.getLoopbackAddress(); |
2 | 427 |
} catch (SocketException e) { |
428 |
// nothing |
|
429 |
// If we're bound, the impl has been created |
|
430 |
// so we shouldn't get here |
|
431 |
} |
|
432 |
return null; |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Returns the port number on which this socket is listening. |
|
437 |
* <p> |
|
438 |
* If the socket was bound prior to being {@link #close closed}, |
|
439 |
* then this method will continue to return the port number |
|
440 |
* after the socket is closed. |
|
441 |
* |
|
442 |
* @return the port number to which this socket is listening or |
|
443 |
* -1 if the socket is not bound yet. |
|
444 |
*/ |
|
445 |
public int getLocalPort() { |
|
446 |
if (!isBound()) |
|
447 |
return -1; |
|
448 |
try { |
|
449 |
return getImpl().getLocalPort(); |
|
450 |
} catch (SocketException e) { |
|
451 |
// nothing |
|
452 |
// If we're bound, the impl has been created |
|
453 |
// so we shouldn't get here |
|
454 |
} |
|
455 |
return -1; |
|
456 |
} |
|
457 |
||
458 |
/** |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
459 |
* Returns the address of the endpoint this socket is bound to. |
2 | 460 |
* <p> |
461 |
* If the socket was bound prior to being {@link #close closed}, |
|
462 |
* then this method will continue to return the address of the endpoint |
|
463 |
* after the socket is closed. |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
464 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
465 |
* If there is a security manager set, its {@code checkConnect} method is |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
466 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
467 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
468 |
* a {@code SocketAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
469 |
* {@link InetAddress#getLoopbackAddress loopback} address and the local |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
470 |
* port to which the socket is bound is returned. |
2 | 471 |
* |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
472 |
* @return a {@code SocketAddress} representing the local endpoint of |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
473 |
* this socket, or a {@code SocketAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
474 |
* loopback address if denied by the security manager, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
475 |
* or {@code null} if the socket is not bound yet. |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
476 |
* |
2 | 477 |
* @see #getInetAddress() |
478 |
* @see #getLocalPort() |
|
479 |
* @see #bind(SocketAddress) |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
480 |
* @see SecurityManager#checkConnect |
2 | 481 |
* @since 1.4 |
482 |
*/ |
|
483 |
||
484 |
public SocketAddress getLocalSocketAddress() { |
|
485 |
if (!isBound()) |
|
486 |
return null; |
|
487 |
return new InetSocketAddress(getInetAddress(), getLocalPort()); |
|
488 |
} |
|
489 |
||
490 |
/** |
|
491 |
* Listens for a connection to be made to this socket and accepts |
|
492 |
* it. The method blocks until a connection is made. |
|
493 |
* |
|
19069 | 494 |
* <p>A new Socket {@code s} is created and, if there |
2 | 495 |
* is a security manager, |
19069 | 496 |
* the security manager's {@code checkAccept} method is called |
497 |
* with {@code s.getInetAddress().getHostAddress()} and |
|
498 |
* {@code s.getPort()} |
|
2 | 499 |
* as its arguments to ensure the operation is allowed. |
500 |
* This could result in a SecurityException. |
|
501 |
* |
|
502 |
* @exception IOException if an I/O error occurs when waiting for a |
|
503 |
* connection. |
|
504 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 505 |
* {@code checkAccept} method doesn't allow the operation. |
2 | 506 |
* @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and |
507 |
* the timeout has been reached. |
|
508 |
* @exception java.nio.channels.IllegalBlockingModeException |
|
509 |
* if this socket has an associated channel, the channel is in |
|
510 |
* non-blocking mode, and there is no connection ready to be |
|
511 |
* accepted |
|
512 |
* |
|
513 |
* @return the new Socket |
|
514 |
* @see SecurityManager#checkAccept |
|
515 |
* @revised 1.4 |
|
516 |
* @spec JSR-51 |
|
517 |
*/ |
|
518 |
public Socket accept() throws IOException { |
|
519 |
if (isClosed()) |
|
520 |
throw new SocketException("Socket is closed"); |
|
521 |
if (!isBound()) |
|
522 |
throw new SocketException("Socket is not bound yet"); |
|
523 |
Socket s = new Socket((SocketImpl) null); |
|
524 |
implAccept(s); |
|
525 |
return s; |
|
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* Subclasses of ServerSocket use this method to override accept() |
|
530 |
* to return their own subclass of socket. So a FooServerSocket |
|
531 |
* will typically hand this method an <i>empty</i> FooSocket. On |
|
532 |
* return from implAccept the FooSocket will be connected to a client. |
|
533 |
* |
|
534 |
* @param s the Socket |
|
535 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
536 |
* if this socket has an associated channel, |
|
537 |
* and the channel is in non-blocking mode |
|
538 |
* @throws IOException if an I/O error occurs when waiting |
|
539 |
* for a connection. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23879
diff
changeset
|
540 |
* @since 1.1 |
2 | 541 |
* @revised 1.4 |
542 |
* @spec JSR-51 |
|
543 |
*/ |
|
544 |
protected final void implAccept(Socket s) throws IOException { |
|
57110 | 545 |
SocketImpl impl = getImpl(); |
546 |
SocketImpl si = s.impl; |
|
547 |
||
548 |
// Socket does not have a SocketImpl |
|
549 |
if (si == null) { |
|
550 |
// create a SocketImpl and accept the connection |
|
551 |
si = Socket.createImpl(); |
|
552 |
impl.accept(si); |
|
553 |
try { |
|
57174 | 554 |
// a custom impl has accepted the connection with a trusted SocketImpl |
57176 | 555 |
if (!(impl instanceof PlatformSocketImpl) && (si instanceof PlatformSocketImpl)) { |
556 |
((PlatformSocketImpl) si).postCustomAccept(); |
|
57110 | 557 |
} |
558 |
} finally { |
|
559 |
securityCheckAccept(si); // closes si if permission check fails |
|
2 | 560 |
} |
57110 | 561 |
|
562 |
// bind Socket to the SocketImpl and update socket state |
|
563 |
s.setImpl(si); |
|
564 |
s.postAccept(); |
|
565 |
return; |
|
566 |
} |
|
567 |
||
57175
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
568 |
// Socket has a SOCKS or HTTP SocketImpl |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
569 |
if (si instanceof DelegatingSocketImpl) { |
57172 | 570 |
si = ((DelegatingSocketImpl) si).delegate(); |
57176 | 571 |
assert si instanceof PlatformSocketImpl; |
57175
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
572 |
} |
57110 | 573 |
|
57174 | 574 |
// ServerSocket or Socket is using a trusted SocketImpl |
57176 | 575 |
if (impl instanceof PlatformSocketImpl || si instanceof PlatformSocketImpl) { |
57174 | 576 |
// accept connection with new SocketImpl |
57176 | 577 |
var nsi = (impl instanceof PlatformSocketImpl) |
578 |
? ((PlatformSocketImpl) impl).newInstance(false) |
|
579 |
: ((PlatformSocketImpl) si).newInstance(false); |
|
57110 | 580 |
impl.accept(nsi); |
57175
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
581 |
try { |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
582 |
// a custom impl has accepted the connection with a trusted SocketImpl |
57176 | 583 |
if (!(impl instanceof PlatformSocketImpl)) { |
57175
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
584 |
nsi.postCustomAccept(); |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
585 |
} |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
586 |
} finally { |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
587 |
securityCheckAccept(nsi); // closes nsi if permission check fails |
7eb6cdd1204a
More cleanup. Also fix up jshell tests that depend on exception messages
alanb
parents:
57174
diff
changeset
|
588 |
} |
2 | 589 |
|
57110 | 590 |
// copy state to the existing SocketImpl and update socket state |
57171 | 591 |
nsi.copyTo(si); |
592 |
s.postAccept(); |
|
593 |
return; |
|
594 |
} |
|
595 |
||
57110 | 596 |
// ServerSocket and Socket bound to custom SocketImpls |
597 |
s.impl = null; // break connection to impl |
|
598 |
boolean completed = false; |
|
599 |
try { |
|
600 |
si.reset(); |
|
57171 | 601 |
si.fd = new FileDescriptor(); |
602 |
si.address = new InetAddress(); |
|
57110 | 603 |
impl.accept(si); |
604 |
securityCheckAccept(si); // closes si if permission check fails |
|
605 |
completed = true; |
|
606 |
} finally { |
|
607 |
if (!completed) |
|
2 | 608 |
si.reset(); |
57110 | 609 |
s.impl = si; // restore connection to impl |
2 | 610 |
} |
611 |
s.postAccept(); |
|
612 |
} |
|
613 |
||
614 |
/** |
|
57110 | 615 |
* Invokes the security manager's checkAccept method. If the permission |
616 |
* check fails then it closes the SocketImpl. |
|
617 |
*/ |
|
618 |
private void securityCheckAccept(SocketImpl si) throws IOException { |
|
619 |
SecurityManager sm = System.getSecurityManager(); |
|
620 |
if (sm != null) { |
|
621 |
try { |
|
622 |
sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort()); |
|
623 |
} catch (SecurityException se) { |
|
624 |
si.close(); |
|
625 |
throw se; |
|
626 |
} |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
/** |
|
2 | 631 |
* Closes this socket. |
632 |
* |
|
633 |
* Any thread currently blocked in {@link #accept()} will throw |
|
634 |
* a {@link SocketException}. |
|
635 |
* |
|
636 |
* <p> If this socket has an associated channel then the channel is closed |
|
637 |
* as well. |
|
638 |
* |
|
639 |
* @exception IOException if an I/O error occurs when closing the socket. |
|
640 |
* @revised 1.4 |
|
641 |
* @spec JSR-51 |
|
642 |
*/ |
|
643 |
public void close() throws IOException { |
|
644 |
synchronized(closeLock) { |
|
645 |
if (isClosed()) |
|
646 |
return; |
|
647 |
if (created) |
|
648 |
impl.close(); |
|
649 |
closed = true; |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Returns the unique {@link java.nio.channels.ServerSocketChannel} object |
|
655 |
* associated with this socket, if any. |
|
656 |
* |
|
657 |
* <p> A server socket will have a channel if, and only if, the channel |
|
658 |
* itself was created via the {@link |
|
659 |
* java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open} |
|
660 |
* method. |
|
661 |
* |
|
662 |
* @return the server-socket channel associated with this socket, |
|
19069 | 663 |
* or {@code null} if this socket was not created |
2 | 664 |
* for a channel |
665 |
* |
|
666 |
* @since 1.4 |
|
667 |
* @spec JSR-51 |
|
668 |
*/ |
|
669 |
public ServerSocketChannel getChannel() { |
|
670 |
return null; |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* Returns the binding state of the ServerSocket. |
|
675 |
* |
|
21278 | 676 |
* @return true if the ServerSocket successfully bound to an address |
2 | 677 |
* @since 1.4 |
678 |
*/ |
|
679 |
public boolean isBound() { |
|
680 |
// Before 1.3 ServerSockets were always bound during creation |
|
681 |
return bound || oldImpl; |
|
682 |
} |
|
683 |
||
684 |
/** |
|
685 |
* Returns the closed state of the ServerSocket. |
|
686 |
* |
|
687 |
* @return true if the socket has been closed |
|
688 |
* @since 1.4 |
|
689 |
*/ |
|
690 |
public boolean isClosed() { |
|
691 |
synchronized(closeLock) { |
|
692 |
return closed; |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
697 |
* Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
698 |
* specified timeout, in milliseconds. With this option set to a non-zero |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
699 |
* timeout, a call to accept() for this ServerSocket |
2 | 700 |
* will block for only this amount of time. If the timeout expires, |
701 |
* a <B>java.net.SocketTimeoutException</B> is raised, though the |
|
702 |
* ServerSocket is still valid. The option <B>must</B> be enabled |
|
703 |
* prior to entering the blocking operation to have effect. The |
|
18156 | 704 |
* timeout must be {@code > 0}. |
2 | 705 |
* A timeout of zero is interpreted as an infinite timeout. |
706 |
* @param timeout the specified timeout, in milliseconds |
|
707 |
* @exception SocketException if there is an error in |
|
708 |
* the underlying protocol, such as a TCP error. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23879
diff
changeset
|
709 |
* @since 1.1 |
2 | 710 |
* @see #getSoTimeout() |
711 |
*/ |
|
712 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
713 |
if (isClosed()) |
|
714 |
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
|
715 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout); |
2 | 716 |
} |
717 |
||
718 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
719 |
* Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}. |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
720 |
* 0 returns implies that the option is disabled (i.e., timeout of infinity). |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
721 |
* @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value |
2 | 722 |
* @exception IOException if an I/O error occurs |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23879
diff
changeset
|
723 |
* @since 1.1 |
2 | 724 |
* @see #setSoTimeout(int) |
725 |
*/ |
|
726 |
public synchronized int getSoTimeout() throws IOException { |
|
727 |
if (isClosed()) |
|
728 |
throw new SocketException("Socket is closed"); |
|
729 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
730 |
/* extra type safety */ |
|
731 |
if (o instanceof Integer) { |
|
732 |
return ((Integer) o).intValue(); |
|
733 |
} else { |
|
734 |
return 0; |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
739 |
* Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
740 |
* socket option. |
2 | 741 |
* <p> |
742 |
* When a TCP connection is closed the connection may remain |
|
743 |
* in a timeout state for a period of time after the connection |
|
19069 | 744 |
* is closed (typically known as the {@code TIME_WAIT} state |
745 |
* or {@code 2MSL} wait state). |
|
2 | 746 |
* For applications using a well known socket address or port |
747 |
* it may not be possible to bind a socket to the required |
|
19069 | 748 |
* {@code SocketAddress} if there is a connection in the |
2 | 749 |
* timeout state involving the socket address or port. |
750 |
* <p> |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
751 |
* Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} prior to |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
752 |
* binding the socket using {@link #bind(SocketAddress)} allows the socket |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
753 |
* to be bound even though a previous connection is in a timeout state. |
2 | 754 |
* <p> |
19069 | 755 |
* When a {@code ServerSocket} is created the initial setting |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
756 |
* of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is not defined. |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
757 |
* Applications can use {@link #getReuseAddress()} to determine the initial |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
758 |
* setting of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}. |
2 | 759 |
* <p> |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
760 |
* The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
761 |
* enabled or disabled after a socket is bound (See {@link #isBound()}) |
2 | 762 |
* is not defined. |
763 |
* |
|
764 |
* @param on whether to enable or disable the socket option |
|
765 |
* @exception SocketException if an error occurs enabling or |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
766 |
* disabling the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
767 |
* socket option, or the socket is closed. |
2 | 768 |
* @since 1.4 |
769 |
* @see #getReuseAddress() |
|
770 |
* @see #bind(SocketAddress) |
|
771 |
* @see #isBound() |
|
772 |
* @see #isClosed() |
|
773 |
*/ |
|
774 |
public void setReuseAddress(boolean on) throws SocketException { |
|
775 |
if (isClosed()) |
|
776 |
throw new SocketException("Socket is closed"); |
|
777 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
778 |
} |
|
779 |
||
780 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
781 |
* Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
2 | 782 |
* |
19069 | 783 |
* @return a {@code boolean} indicating whether or not |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
784 |
* {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
2 | 785 |
* @exception SocketException if there is an error |
786 |
* in the underlying protocol, such as a TCP error. |
|
787 |
* @since 1.4 |
|
788 |
* @see #setReuseAddress(boolean) |
|
789 |
*/ |
|
790 |
public boolean getReuseAddress() throws SocketException { |
|
791 |
if (isClosed()) |
|
792 |
throw new SocketException("Socket is closed"); |
|
793 |
return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Returns the implementation address and implementation port of |
|
19069 | 798 |
* this socket as a {@code String}. |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
799 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
800 |
* If there is a security manager set, its {@code checkConnect} method is |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
801 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
802 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
803 |
* an {@code InetAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
804 |
* {@link InetAddress#getLoopbackAddress loopback} address is returned as |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
805 |
* the implementation address. |
2 | 806 |
* |
807 |
* @return a string representation of this socket. |
|
808 |
*/ |
|
809 |
public String toString() { |
|
810 |
if (!isBound()) |
|
811 |
return "ServerSocket[unbound]"; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
812 |
InetAddress in; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
813 |
if (System.getSecurityManager() != null) |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
814 |
in = InetAddress.getLoopbackAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
815 |
else |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
816 |
in = impl.getInetAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
817 |
return "ServerSocket[addr=" + in + |
2 | 818 |
",localport=" + impl.getLocalPort() + "]"; |
819 |
} |
|
820 |
||
821 |
void setBound() { |
|
822 |
bound = true; |
|
823 |
} |
|
824 |
||
825 |
void setCreated() { |
|
826 |
created = true; |
|
827 |
} |
|
828 |
||
829 |
/** |
|
830 |
* The factory for all server sockets. |
|
831 |
*/ |
|
832 |
private static SocketImplFactory factory = null; |
|
833 |
||
834 |
/** |
|
835 |
* Sets the server socket implementation factory for the |
|
836 |
* application. The factory can be specified only once. |
|
837 |
* <p> |
|
838 |
* When an application creates a new server socket, the socket |
|
19069 | 839 |
* implementation factory's {@code createSocketImpl} method is |
2 | 840 |
* called to create the actual socket implementation. |
841 |
* <p> |
|
19069 | 842 |
* Passing {@code null} to the method is a no-op unless the factory |
2 | 843 |
* was already set. |
844 |
* <p> |
|
845 |
* If there is a security manager, this method first calls |
|
19069 | 846 |
* the security manager's {@code checkSetFactory} method |
2 | 847 |
* to ensure the operation is allowed. |
848 |
* This could result in a SecurityException. |
|
849 |
* |
|
850 |
* @param fac the desired factory. |
|
851 |
* @exception IOException if an I/O error occurs when setting the |
|
852 |
* socket factory. |
|
853 |
* @exception SocketException if the factory has already been defined. |
|
854 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 855 |
* {@code checkSetFactory} method doesn't allow the operation. |
2 | 856 |
* @see java.net.SocketImplFactory#createSocketImpl() |
857 |
* @see SecurityManager#checkSetFactory |
|
858 |
*/ |
|
859 |
public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { |
|
860 |
if (factory != null) { |
|
861 |
throw new SocketException("factory already defined"); |
|
862 |
} |
|
863 |
SecurityManager security = System.getSecurityManager(); |
|
864 |
if (security != null) { |
|
865 |
security.checkSetFactory(); |
|
866 |
} |
|
867 |
factory = fac; |
|
868 |
} |
|
869 |
||
870 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
871 |
* Sets a default proposed value for the |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
872 |
* {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets |
19069 | 873 |
* accepted from this {@code ServerSocket}. The value actually set |
2 | 874 |
* in the accepted socket must be determined by calling |
875 |
* {@link Socket#getReceiveBufferSize()} after the socket |
|
876 |
* is returned by {@link #accept()}. |
|
877 |
* <p> |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
878 |
* The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is used both to |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
879 |
* set the size of the internal socket receive buffer, and to set the size |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
880 |
* of the TCP receive window that is advertised to the remote peer. |
2 | 881 |
* <p> |
882 |
* It is possible to change the value subsequently, by calling |
|
883 |
* {@link Socket#setReceiveBufferSize(int)}. However, if the application |
|
884 |
* wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 |
|
885 |
* then the proposed value must be set in the ServerSocket <B>before</B> |
|
886 |
* it is bound to a local address. This implies, that the ServerSocket must be |
|
887 |
* created with the no-argument constructor, then setReceiveBufferSize() must |
|
888 |
* be called and lastly the ServerSocket is bound to an address by calling bind(). |
|
889 |
* <p> |
|
890 |
* Failure to do this will not cause an error, and the buffer size may be set to the |
|
891 |
* requested value but the TCP receive window in sockets accepted from |
|
892 |
* this ServerSocket will be no larger than 64K bytes. |
|
893 |
* |
|
894 |
* @exception SocketException if there is an error |
|
895 |
* in the underlying protocol, such as a TCP error. |
|
896 |
* |
|
897 |
* @param size the size to which to set the receive buffer |
|
898 |
* size. This value must be greater than 0. |
|
899 |
* |
|
900 |
* @exception IllegalArgumentException if the |
|
901 |
* value is 0 or is negative. |
|
902 |
* |
|
903 |
* @since 1.4 |
|
904 |
* @see #getReceiveBufferSize |
|
905 |
*/ |
|
906 |
public synchronized void setReceiveBufferSize (int size) throws SocketException { |
|
907 |
if (!(size > 0)) { |
|
908 |
throw new IllegalArgumentException("negative receive size"); |
|
909 |
} |
|
910 |
if (isClosed()) |
|
911 |
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
|
912 |
getImpl().setOption(SocketOptions.SO_RCVBUF, size); |
2 | 913 |
} |
914 |
||
915 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
916 |
* Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option |
19069 | 917 |
* for this {@code ServerSocket}, that is the proposed buffer size that |
918 |
* will be used for Sockets accepted from this {@code ServerSocket}. |
|
2 | 919 |
* |
920 |
* <p>Note, the value actually set in the accepted socket is determined by |
|
921 |
* calling {@link Socket#getReceiveBufferSize()}. |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
922 |
* @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} |
19069 | 923 |
* option for this {@code Socket}. |
2 | 924 |
* @exception SocketException if there is an error |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
925 |
* in the underlying protocol, such as a TCP error. |
2 | 926 |
* @see #setReceiveBufferSize(int) |
927 |
* @since 1.4 |
|
928 |
*/ |
|
929 |
public synchronized int getReceiveBufferSize() |
|
930 |
throws SocketException{ |
|
931 |
if (isClosed()) |
|
932 |
throw new SocketException("Socket is closed"); |
|
933 |
int result = 0; |
|
934 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
935 |
if (o instanceof Integer) { |
|
936 |
result = ((Integer)o).intValue(); |
|
937 |
} |
|
938 |
return result; |
|
939 |
} |
|
940 |
||
941 |
/** |
|
942 |
* Sets performance preferences for this ServerSocket. |
|
943 |
* |
|
944 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
945 |
* may offer alternative protocols which have different performance |
|
946 |
* characteristics than TCP/IP. This method allows the application to |
|
947 |
* express its own preferences as to how these tradeoffs should be made |
|
948 |
* when the implementation chooses from the available protocols. |
|
949 |
* |
|
950 |
* <p> Performance preferences are described by three integers |
|
951 |
* whose values indicate the relative importance of short connection time, |
|
952 |
* low latency, and high bandwidth. The absolute values of the integers |
|
953 |
* are irrelevant; in order to choose a protocol the values are simply |
|
954 |
* compared, with larger values indicating stronger preferences. If the |
|
955 |
* application prefers short connection time over both low latency and high |
|
956 |
* bandwidth, for example, then it could invoke this method with the values |
|
19069 | 957 |
* {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
2 | 958 |
* latency, and low latency above short connection time, then it could |
19069 | 959 |
* invoke this method with the values {@code (0, 1, 2)}. |
2 | 960 |
* |
961 |
* <p> Invoking this method after this socket has been bound |
|
962 |
* will have no effect. This implies that in order to use this capability |
|
963 |
* requires the socket to be created with the no-argument constructor. |
|
964 |
* |
|
965 |
* @param connectionTime |
|
19069 | 966 |
* An {@code int} expressing the relative importance of a short |
2 | 967 |
* connection time |
968 |
* |
|
969 |
* @param latency |
|
19069 | 970 |
* An {@code int} expressing the relative importance of low |
2 | 971 |
* latency |
972 |
* |
|
973 |
* @param bandwidth |
|
19069 | 974 |
* An {@code int} expressing the relative importance of high |
2 | 975 |
* bandwidth |
976 |
* |
|
977 |
* @since 1.5 |
|
978 |
*/ |
|
979 |
public void setPerformancePreferences(int connectionTime, |
|
980 |
int latency, |
|
981 |
int bandwidth) |
|
982 |
{ |
|
983 |
/* Not implemented yet */ |
|
984 |
} |
|
985 |
||
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
986 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
987 |
* Sets the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
988 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
989 |
* @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
|
990 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
991 |
* @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
|
992 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
993 |
* @return this ServerSocket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
994 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
995 |
* @throws UnsupportedOperationException if the server socket does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
996 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
997 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
998 |
* @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
|
999 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1000 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1001 |
* @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
|
1002 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1003 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1004 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1005 |
* @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
|
1006 |
* 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
|
1007 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1008 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1009 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1010 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
1011 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1012 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1013 |
public <T> ServerSocket setOption(SocketOption<T> name, T value) |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1014 |
throws IOException |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1015 |
{ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1016 |
getImpl().setOption(name, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1017 |
return this; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1018 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1019 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1020 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1021 |
* Returns the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1022 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
1023 |
* @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
|
1024 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1025 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1026 |
* @return The value of the socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1027 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1028 |
* @throws UnsupportedOperationException if the server socket does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1029 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1030 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1031 |
* @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
|
1032 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1033 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1034 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1035 |
* @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
|
1036 |
* 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
|
1037 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1038 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1039 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1040 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
1041 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1042 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1043 |
public <T> T getOption(SocketOption<T> name) throws IOException { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1044 |
return getImpl().getOption(name); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1045 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1046 |
|
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1047 |
// cache of unmodifiable impl options. Possibly set racy, in impl we trust |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1048 |
private volatile Set<SocketOption<?>> options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1049 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1050 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1051 |
* Returns a set of the socket options supported by this server socket. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1052 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1053 |
* 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
|
1054 |
* the socket has been closed. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1055 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1056 |
* @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
|
1057 |
* may be empty if the socket's SocketImpl cannot be created. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1058 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
1059 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1060 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1061 |
public Set<SocketOption<?>> supportedOptions() { |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1062 |
Set<SocketOption<?>> so = options; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1063 |
if (so != null) |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1064 |
return so; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1065 |
|
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1066 |
try { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1067 |
SocketImpl impl = getImpl(); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1068 |
options = Collections.unmodifiableSet(impl.supportedOptions()); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1069 |
} catch (IOException e) { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1070 |
options = Collections.emptySet(); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1071 |
} |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1072 |
return options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1073 |
} |
40696
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1074 |
|
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1075 |
static { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1076 |
SharedSecrets.setJavaNetSocketAccess( |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1077 |
new JavaNetSocketAccess() { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1078 |
@Override |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1079 |
public ServerSocket newServerSocket(SocketImpl impl) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1080 |
return new ServerSocket(impl); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1081 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1082 |
|
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1083 |
@Override |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1084 |
public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1085 |
try { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1086 |
Constructor<? extends SocketImpl> ctor = |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1087 |
implClass.getDeclaredConstructor(); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1088 |
return ctor.newInstance(); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1089 |
} catch (NoSuchMethodException | InstantiationException | |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1090 |
IllegalAccessException | InvocationTargetException e) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1091 |
throw new AssertionError(e); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1092 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1093 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1094 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1095 |
); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1096 |
} |
2 | 1097 |
} |