author | prappo |
Tue, 13 Nov 2018 12:24:34 +0000 | |
changeset 52499 | 768b1c612100 |
parent 52456 | 90ff0e286a5e |
child 54155 | b5a73f22b2bd |
child 57110 | b848ca1ef778 |
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; |
2 | 40 |
|
41 |
/** |
|
42 |
* This class implements server sockets. A server socket waits for |
|
43 |
* requests to come in over the network. It performs some operation |
|
44 |
* based on that request, and then possibly returns a result to the requester. |
|
45 |
* <p> |
|
46 |
* The actual work of the server socket is performed by an instance |
|
19069 | 47 |
* of the {@code SocketImpl} class. An application can |
2 | 48 |
* change the socket factory that creates the socket |
49 |
* implementation to configure itself to create sockets |
|
50 |
* appropriate to the local firewall. |
|
51 |
* |
|
52 |
* @author unascribed |
|
53 |
* @see java.net.SocketImpl |
|
54 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
55 |
* @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
|
56 |
* @since 1.0 |
2 | 57 |
*/ |
58 |
public |
|
59 |
class ServerSocket implements java.io.Closeable { |
|
60 |
/** |
|
61 |
* Various states of this socket. |
|
62 |
*/ |
|
63 |
private boolean created = false; |
|
64 |
private boolean bound = false; |
|
65 |
private boolean closed = false; |
|
66 |
private Object closeLock = new Object(); |
|
67 |
||
68 |
/** |
|
69 |
* The implementation of this Socket. |
|
70 |
*/ |
|
71 |
private SocketImpl impl; |
|
72 |
||
73 |
/** |
|
74 |
* Are we using an older SocketImpl? |
|
75 |
*/ |
|
76 |
private boolean oldImpl = false; |
|
77 |
||
78 |
/** |
|
52371
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
79 |
* 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
|
80 |
* |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
81 |
* @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
|
82 |
* |
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
83 |
* @throws NullPointerException if impl is {@code null}. |
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 |
* @since 12 |
6525 | 86 |
*/ |
52371
ae4967993e17
8213210: Change ServerSocket(SocketImpl impl) constructor to protected access
bpb
parents:
48737
diff
changeset
|
87 |
protected ServerSocket(SocketImpl impl) { |
6525 | 88 |
this.impl = impl; |
89 |
impl.setServerSocket(this); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
2 | 93 |
* Creates an unbound server socket. |
94 |
* |
|
95 |
* @exception IOException IO error when opening the socket. |
|
96 |
* @revised 1.4 |
|
97 |
*/ |
|
98 |
public ServerSocket() throws IOException { |
|
99 |
setImpl(); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Creates a server socket, bound to the specified port. A port number |
|
19069 | 104 |
* of {@code 0} means that the port number is automatically |
2 | 105 |
* allocated, typically from an ephemeral port range. This port |
106 |
* number can then be retrieved by calling {@link #getLocalPort getLocalPort}. |
|
107 |
* <p> |
|
108 |
* The maximum queue length for incoming connection indications (a |
|
19069 | 109 |
* request to connect) is set to {@code 50}. If a connection |
2 | 110 |
* indication arrives when the queue is full, the connection is refused. |
111 |
* <p> |
|
112 |
* If the application has specified a server socket factory, that |
|
19069 | 113 |
* factory's {@code createSocketImpl} method is called to create |
2 | 114 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
115 |
* <p> |
|
116 |
* If there is a security manager, |
|
19069 | 117 |
* its {@code checkListen} method is called |
118 |
* with the {@code port} argument |
|
2 | 119 |
* as its argument to ensure the operation is allowed. |
120 |
* This could result in a SecurityException. |
|
121 |
* |
|
122 |
* |
|
19069 | 123 |
* @param port the port number, or {@code 0} to use a port |
2 | 124 |
* number that is automatically allocated. |
125 |
* |
|
126 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
127 |
* @exception SecurityException |
|
19069 | 128 |
* if a security manager exists and its {@code checkListen} |
2 | 129 |
* method doesn't allow the operation. |
130 |
* @exception IllegalArgumentException if the port parameter is outside |
|
131 |
* the specified range of valid port values, which is between |
|
132 |
* 0 and 65535, inclusive. |
|
133 |
* |
|
134 |
* @see java.net.SocketImpl |
|
135 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
136 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
137 |
* @see SecurityManager#checkListen |
|
138 |
*/ |
|
139 |
public ServerSocket(int port) throws IOException { |
|
140 |
this(port, 50, null); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Creates a server socket and binds it to the specified local port |
|
145 |
* number, with the specified backlog. |
|
19069 | 146 |
* A port number of {@code 0} means that the port number is |
2 | 147 |
* automatically allocated, typically from an ephemeral port range. |
148 |
* This port number can then be retrieved by calling |
|
149 |
* {@link #getLocalPort getLocalPort}. |
|
150 |
* <p> |
|
151 |
* The maximum queue length for incoming connection indications (a |
|
19069 | 152 |
* request to connect) is set to the {@code backlog} parameter. If |
2 | 153 |
* a connection indication arrives when the queue is full, the |
154 |
* connection is refused. |
|
155 |
* <p> |
|
156 |
* If the application has specified a server socket factory, that |
|
19069 | 157 |
* factory's {@code createSocketImpl} method is called to create |
2 | 158 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
159 |
* <p> |
|
160 |
* If there is a security manager, |
|
19069 | 161 |
* its {@code checkListen} method is called |
162 |
* with the {@code port} argument |
|
2 | 163 |
* as its argument to ensure the operation is allowed. |
164 |
* This could result in a SecurityException. |
|
165 |
* |
|
19069 | 166 |
* 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
|
167 |
* 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
|
168 |
* specific. In particular, an implementation may impose a maximum length |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
169 |
* or may choose to ignore the parameter altogether. The value provided |
19069 | 170 |
* should be greater than {@code 0}. If it is less than or equal to |
171 |
* {@code 0}, then an implementation specific default will be used. |
|
2 | 172 |
* |
19069 | 173 |
* @param port the port number, or {@code 0} to use a port |
2 | 174 |
* 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
|
175 |
* @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
|
176 |
* connections. |
2 | 177 |
* |
178 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
179 |
* @exception SecurityException |
|
19069 | 180 |
* if a security manager exists and its {@code checkListen} |
2 | 181 |
* method doesn't allow the operation. |
182 |
* @exception IllegalArgumentException if the port parameter is outside |
|
183 |
* the specified range of valid port values, which is between |
|
184 |
* 0 and 65535, inclusive. |
|
185 |
* |
|
186 |
* @see java.net.SocketImpl |
|
187 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
188 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
189 |
* @see SecurityManager#checkListen |
|
190 |
*/ |
|
191 |
public ServerSocket(int port, int backlog) throws IOException { |
|
192 |
this(port, backlog, null); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Create a server with the specified port, listen backlog, and |
|
197 |
* local IP address to bind to. The <i>bindAddr</i> argument |
|
198 |
* can be used on a multi-homed host for a ServerSocket that |
|
199 |
* will only accept connect requests to one of its addresses. |
|
200 |
* If <i>bindAddr</i> is null, it will default accepting |
|
201 |
* connections on any/all local addresses. |
|
202 |
* The port must be between 0 and 65535, inclusive. |
|
19069 | 203 |
* A port number of {@code 0} means that the port number is |
2 | 204 |
* automatically allocated, typically from an ephemeral port range. |
205 |
* This port number can then be retrieved by calling |
|
206 |
* {@link #getLocalPort getLocalPort}. |
|
207 |
* |
|
208 |
* <P>If there is a security manager, this method |
|
19069 | 209 |
* calls its {@code checkListen} method |
210 |
* with the {@code port} argument |
|
2 | 211 |
* as its argument to ensure the operation is allowed. |
212 |
* This could result in a SecurityException. |
|
213 |
* |
|
19069 | 214 |
* 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
|
215 |
* 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
|
216 |
* specific. In particular, an implementation may impose a maximum length |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52456
diff
changeset
|
217 |
* or may choose to ignore the parameter altogether. The value provided |
19069 | 218 |
* should be greater than {@code 0}. If it is less than or equal to |
219 |
* {@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
|
220 |
* |
19069 | 221 |
* @param port the port number, or {@code 0} to use a port |
2 | 222 |
* 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
|
223 |
* @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
|
224 |
* connections. |
2 | 225 |
* @param bindAddr the local InetAddress the server will bind to |
226 |
* |
|
227 |
* @throws SecurityException if a security manager exists and |
|
19069 | 228 |
* its {@code checkListen} method doesn't allow the operation. |
2 | 229 |
* |
230 |
* @throws IOException if an I/O error occurs when opening the socket. |
|
231 |
* @exception IllegalArgumentException if the port parameter is outside |
|
232 |
* the specified range of valid port values, which is between |
|
233 |
* 0 and 65535, inclusive. |
|
234 |
* |
|
235 |
* @see SocketOptions |
|
236 |
* @see SocketImpl |
|
237 |
* @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
|
238 |
* @since 1.1 |
2 | 239 |
*/ |
240 |
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { |
|
241 |
setImpl(); |
|
242 |
if (port < 0 || port > 0xFFFF) |
|
243 |
throw new IllegalArgumentException( |
|
244 |
"Port value out of range: " + port); |
|
245 |
if (backlog < 1) |
|
246 |
backlog = 50; |
|
247 |
try { |
|
248 |
bind(new InetSocketAddress(bindAddr, port), backlog); |
|
249 |
} catch(SecurityException e) { |
|
250 |
close(); |
|
251 |
throw e; |
|
252 |
} catch(IOException e) { |
|
253 |
close(); |
|
254 |
throw e; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
19069 | 259 |
* Get the {@code SocketImpl} attached to this socket, creating |
2 | 260 |
* it if necessary. |
261 |
* |
|
19069 | 262 |
* @return the {@code SocketImpl} attached to that ServerSocket. |
2 | 263 |
* @throws SocketException if creation fails. |
264 |
* @since 1.4 |
|
265 |
*/ |
|
266 |
SocketImpl getImpl() throws SocketException { |
|
267 |
if (!created) |
|
268 |
createImpl(); |
|
269 |
return impl; |
|
270 |
} |
|
271 |
||
272 |
private void checkOldImpl() { |
|
273 |
if (impl == null) |
|
274 |
return; |
|
275 |
// SocketImpl.connect() is a protected method, therefore we need to use |
|
276 |
// getDeclaredMethod, therefore we need permission to access the member |
|
277 |
try { |
|
51 | 278 |
AccessController.doPrivileged( |
279 |
new PrivilegedExceptionAction<Void>() { |
|
280 |
public Void run() throws NoSuchMethodException { |
|
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
281 |
impl.getClass().getDeclaredMethod("connect", |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
282 |
SocketAddress.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10140
diff
changeset
|
283 |
int.class); |
2 | 284 |
return null; |
285 |
} |
|
286 |
}); |
|
287 |
} catch (java.security.PrivilegedActionException e) { |
|
288 |
oldImpl = true; |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
private void setImpl() { |
|
293 |
if (factory != null) { |
|
294 |
impl = factory.createSocketImpl(); |
|
295 |
checkOldImpl(); |
|
296 |
} else { |
|
297 |
// No need to do a checkOldImpl() here, we know it's an up to date |
|
298 |
// SocketImpl! |
|
299 |
impl = new SocksSocketImpl(); |
|
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 { |
|
545 |
SocketImpl si = null; |
|
546 |
try { |
|
547 |
if (s.impl == null) |
|
548 |
s.setImpl(); |
|
549 |
else { |
|
550 |
s.impl.reset(); |
|
551 |
} |
|
552 |
si = s.impl; |
|
553 |
s.impl = null; |
|
554 |
si.address = new InetAddress(); |
|
555 |
si.fd = new FileDescriptor(); |
|
556 |
getImpl().accept(si); |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
557 |
SocketCleanable.register(si.fd); // raw fd has been set |
2 | 558 |
|
559 |
SecurityManager security = System.getSecurityManager(); |
|
560 |
if (security != null) { |
|
561 |
security.checkAccept(si.getInetAddress().getHostAddress(), |
|
562 |
si.getPort()); |
|
563 |
} |
|
564 |
} catch (IOException e) { |
|
565 |
if (si != null) |
|
566 |
si.reset(); |
|
567 |
s.impl = si; |
|
568 |
throw e; |
|
569 |
} catch (SecurityException e) { |
|
570 |
if (si != null) |
|
571 |
si.reset(); |
|
572 |
s.impl = si; |
|
573 |
throw e; |
|
574 |
} |
|
575 |
s.impl = si; |
|
576 |
s.postAccept(); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Closes this socket. |
|
581 |
* |
|
582 |
* Any thread currently blocked in {@link #accept()} will throw |
|
583 |
* a {@link SocketException}. |
|
584 |
* |
|
585 |
* <p> If this socket has an associated channel then the channel is closed |
|
586 |
* as well. |
|
587 |
* |
|
588 |
* @exception IOException if an I/O error occurs when closing the socket. |
|
589 |
* @revised 1.4 |
|
590 |
* @spec JSR-51 |
|
591 |
*/ |
|
592 |
public void close() throws IOException { |
|
593 |
synchronized(closeLock) { |
|
594 |
if (isClosed()) |
|
595 |
return; |
|
596 |
if (created) |
|
597 |
impl.close(); |
|
598 |
closed = true; |
|
599 |
} |
|
600 |
} |
|
601 |
||
602 |
/** |
|
603 |
* Returns the unique {@link java.nio.channels.ServerSocketChannel} object |
|
604 |
* associated with this socket, if any. |
|
605 |
* |
|
606 |
* <p> A server socket will have a channel if, and only if, the channel |
|
607 |
* itself was created via the {@link |
|
608 |
* java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open} |
|
609 |
* method. |
|
610 |
* |
|
611 |
* @return the server-socket channel associated with this socket, |
|
19069 | 612 |
* or {@code null} if this socket was not created |
2 | 613 |
* for a channel |
614 |
* |
|
615 |
* @since 1.4 |
|
616 |
* @spec JSR-51 |
|
617 |
*/ |
|
618 |
public ServerSocketChannel getChannel() { |
|
619 |
return null; |
|
620 |
} |
|
621 |
||
622 |
/** |
|
623 |
* Returns the binding state of the ServerSocket. |
|
624 |
* |
|
21278 | 625 |
* @return true if the ServerSocket successfully bound to an address |
2 | 626 |
* @since 1.4 |
627 |
*/ |
|
628 |
public boolean isBound() { |
|
629 |
// Before 1.3 ServerSockets were always bound during creation |
|
630 |
return bound || oldImpl; |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Returns the closed state of the ServerSocket. |
|
635 |
* |
|
636 |
* @return true if the socket has been closed |
|
637 |
* @since 1.4 |
|
638 |
*/ |
|
639 |
public boolean isClosed() { |
|
640 |
synchronized(closeLock) { |
|
641 |
return closed; |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
646 |
* 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
|
647 |
* 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
|
648 |
* timeout, a call to accept() for this ServerSocket |
2 | 649 |
* will block for only this amount of time. If the timeout expires, |
650 |
* a <B>java.net.SocketTimeoutException</B> is raised, though the |
|
651 |
* ServerSocket is still valid. The option <B>must</B> be enabled |
|
652 |
* prior to entering the blocking operation to have effect. The |
|
18156 | 653 |
* timeout must be {@code > 0}. |
2 | 654 |
* A timeout of zero is interpreted as an infinite timeout. |
655 |
* @param timeout the specified timeout, in milliseconds |
|
656 |
* @exception SocketException if there is an error in |
|
657 |
* 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
|
658 |
* @since 1.1 |
2 | 659 |
* @see #getSoTimeout() |
660 |
*/ |
|
661 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
662 |
if (isClosed()) |
|
663 |
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
|
664 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout); |
2 | 665 |
} |
666 |
||
667 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
668 |
* 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
|
669 |
* 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
|
670 |
* @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value |
2 | 671 |
* @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
|
672 |
* @since 1.1 |
2 | 673 |
* @see #setSoTimeout(int) |
674 |
*/ |
|
675 |
public synchronized int getSoTimeout() throws IOException { |
|
676 |
if (isClosed()) |
|
677 |
throw new SocketException("Socket is closed"); |
|
678 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
679 |
/* extra type safety */ |
|
680 |
if (o instanceof Integer) { |
|
681 |
return ((Integer) o).intValue(); |
|
682 |
} else { |
|
683 |
return 0; |
|
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 the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
689 |
* socket option. |
2 | 690 |
* <p> |
691 |
* When a TCP connection is closed the connection may remain |
|
692 |
* in a timeout state for a period of time after the connection |
|
19069 | 693 |
* is closed (typically known as the {@code TIME_WAIT} state |
694 |
* or {@code 2MSL} wait state). |
|
2 | 695 |
* For applications using a well known socket address or port |
696 |
* it may not be possible to bind a socket to the required |
|
19069 | 697 |
* {@code SocketAddress} if there is a connection in the |
2 | 698 |
* timeout state involving the socket address or port. |
699 |
* <p> |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
700 |
* 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
|
701 |
* 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
|
702 |
* to be bound even though a previous connection is in a timeout state. |
2 | 703 |
* <p> |
19069 | 704 |
* 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
|
705 |
* 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
|
706 |
* 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
|
707 |
* setting of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}. |
2 | 708 |
* <p> |
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
709 |
* 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
|
710 |
* enabled or disabled after a socket is bound (See {@link #isBound()}) |
2 | 711 |
* is not defined. |
712 |
* |
|
713 |
* @param on whether to enable or disable the socket option |
|
714 |
* @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
|
715 |
* 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
|
716 |
* socket option, or the socket is closed. |
2 | 717 |
* @since 1.4 |
718 |
* @see #getReuseAddress() |
|
719 |
* @see #bind(SocketAddress) |
|
720 |
* @see #isBound() |
|
721 |
* @see #isClosed() |
|
722 |
*/ |
|
723 |
public void setReuseAddress(boolean on) throws SocketException { |
|
724 |
if (isClosed()) |
|
725 |
throw new SocketException("Socket is closed"); |
|
726 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
727 |
} |
|
728 |
||
729 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
730 |
* Tests if {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
2 | 731 |
* |
19069 | 732 |
* @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
|
733 |
* {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is enabled. |
2 | 734 |
* @exception SocketException if there is an error |
735 |
* in the underlying protocol, such as a TCP error. |
|
736 |
* @since 1.4 |
|
737 |
* @see #setReuseAddress(boolean) |
|
738 |
*/ |
|
739 |
public boolean getReuseAddress() throws SocketException { |
|
740 |
if (isClosed()) |
|
741 |
throw new SocketException("Socket is closed"); |
|
742 |
return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); |
|
743 |
} |
|
744 |
||
745 |
/** |
|
746 |
* Returns the implementation address and implementation port of |
|
19069 | 747 |
* this socket as a {@code String}. |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
748 |
* <p> |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
749 |
* 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
|
750 |
* 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
|
751 |
* 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
|
752 |
* an {@code InetAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
753 |
* {@link InetAddress#getLoopbackAddress loopback} address is returned as |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
754 |
* the implementation address. |
2 | 755 |
* |
756 |
* @return a string representation of this socket. |
|
757 |
*/ |
|
758 |
public String toString() { |
|
759 |
if (!isBound()) |
|
760 |
return "ServerSocket[unbound]"; |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
761 |
InetAddress in; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
762 |
if (System.getSecurityManager() != null) |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
763 |
in = InetAddress.getLoopbackAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
764 |
else |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
765 |
in = impl.getInetAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
14342
diff
changeset
|
766 |
return "ServerSocket[addr=" + in + |
2 | 767 |
",localport=" + impl.getLocalPort() + "]"; |
768 |
} |
|
769 |
||
770 |
void setBound() { |
|
771 |
bound = true; |
|
772 |
} |
|
773 |
||
774 |
void setCreated() { |
|
775 |
created = true; |
|
776 |
} |
|
777 |
||
778 |
/** |
|
779 |
* The factory for all server sockets. |
|
780 |
*/ |
|
781 |
private static SocketImplFactory factory = null; |
|
782 |
||
783 |
/** |
|
784 |
* Sets the server socket implementation factory for the |
|
785 |
* application. The factory can be specified only once. |
|
786 |
* <p> |
|
787 |
* When an application creates a new server socket, the socket |
|
19069 | 788 |
* implementation factory's {@code createSocketImpl} method is |
2 | 789 |
* called to create the actual socket implementation. |
790 |
* <p> |
|
19069 | 791 |
* Passing {@code null} to the method is a no-op unless the factory |
2 | 792 |
* was already set. |
793 |
* <p> |
|
794 |
* If there is a security manager, this method first calls |
|
19069 | 795 |
* the security manager's {@code checkSetFactory} method |
2 | 796 |
* to ensure the operation is allowed. |
797 |
* This could result in a SecurityException. |
|
798 |
* |
|
799 |
* @param fac the desired factory. |
|
800 |
* @exception IOException if an I/O error occurs when setting the |
|
801 |
* socket factory. |
|
802 |
* @exception SocketException if the factory has already been defined. |
|
803 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 804 |
* {@code checkSetFactory} method doesn't allow the operation. |
2 | 805 |
* @see java.net.SocketImplFactory#createSocketImpl() |
806 |
* @see SecurityManager#checkSetFactory |
|
807 |
*/ |
|
808 |
public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { |
|
809 |
if (factory != null) { |
|
810 |
throw new SocketException("factory already defined"); |
|
811 |
} |
|
812 |
SecurityManager security = System.getSecurityManager(); |
|
813 |
if (security != null) { |
|
814 |
security.checkSetFactory(); |
|
815 |
} |
|
816 |
factory = fac; |
|
817 |
} |
|
818 |
||
819 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
820 |
* Sets a default proposed value for the |
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
821 |
* {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option for sockets |
19069 | 822 |
* accepted from this {@code ServerSocket}. The value actually set |
2 | 823 |
* in the accepted socket must be determined by calling |
824 |
* {@link Socket#getReceiveBufferSize()} after the socket |
|
825 |
* is returned by {@link #accept()}. |
|
826 |
* <p> |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
827 |
* 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
|
828 |
* 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
|
829 |
* of the TCP receive window that is advertised to the remote peer. |
2 | 830 |
* <p> |
831 |
* It is possible to change the value subsequently, by calling |
|
832 |
* {@link Socket#setReceiveBufferSize(int)}. However, if the application |
|
833 |
* wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 |
|
834 |
* then the proposed value must be set in the ServerSocket <B>before</B> |
|
835 |
* it is bound to a local address. This implies, that the ServerSocket must be |
|
836 |
* created with the no-argument constructor, then setReceiveBufferSize() must |
|
837 |
* be called and lastly the ServerSocket is bound to an address by calling bind(). |
|
838 |
* <p> |
|
839 |
* Failure to do this will not cause an error, and the buffer size may be set to the |
|
840 |
* requested value but the TCP receive window in sockets accepted from |
|
841 |
* this ServerSocket will be no larger than 64K bytes. |
|
842 |
* |
|
843 |
* @exception SocketException if there is an error |
|
844 |
* in the underlying protocol, such as a TCP error. |
|
845 |
* |
|
846 |
* @param size the size to which to set the receive buffer |
|
847 |
* size. This value must be greater than 0. |
|
848 |
* |
|
849 |
* @exception IllegalArgumentException if the |
|
850 |
* value is 0 or is negative. |
|
851 |
* |
|
852 |
* @since 1.4 |
|
853 |
* @see #getReceiveBufferSize |
|
854 |
*/ |
|
855 |
public synchronized void setReceiveBufferSize (int size) throws SocketException { |
|
856 |
if (!(size > 0)) { |
|
857 |
throw new IllegalArgumentException("negative receive size"); |
|
858 |
} |
|
859 |
if (isClosed()) |
|
860 |
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
|
861 |
getImpl().setOption(SocketOptions.SO_RCVBUF, size); |
2 | 862 |
} |
863 |
||
864 |
/** |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
865 |
* Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option |
19069 | 866 |
* for this {@code ServerSocket}, that is the proposed buffer size that |
867 |
* will be used for Sockets accepted from this {@code ServerSocket}. |
|
2 | 868 |
* |
869 |
* <p>Note, the value actually set in the accepted socket is determined by |
|
870 |
* calling {@link Socket#getReceiveBufferSize()}. |
|
17469
e00a02431a8b
6328537: Improve javadocs for Socket class by adding references to SocketOptions
khazra
parents:
14342
diff
changeset
|
871 |
* @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} |
19069 | 872 |
* option for this {@code Socket}. |
2 | 873 |
* @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
|
874 |
* in the underlying protocol, such as a TCP error. |
2 | 875 |
* @see #setReceiveBufferSize(int) |
876 |
* @since 1.4 |
|
877 |
*/ |
|
878 |
public synchronized int getReceiveBufferSize() |
|
879 |
throws SocketException{ |
|
880 |
if (isClosed()) |
|
881 |
throw new SocketException("Socket is closed"); |
|
882 |
int result = 0; |
|
883 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
884 |
if (o instanceof Integer) { |
|
885 |
result = ((Integer)o).intValue(); |
|
886 |
} |
|
887 |
return result; |
|
888 |
} |
|
889 |
||
890 |
/** |
|
891 |
* Sets performance preferences for this ServerSocket. |
|
892 |
* |
|
893 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
894 |
* may offer alternative protocols which have different performance |
|
895 |
* characteristics than TCP/IP. This method allows the application to |
|
896 |
* express its own preferences as to how these tradeoffs should be made |
|
897 |
* when the implementation chooses from the available protocols. |
|
898 |
* |
|
899 |
* <p> Performance preferences are described by three integers |
|
900 |
* whose values indicate the relative importance of short connection time, |
|
901 |
* low latency, and high bandwidth. The absolute values of the integers |
|
902 |
* are irrelevant; in order to choose a protocol the values are simply |
|
903 |
* compared, with larger values indicating stronger preferences. If the |
|
904 |
* application prefers short connection time over both low latency and high |
|
905 |
* bandwidth, for example, then it could invoke this method with the values |
|
19069 | 906 |
* {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
2 | 907 |
* latency, and low latency above short connection time, then it could |
19069 | 908 |
* invoke this method with the values {@code (0, 1, 2)}. |
2 | 909 |
* |
910 |
* <p> Invoking this method after this socket has been bound |
|
911 |
* will have no effect. This implies that in order to use this capability |
|
912 |
* requires the socket to be created with the no-argument constructor. |
|
913 |
* |
|
914 |
* @param connectionTime |
|
19069 | 915 |
* An {@code int} expressing the relative importance of a short |
2 | 916 |
* connection time |
917 |
* |
|
918 |
* @param latency |
|
19069 | 919 |
* An {@code int} expressing the relative importance of low |
2 | 920 |
* latency |
921 |
* |
|
922 |
* @param bandwidth |
|
19069 | 923 |
* An {@code int} expressing the relative importance of high |
2 | 924 |
* bandwidth |
925 |
* |
|
926 |
* @since 1.5 |
|
927 |
*/ |
|
928 |
public void setPerformancePreferences(int connectionTime, |
|
929 |
int latency, |
|
930 |
int bandwidth) |
|
931 |
{ |
|
932 |
/* Not implemented yet */ |
|
933 |
} |
|
934 |
||
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
935 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
936 |
* Sets the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
937 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
938 |
* @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
|
939 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
940 |
* @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
|
941 |
* may be valid for some options. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
942 |
* @return this ServerSocket |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
943 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
944 |
* @throws UnsupportedOperationException if the server socket does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
945 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
946 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
947 |
* @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
|
948 |
* the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
949 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
950 |
* @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
|
951 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
952 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
953 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
954 |
* @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
|
955 |
* 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
|
956 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
957 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
958 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
959 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
960 |
* @since 9 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
961 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
962 |
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
|
963 |
throws IOException |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
964 |
{ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
965 |
getImpl().setOption(name, value); |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
966 |
return this; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
967 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
968 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
969 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
970 |
* Returns the value of a socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
971 |
* |
28852
a581c7868768
8041395: Doclint regression in java.net.SocketOption
darcy
parents:
25859
diff
changeset
|
972 |
* @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
|
973 |
* @param name The socket option |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
974 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
975 |
* @return The value of the socket option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
976 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
977 |
* @throws UnsupportedOperationException if the server socket does not |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
978 |
* support the option. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
979 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
980 |
* @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
|
981 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
982 |
* @throws NullPointerException if name is {@code null} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
983 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
984 |
* @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
|
985 |
* 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
|
986 |
* not have the required permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
987 |
* {@link java.net.StandardSocketOptions StandardSocketOptions} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
988 |
* do not require any security permission. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
989 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
990 |
* @since 9 |
23879
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 |
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
|
993 |
return getImpl().getOption(name); |
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 |
|
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
996 |
// 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
|
997 |
private volatile Set<SocketOption<?>> options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
998 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
999 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1000 |
* 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
|
1001 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1002 |
* 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
|
1003 |
* the socket has been closed. |
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 |
* @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
|
1006 |
* 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
|
1007 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
28852
diff
changeset
|
1008 |
* @since 9 |
23879
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 |
public Set<SocketOption<?>> supportedOptions() { |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1011 |
Set<SocketOption<?>> so = options; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1012 |
if (so != null) |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1013 |
return so; |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1014 |
|
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1015 |
try { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1016 |
SocketImpl impl = getImpl(); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1017 |
options = Collections.unmodifiableSet(impl.supportedOptions()); |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1018 |
} catch (IOException e) { |
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1019 |
options = Collections.emptySet(); |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1020 |
} |
52456
90ff0e286a5e
8213418: Socket/ServerSocket supportedOptions does not work with custom SocketImpl
chegar
parents:
52427
diff
changeset
|
1021 |
return options; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
23720
diff
changeset
|
1022 |
} |
40696
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1023 |
|
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1024 |
static { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1025 |
SharedSecrets.setJavaNetSocketAccess( |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1026 |
new JavaNetSocketAccess() { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1027 |
@Override |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1028 |
public ServerSocket newServerSocket(SocketImpl impl) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1029 |
return new ServerSocket(impl); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1030 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1031 |
|
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1032 |
@Override |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1033 |
public SocketImpl newSocketImpl(Class<? extends SocketImpl> implClass) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1034 |
try { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1035 |
Constructor<? extends SocketImpl> ctor = |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1036 |
implClass.getDeclaredConstructor(); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1037 |
return ctor.newInstance(); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1038 |
} catch (NoSuchMethodException | InstantiationException | |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1039 |
IllegalAccessException | InvocationTargetException e) { |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1040 |
throw new AssertionError(e); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1041 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1042 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1043 |
} |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1044 |
); |
d38ae985f810
8165180: Provide a shared secret to access non-public ServerSocket constructor
mchung
parents:
35302
diff
changeset
|
1045 |
} |
2 | 1046 |
} |