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