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