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