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