author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
child 6525 | 56be41b86ef8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1995, 2008, 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; |
|
31 |
import java.security.AccessController; |
|
32 |
import java.security.PrivilegedExceptionAction; |
|
33 |
||
34 |
/** |
|
35 |
* This class implements server sockets. A server socket waits for |
|
36 |
* requests to come in over the network. It performs some operation |
|
37 |
* based on that request, and then possibly returns a result to the requester. |
|
38 |
* <p> |
|
39 |
* The actual work of the server socket is performed by an instance |
|
40 |
* of the <code>SocketImpl</code> class. An application can |
|
41 |
* change the socket factory that creates the socket |
|
42 |
* implementation to configure itself to create sockets |
|
43 |
* appropriate to the local firewall. |
|
44 |
* |
|
45 |
* @author unascribed |
|
46 |
* @see java.net.SocketImpl |
|
47 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
48 |
* @see java.nio.channels.ServerSocketChannel |
|
49 |
* @since JDK1.0 |
|
50 |
*/ |
|
51 |
public |
|
52 |
class ServerSocket implements java.io.Closeable { |
|
53 |
/** |
|
54 |
* Various states of this socket. |
|
55 |
*/ |
|
56 |
private boolean created = false; |
|
57 |
private boolean bound = false; |
|
58 |
private boolean closed = false; |
|
59 |
private Object closeLock = new Object(); |
|
60 |
||
61 |
/** |
|
62 |
* The implementation of this Socket. |
|
63 |
*/ |
|
64 |
private SocketImpl impl; |
|
65 |
||
66 |
/** |
|
67 |
* Are we using an older SocketImpl? |
|
68 |
*/ |
|
69 |
private boolean oldImpl = false; |
|
70 |
||
71 |
/** |
|
72 |
* Creates an unbound server socket. |
|
73 |
* |
|
74 |
* @exception IOException IO error when opening the socket. |
|
75 |
* @revised 1.4 |
|
76 |
*/ |
|
77 |
public ServerSocket() throws IOException { |
|
78 |
setImpl(); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Creates a server socket, bound to the specified port. A port number |
|
83 |
* of <code>0</code> means that the port number is automatically |
|
84 |
* allocated, typically from an ephemeral port range. This port |
|
85 |
* number can then be retrieved by calling {@link #getLocalPort getLocalPort}. |
|
86 |
* <p> |
|
87 |
* The maximum queue length for incoming connection indications (a |
|
88 |
* request to connect) is set to <code>50</code>. If a connection |
|
89 |
* indication arrives when the queue is full, the connection is refused. |
|
90 |
* <p> |
|
91 |
* If the application has specified a server socket factory, that |
|
92 |
* factory's <code>createSocketImpl</code> method is called to create |
|
93 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
|
94 |
* <p> |
|
95 |
* If there is a security manager, |
|
96 |
* its <code>checkListen</code> method is called |
|
97 |
* with the <code>port</code> argument |
|
98 |
* as its argument to ensure the operation is allowed. |
|
99 |
* This could result in a SecurityException. |
|
100 |
* |
|
101 |
* |
|
102 |
* @param port the port number, or <code>0</code> to use a port |
|
103 |
* number that is automatically allocated. |
|
104 |
* |
|
105 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
106 |
* @exception SecurityException |
|
107 |
* if a security manager exists and its <code>checkListen</code> |
|
108 |
* method doesn't allow the operation. |
|
109 |
* @exception IllegalArgumentException if the port parameter is outside |
|
110 |
* the specified range of valid port values, which is between |
|
111 |
* 0 and 65535, inclusive. |
|
112 |
* |
|
113 |
* @see java.net.SocketImpl |
|
114 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
115 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
116 |
* @see SecurityManager#checkListen |
|
117 |
*/ |
|
118 |
public ServerSocket(int port) throws IOException { |
|
119 |
this(port, 50, null); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Creates a server socket and binds it to the specified local port |
|
124 |
* number, with the specified backlog. |
|
125 |
* A port number of <code>0</code> means that the port number is |
|
126 |
* automatically allocated, typically from an ephemeral port range. |
|
127 |
* This port number can then be retrieved by calling |
|
128 |
* {@link #getLocalPort getLocalPort}. |
|
129 |
* <p> |
|
130 |
* The maximum queue length for incoming connection indications (a |
|
131 |
* request to connect) is set to the <code>backlog</code> parameter. If |
|
132 |
* a connection indication arrives when the queue is full, the |
|
133 |
* connection is refused. |
|
134 |
* <p> |
|
135 |
* If the application has specified a server socket factory, that |
|
136 |
* factory's <code>createSocketImpl</code> method is called to create |
|
137 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
|
138 |
* <p> |
|
139 |
* If there is a security manager, |
|
140 |
* its <code>checkListen</code> method is called |
|
141 |
* with the <code>port</code> argument |
|
142 |
* as its argument to ensure the operation is allowed. |
|
143 |
* This could result in a SecurityException. |
|
144 |
* |
|
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
145 |
* The <code>backlog</code> argument is the requested maximum number of |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
146 |
* 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
|
147 |
* specific. In particular, an implementation may impose a maximum length |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
148 |
* or may choose to ignore the parameter altogther. The value provided |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
149 |
* should be greater than <code>0</code>. If it is less than or equal to |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
150 |
* <code>0</code>, then an implementation specific default will be used. |
2 | 151 |
* <P> |
152 |
* |
|
153 |
* @param port the port number, or <code>0</code> to use a port |
|
154 |
* 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
|
155 |
* @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
|
156 |
* connections. |
2 | 157 |
* |
158 |
* @exception IOException if an I/O error occurs when opening the socket. |
|
159 |
* @exception SecurityException |
|
160 |
* if a security manager exists and its <code>checkListen</code> |
|
161 |
* method doesn't allow the operation. |
|
162 |
* @exception IllegalArgumentException if the port parameter is outside |
|
163 |
* the specified range of valid port values, which is between |
|
164 |
* 0 and 65535, inclusive. |
|
165 |
* |
|
166 |
* @see java.net.SocketImpl |
|
167 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
168 |
* @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) |
|
169 |
* @see SecurityManager#checkListen |
|
170 |
*/ |
|
171 |
public ServerSocket(int port, int backlog) throws IOException { |
|
172 |
this(port, backlog, null); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Create a server with the specified port, listen backlog, and |
|
177 |
* local IP address to bind to. The <i>bindAddr</i> argument |
|
178 |
* can be used on a multi-homed host for a ServerSocket that |
|
179 |
* will only accept connect requests to one of its addresses. |
|
180 |
* If <i>bindAddr</i> is null, it will default accepting |
|
181 |
* connections on any/all local addresses. |
|
182 |
* The port must be between 0 and 65535, inclusive. |
|
183 |
* A port number of <code>0</code> means that the port number is |
|
184 |
* automatically allocated, typically from an ephemeral port range. |
|
185 |
* This port number can then be retrieved by calling |
|
186 |
* {@link #getLocalPort getLocalPort}. |
|
187 |
* |
|
188 |
* <P>If there is a security manager, this method |
|
189 |
* calls its <code>checkListen</code> method |
|
190 |
* with the <code>port</code> argument |
|
191 |
* as its argument to ensure the operation is allowed. |
|
192 |
* This could result in a SecurityException. |
|
193 |
* |
|
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
194 |
* The <code>backlog</code> argument is the requested maximum number of |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
195 |
* 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
|
196 |
* specific. In particular, an implementation may impose a maximum length |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
197 |
* or may choose to ignore the parameter altogther. The value provided |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
198 |
* should be greater than <code>0</code>. If it is less than or equal to |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
199 |
* <code>0</code>, then an implementation specific default will be used. |
2 | 200 |
* <P> |
201 |
* @param port the port number, or <code>0</code> to use a port |
|
202 |
* 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
|
203 |
* @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
|
204 |
* connections. |
2 | 205 |
* @param bindAddr the local InetAddress the server will bind to |
206 |
* |
|
207 |
* @throws SecurityException if a security manager exists and |
|
208 |
* its <code>checkListen</code> method doesn't allow the operation. |
|
209 |
* |
|
210 |
* @throws IOException if an I/O error occurs when opening the socket. |
|
211 |
* @exception IllegalArgumentException if the port parameter is outside |
|
212 |
* the specified range of valid port values, which is between |
|
213 |
* 0 and 65535, inclusive. |
|
214 |
* |
|
215 |
* @see SocketOptions |
|
216 |
* @see SocketImpl |
|
217 |
* @see SecurityManager#checkListen |
|
218 |
* @since JDK1.1 |
|
219 |
*/ |
|
220 |
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { |
|
221 |
setImpl(); |
|
222 |
if (port < 0 || port > 0xFFFF) |
|
223 |
throw new IllegalArgumentException( |
|
224 |
"Port value out of range: " + port); |
|
225 |
if (backlog < 1) |
|
226 |
backlog = 50; |
|
227 |
try { |
|
228 |
bind(new InetSocketAddress(bindAddr, port), backlog); |
|
229 |
} catch(SecurityException e) { |
|
230 |
close(); |
|
231 |
throw e; |
|
232 |
} catch(IOException e) { |
|
233 |
close(); |
|
234 |
throw e; |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Get the <code>SocketImpl</code> attached to this socket, creating |
|
240 |
* it if necessary. |
|
241 |
* |
|
242 |
* @return the <code>SocketImpl</code> attached to that ServerSocket. |
|
243 |
* @throws SocketException if creation fails. |
|
244 |
* @since 1.4 |
|
245 |
*/ |
|
246 |
SocketImpl getImpl() throws SocketException { |
|
247 |
if (!created) |
|
248 |
createImpl(); |
|
249 |
return impl; |
|
250 |
} |
|
251 |
||
252 |
private void checkOldImpl() { |
|
253 |
if (impl == null) |
|
254 |
return; |
|
255 |
// SocketImpl.connect() is a protected method, therefore we need to use |
|
256 |
// getDeclaredMethod, therefore we need permission to access the member |
|
257 |
try { |
|
51 | 258 |
AccessController.doPrivileged( |
259 |
new PrivilegedExceptionAction<Void>() { |
|
260 |
public Void run() throws NoSuchMethodException { |
|
2 | 261 |
Class[] cl = new Class[2]; |
262 |
cl[0] = SocketAddress.class; |
|
263 |
cl[1] = Integer.TYPE; |
|
264 |
impl.getClass().getDeclaredMethod("connect", cl); |
|
265 |
return null; |
|
266 |
} |
|
267 |
}); |
|
268 |
} catch (java.security.PrivilegedActionException e) { |
|
269 |
oldImpl = true; |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
private void setImpl() { |
|
274 |
if (factory != null) { |
|
275 |
impl = factory.createSocketImpl(); |
|
276 |
checkOldImpl(); |
|
277 |
} else { |
|
278 |
// No need to do a checkOldImpl() here, we know it's an up to date |
|
279 |
// SocketImpl! |
|
280 |
impl = new SocksSocketImpl(); |
|
281 |
} |
|
282 |
if (impl != null) |
|
283 |
impl.setServerSocket(this); |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Creates the socket implementation. |
|
288 |
* |
|
289 |
* @throws IOException if creation fails |
|
290 |
* @since 1.4 |
|
291 |
*/ |
|
292 |
void createImpl() throws SocketException { |
|
293 |
if (impl == null) |
|
294 |
setImpl(); |
|
295 |
try { |
|
296 |
impl.create(true); |
|
297 |
created = true; |
|
298 |
} catch (IOException e) { |
|
299 |
throw new SocketException(e.getMessage()); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* |
|
305 |
* Binds the <code>ServerSocket</code> to a specific address |
|
306 |
* (IP address and port number). |
|
307 |
* <p> |
|
308 |
* If the address is <code>null</code>, then the system will pick up |
|
309 |
* an ephemeral port and a valid local address to bind the socket. |
|
310 |
* <p> |
|
311 |
* @param endpoint The IP address & port number to bind to. |
|
312 |
* @throws IOException if the bind operation fails, or if the socket |
|
313 |
* is already bound. |
|
314 |
* @throws SecurityException if a <code>SecurityManager</code> is present and |
|
315 |
* its <code>checkListen</code> method doesn't allow the operation. |
|
316 |
* @throws IllegalArgumentException if endpoint is a |
|
317 |
* SocketAddress subclass not supported by this socket |
|
318 |
* @since 1.4 |
|
319 |
*/ |
|
320 |
public void bind(SocketAddress endpoint) throws IOException { |
|
321 |
bind(endpoint, 50); |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* |
|
326 |
* Binds the <code>ServerSocket</code> to a specific address |
|
327 |
* (IP address and port number). |
|
328 |
* <p> |
|
329 |
* If the address is <code>null</code>, then the system will pick up |
|
330 |
* an ephemeral port and a valid local address to bind the socket. |
|
331 |
* <P> |
|
1096
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
332 |
* The <code>backlog</code> argument is the requested maximum number of |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
333 |
* 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
|
334 |
* specific. In particular, an implementation may impose a maximum length |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
335 |
* or may choose to ignore the parameter altogther. The value provided |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
336 |
* should be greater than <code>0</code>. If it is less than or equal to |
7906d13db4eb
6258215: Num of backlog in ServerSocket(int, int) should be mentioned more explicitly
michaelm
parents:
715
diff
changeset
|
337 |
* <code>0</code>, then an implementation specific default will be used. |
2 | 338 |
* @param endpoint The IP address & 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
|
339 |
* @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
|
340 |
* incoming connections. |
2 | 341 |
* @throws IOException if the bind operation fails, or if the socket |
342 |
* is already bound. |
|
343 |
* @throws SecurityException if a <code>SecurityManager</code> is present and |
|
344 |
* its <code>checkListen</code> method doesn't allow the operation. |
|
345 |
* @throws IllegalArgumentException if endpoint is a |
|
346 |
* SocketAddress subclass not supported by this socket |
|
347 |
* @since 1.4 |
|
348 |
*/ |
|
349 |
public void bind(SocketAddress endpoint, int backlog) throws IOException { |
|
350 |
if (isClosed()) |
|
351 |
throw new SocketException("Socket is closed"); |
|
352 |
if (!oldImpl && isBound()) |
|
353 |
throw new SocketException("Already bound"); |
|
354 |
if (endpoint == null) |
|
355 |
endpoint = new InetSocketAddress(0); |
|
356 |
if (!(endpoint instanceof InetSocketAddress)) |
|
357 |
throw new IllegalArgumentException("Unsupported address type"); |
|
358 |
InetSocketAddress epoint = (InetSocketAddress) endpoint; |
|
359 |
if (epoint.isUnresolved()) |
|
360 |
throw new SocketException("Unresolved address"); |
|
361 |
if (backlog < 1) |
|
362 |
backlog = 50; |
|
363 |
try { |
|
364 |
SecurityManager security = System.getSecurityManager(); |
|
365 |
if (security != null) |
|
366 |
security.checkListen(epoint.getPort()); |
|
367 |
getImpl().bind(epoint.getAddress(), epoint.getPort()); |
|
368 |
getImpl().listen(backlog); |
|
369 |
bound = true; |
|
370 |
} catch(SecurityException e) { |
|
371 |
bound = false; |
|
372 |
throw e; |
|
373 |
} catch(IOException e) { |
|
374 |
bound = false; |
|
375 |
throw e; |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Returns the local address of this server socket. |
|
381 |
* <p> |
|
382 |
* If the socket was bound prior to being {@link #close closed}, |
|
383 |
* then this method will continue to return the local address |
|
384 |
* after the socket is closed. |
|
385 |
* |
|
386 |
* @return the address to which this socket is bound, |
|
387 |
* or <code>null</code> if the socket is unbound. |
|
388 |
*/ |
|
389 |
public InetAddress getInetAddress() { |
|
390 |
if (!isBound()) |
|
391 |
return null; |
|
392 |
try { |
|
393 |
return getImpl().getInetAddress(); |
|
394 |
} catch (SocketException e) { |
|
395 |
// nothing |
|
396 |
// If we're bound, the impl has been created |
|
397 |
// so we shouldn't get here |
|
398 |
} |
|
399 |
return null; |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Returns the port number on which this socket is listening. |
|
404 |
* <p> |
|
405 |
* If the socket was bound prior to being {@link #close closed}, |
|
406 |
* then this method will continue to return the port number |
|
407 |
* after the socket is closed. |
|
408 |
* |
|
409 |
* @return the port number to which this socket is listening or |
|
410 |
* -1 if the socket is not bound yet. |
|
411 |
*/ |
|
412 |
public int getLocalPort() { |
|
413 |
if (!isBound()) |
|
414 |
return -1; |
|
415 |
try { |
|
416 |
return getImpl().getLocalPort(); |
|
417 |
} catch (SocketException e) { |
|
418 |
// nothing |
|
419 |
// If we're bound, the impl has been created |
|
420 |
// so we shouldn't get here |
|
421 |
} |
|
422 |
return -1; |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* Returns the address of the endpoint this socket is bound to, or |
|
427 |
* <code>null</code> if it is not bound yet. |
|
428 |
* <p> |
|
429 |
* If the socket was bound prior to being {@link #close closed}, |
|
430 |
* then this method will continue to return the address of the endpoint |
|
431 |
* after the socket is closed. |
|
432 |
* |
|
433 |
* @return a <code>SocketAddress</code> representing the local endpoint of this |
|
434 |
* socket, or <code>null</code> if it is not bound yet. |
|
435 |
* @see #getInetAddress() |
|
436 |
* @see #getLocalPort() |
|
437 |
* @see #bind(SocketAddress) |
|
438 |
* @since 1.4 |
|
439 |
*/ |
|
440 |
||
441 |
public SocketAddress getLocalSocketAddress() { |
|
442 |
if (!isBound()) |
|
443 |
return null; |
|
444 |
return new InetSocketAddress(getInetAddress(), getLocalPort()); |
|
445 |
} |
|
446 |
||
447 |
/** |
|
448 |
* Listens for a connection to be made to this socket and accepts |
|
449 |
* it. The method blocks until a connection is made. |
|
450 |
* |
|
451 |
* <p>A new Socket <code>s</code> is created and, if there |
|
452 |
* is a security manager, |
|
453 |
* the security manager's <code>checkAccept</code> method is called |
|
454 |
* with <code>s.getInetAddress().getHostAddress()</code> and |
|
455 |
* <code>s.getPort()</code> |
|
456 |
* as its arguments to ensure the operation is allowed. |
|
457 |
* This could result in a SecurityException. |
|
458 |
* |
|
459 |
* @exception IOException if an I/O error occurs when waiting for a |
|
460 |
* connection. |
|
461 |
* @exception SecurityException if a security manager exists and its |
|
462 |
* <code>checkAccept</code> method doesn't allow the operation. |
|
463 |
* @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and |
|
464 |
* the timeout has been reached. |
|
465 |
* @exception java.nio.channels.IllegalBlockingModeException |
|
466 |
* if this socket has an associated channel, the channel is in |
|
467 |
* non-blocking mode, and there is no connection ready to be |
|
468 |
* accepted |
|
469 |
* |
|
470 |
* @return the new Socket |
|
471 |
* @see SecurityManager#checkAccept |
|
472 |
* @revised 1.4 |
|
473 |
* @spec JSR-51 |
|
474 |
*/ |
|
475 |
public Socket accept() throws IOException { |
|
476 |
if (isClosed()) |
|
477 |
throw new SocketException("Socket is closed"); |
|
478 |
if (!isBound()) |
|
479 |
throw new SocketException("Socket is not bound yet"); |
|
480 |
Socket s = new Socket((SocketImpl) null); |
|
481 |
implAccept(s); |
|
482 |
return s; |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* Subclasses of ServerSocket use this method to override accept() |
|
487 |
* to return their own subclass of socket. So a FooServerSocket |
|
488 |
* will typically hand this method an <i>empty</i> FooSocket. On |
|
489 |
* return from implAccept the FooSocket will be connected to a client. |
|
490 |
* |
|
491 |
* @param s the Socket |
|
492 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
493 |
* if this socket has an associated channel, |
|
494 |
* and the channel is in non-blocking mode |
|
495 |
* @throws IOException if an I/O error occurs when waiting |
|
496 |
* for a connection. |
|
497 |
* @since JDK1.1 |
|
498 |
* @revised 1.4 |
|
499 |
* @spec JSR-51 |
|
500 |
*/ |
|
501 |
protected final void implAccept(Socket s) throws IOException { |
|
502 |
SocketImpl si = null; |
|
503 |
try { |
|
504 |
if (s.impl == null) |
|
505 |
s.setImpl(); |
|
506 |
else { |
|
507 |
s.impl.reset(); |
|
508 |
} |
|
509 |
si = s.impl; |
|
510 |
s.impl = null; |
|
511 |
si.address = new InetAddress(); |
|
512 |
si.fd = new FileDescriptor(); |
|
513 |
getImpl().accept(si); |
|
514 |
||
515 |
SecurityManager security = System.getSecurityManager(); |
|
516 |
if (security != null) { |
|
517 |
security.checkAccept(si.getInetAddress().getHostAddress(), |
|
518 |
si.getPort()); |
|
519 |
} |
|
520 |
} catch (IOException e) { |
|
521 |
if (si != null) |
|
522 |
si.reset(); |
|
523 |
s.impl = si; |
|
524 |
throw e; |
|
525 |
} catch (SecurityException e) { |
|
526 |
if (si != null) |
|
527 |
si.reset(); |
|
528 |
s.impl = si; |
|
529 |
throw e; |
|
530 |
} |
|
531 |
s.impl = si; |
|
532 |
s.postAccept(); |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* Closes this socket. |
|
537 |
* |
|
538 |
* Any thread currently blocked in {@link #accept()} will throw |
|
539 |
* a {@link SocketException}. |
|
540 |
* |
|
541 |
* <p> If this socket has an associated channel then the channel is closed |
|
542 |
* as well. |
|
543 |
* |
|
544 |
* @exception IOException if an I/O error occurs when closing the socket. |
|
545 |
* @revised 1.4 |
|
546 |
* @spec JSR-51 |
|
547 |
*/ |
|
548 |
public void close() throws IOException { |
|
549 |
synchronized(closeLock) { |
|
550 |
if (isClosed()) |
|
551 |
return; |
|
552 |
if (created) |
|
553 |
impl.close(); |
|
554 |
closed = true; |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Returns the unique {@link java.nio.channels.ServerSocketChannel} object |
|
560 |
* associated with this socket, if any. |
|
561 |
* |
|
562 |
* <p> A server socket will have a channel if, and only if, the channel |
|
563 |
* itself was created via the {@link |
|
564 |
* java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open} |
|
565 |
* method. |
|
566 |
* |
|
567 |
* @return the server-socket channel associated with this socket, |
|
568 |
* or <tt>null</tt> if this socket was not created |
|
569 |
* for a channel |
|
570 |
* |
|
571 |
* @since 1.4 |
|
572 |
* @spec JSR-51 |
|
573 |
*/ |
|
574 |
public ServerSocketChannel getChannel() { |
|
575 |
return null; |
|
576 |
} |
|
577 |
||
578 |
/** |
|
579 |
* Returns the binding state of the ServerSocket. |
|
580 |
* |
|
581 |
* @return true if the ServerSocket succesfuly bound to an address |
|
582 |
* @since 1.4 |
|
583 |
*/ |
|
584 |
public boolean isBound() { |
|
585 |
// Before 1.3 ServerSockets were always bound during creation |
|
586 |
return bound || oldImpl; |
|
587 |
} |
|
588 |
||
589 |
/** |
|
590 |
* Returns the closed state of the ServerSocket. |
|
591 |
* |
|
592 |
* @return true if the socket has been closed |
|
593 |
* @since 1.4 |
|
594 |
*/ |
|
595 |
public boolean isClosed() { |
|
596 |
synchronized(closeLock) { |
|
597 |
return closed; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* Enable/disable SO_TIMEOUT with the specified timeout, in |
|
603 |
* milliseconds. With this option set to a non-zero timeout, |
|
604 |
* a call to accept() for this ServerSocket |
|
605 |
* will block for only this amount of time. If the timeout expires, |
|
606 |
* a <B>java.net.SocketTimeoutException</B> is raised, though the |
|
607 |
* ServerSocket is still valid. The option <B>must</B> be enabled |
|
608 |
* prior to entering the blocking operation to have effect. The |
|
609 |
* timeout must be > 0. |
|
610 |
* A timeout of zero is interpreted as an infinite timeout. |
|
611 |
* @param timeout the specified timeout, in milliseconds |
|
612 |
* @exception SocketException if there is an error in |
|
613 |
* the underlying protocol, such as a TCP error. |
|
614 |
* @since JDK1.1 |
|
615 |
* @see #getSoTimeout() |
|
616 |
*/ |
|
617 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
618 |
if (isClosed()) |
|
619 |
throw new SocketException("Socket is closed"); |
|
620 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); |
|
621 |
} |
|
622 |
||
623 |
/** |
|
624 |
* Retrieve setting for SO_TIMEOUT. 0 returns implies that the |
|
625 |
* option is disabled (i.e., timeout of infinity). |
|
626 |
* @return the SO_TIMEOUT value |
|
627 |
* @exception IOException if an I/O error occurs |
|
628 |
* @since JDK1.1 |
|
629 |
* @see #setSoTimeout(int) |
|
630 |
*/ |
|
631 |
public synchronized int getSoTimeout() throws IOException { |
|
632 |
if (isClosed()) |
|
633 |
throw new SocketException("Socket is closed"); |
|
634 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
635 |
/* extra type safety */ |
|
636 |
if (o instanceof Integer) { |
|
637 |
return ((Integer) o).intValue(); |
|
638 |
} else { |
|
639 |
return 0; |
|
640 |
} |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
* Enable/disable the SO_REUSEADDR socket option. |
|
645 |
* <p> |
|
646 |
* When a TCP connection is closed the connection may remain |
|
647 |
* in a timeout state for a period of time after the connection |
|
648 |
* is closed (typically known as the <tt>TIME_WAIT</tt> state |
|
649 |
* or <tt>2MSL</tt> wait state). |
|
650 |
* For applications using a well known socket address or port |
|
651 |
* it may not be possible to bind a socket to the required |
|
652 |
* <tt>SocketAddress</tt> if there is a connection in the |
|
653 |
* timeout state involving the socket address or port. |
|
654 |
* <p> |
|
655 |
* Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket |
|
656 |
* using {@link #bind(SocketAddress)} allows the socket to be |
|
657 |
* bound even though a previous connection is in a timeout |
|
658 |
* state. |
|
659 |
* <p> |
|
660 |
* When a <tt>ServerSocket</tt> is created the initial setting |
|
661 |
* of <tt>SO_REUSEADDR</tt> is not defined. Applications can |
|
662 |
* use {@link #getReuseAddress()} to determine the initial |
|
663 |
* setting of <tt>SO_REUSEADDR</tt>. |
|
664 |
* <p> |
|
665 |
* The behaviour when <tt>SO_REUSEADDR</tt> is enabled or |
|
666 |
* disabled after a socket is bound (See {@link #isBound()}) |
|
667 |
* is not defined. |
|
668 |
* |
|
669 |
* @param on whether to enable or disable the socket option |
|
670 |
* @exception SocketException if an error occurs enabling or |
|
671 |
* disabling the <tt>SO_RESUEADDR</tt> socket option, |
|
672 |
* or the socket is closed. |
|
673 |
* @since 1.4 |
|
674 |
* @see #getReuseAddress() |
|
675 |
* @see #bind(SocketAddress) |
|
676 |
* @see #isBound() |
|
677 |
* @see #isClosed() |
|
678 |
*/ |
|
679 |
public void setReuseAddress(boolean on) throws SocketException { |
|
680 |
if (isClosed()) |
|
681 |
throw new SocketException("Socket is closed"); |
|
682 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
683 |
} |
|
684 |
||
685 |
/** |
|
686 |
* Tests if SO_REUSEADDR is enabled. |
|
687 |
* |
|
688 |
* @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled. |
|
689 |
* @exception SocketException if there is an error |
|
690 |
* in the underlying protocol, such as a TCP error. |
|
691 |
* @since 1.4 |
|
692 |
* @see #setReuseAddress(boolean) |
|
693 |
*/ |
|
694 |
public boolean getReuseAddress() throws SocketException { |
|
695 |
if (isClosed()) |
|
696 |
throw new SocketException("Socket is closed"); |
|
697 |
return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); |
|
698 |
} |
|
699 |
||
700 |
/** |
|
701 |
* Returns the implementation address and implementation port of |
|
702 |
* this socket as a <code>String</code>. |
|
703 |
* |
|
704 |
* @return a string representation of this socket. |
|
705 |
*/ |
|
706 |
public String toString() { |
|
707 |
if (!isBound()) |
|
708 |
return "ServerSocket[unbound]"; |
|
709 |
return "ServerSocket[addr=" + impl.getInetAddress() + |
|
710 |
",port=" + impl.getPort() + |
|
711 |
",localport=" + impl.getLocalPort() + "]"; |
|
712 |
} |
|
713 |
||
714 |
void setBound() { |
|
715 |
bound = true; |
|
716 |
} |
|
717 |
||
718 |
void setCreated() { |
|
719 |
created = true; |
|
720 |
} |
|
721 |
||
722 |
/** |
|
723 |
* The factory for all server sockets. |
|
724 |
*/ |
|
725 |
private static SocketImplFactory factory = null; |
|
726 |
||
727 |
/** |
|
728 |
* Sets the server socket implementation factory for the |
|
729 |
* application. The factory can be specified only once. |
|
730 |
* <p> |
|
731 |
* When an application creates a new server socket, the socket |
|
732 |
* implementation factory's <code>createSocketImpl</code> method is |
|
733 |
* called to create the actual socket implementation. |
|
734 |
* <p> |
|
735 |
* Passing <code>null</code> to the method is a no-op unless the factory |
|
736 |
* was already set. |
|
737 |
* <p> |
|
738 |
* If there is a security manager, this method first calls |
|
739 |
* the security manager's <code>checkSetFactory</code> method |
|
740 |
* to ensure the operation is allowed. |
|
741 |
* This could result in a SecurityException. |
|
742 |
* |
|
743 |
* @param fac the desired factory. |
|
744 |
* @exception IOException if an I/O error occurs when setting the |
|
745 |
* socket factory. |
|
746 |
* @exception SocketException if the factory has already been defined. |
|
747 |
* @exception SecurityException if a security manager exists and its |
|
748 |
* <code>checkSetFactory</code> method doesn't allow the operation. |
|
749 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
750 |
* @see SecurityManager#checkSetFactory |
|
751 |
*/ |
|
752 |
public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { |
|
753 |
if (factory != null) { |
|
754 |
throw new SocketException("factory already defined"); |
|
755 |
} |
|
756 |
SecurityManager security = System.getSecurityManager(); |
|
757 |
if (security != null) { |
|
758 |
security.checkSetFactory(); |
|
759 |
} |
|
760 |
factory = fac; |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Sets a default proposed value for the SO_RCVBUF option for sockets |
|
765 |
* accepted from this <tt>ServerSocket</tt>. The value actually set |
|
766 |
* in the accepted socket must be determined by calling |
|
767 |
* {@link Socket#getReceiveBufferSize()} after the socket |
|
768 |
* is returned by {@link #accept()}. |
|
769 |
* <p> |
|
770 |
* The value of SO_RCVBUF is used both to set the size of the internal |
|
771 |
* socket receive buffer, and to set the size of the TCP receive window |
|
772 |
* that is advertized to the remote peer. |
|
773 |
* <p> |
|
774 |
* It is possible to change the value subsequently, by calling |
|
775 |
* {@link Socket#setReceiveBufferSize(int)}. However, if the application |
|
776 |
* wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 |
|
777 |
* then the proposed value must be set in the ServerSocket <B>before</B> |
|
778 |
* it is bound to a local address. This implies, that the ServerSocket must be |
|
779 |
* created with the no-argument constructor, then setReceiveBufferSize() must |
|
780 |
* be called and lastly the ServerSocket is bound to an address by calling bind(). |
|
781 |
* <p> |
|
782 |
* Failure to do this will not cause an error, and the buffer size may be set to the |
|
783 |
* requested value but the TCP receive window in sockets accepted from |
|
784 |
* this ServerSocket will be no larger than 64K bytes. |
|
785 |
* |
|
786 |
* @exception SocketException if there is an error |
|
787 |
* in the underlying protocol, such as a TCP error. |
|
788 |
* |
|
789 |
* @param size the size to which to set the receive buffer |
|
790 |
* size. This value must be greater than 0. |
|
791 |
* |
|
792 |
* @exception IllegalArgumentException if the |
|
793 |
* value is 0 or is negative. |
|
794 |
* |
|
795 |
* @since 1.4 |
|
796 |
* @see #getReceiveBufferSize |
|
797 |
*/ |
|
798 |
public synchronized void setReceiveBufferSize (int size) throws SocketException { |
|
799 |
if (!(size > 0)) { |
|
800 |
throw new IllegalArgumentException("negative receive size"); |
|
801 |
} |
|
802 |
if (isClosed()) |
|
803 |
throw new SocketException("Socket is closed"); |
|
804 |
getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size)); |
|
805 |
} |
|
806 |
||
807 |
/** |
|
808 |
* Gets the value of the SO_RCVBUF option for this <tt>ServerSocket</tt>, |
|
809 |
* that is the proposed buffer size that will be used for Sockets accepted |
|
810 |
* from this <tt>ServerSocket</tt>. |
|
811 |
* |
|
812 |
* <p>Note, the value actually set in the accepted socket is determined by |
|
813 |
* calling {@link Socket#getReceiveBufferSize()}. |
|
814 |
* @return the value of the SO_RCVBUF option for this <tt>Socket</tt>. |
|
815 |
* @exception SocketException if there is an error |
|
816 |
* in the underlying protocol, such as a TCP error. |
|
817 |
* @see #setReceiveBufferSize(int) |
|
818 |
* @since 1.4 |
|
819 |
*/ |
|
820 |
public synchronized int getReceiveBufferSize() |
|
821 |
throws SocketException{ |
|
822 |
if (isClosed()) |
|
823 |
throw new SocketException("Socket is closed"); |
|
824 |
int result = 0; |
|
825 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
826 |
if (o instanceof Integer) { |
|
827 |
result = ((Integer)o).intValue(); |
|
828 |
} |
|
829 |
return result; |
|
830 |
} |
|
831 |
||
832 |
/** |
|
833 |
* Sets performance preferences for this ServerSocket. |
|
834 |
* |
|
835 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
836 |
* may offer alternative protocols which have different performance |
|
837 |
* characteristics than TCP/IP. This method allows the application to |
|
838 |
* express its own preferences as to how these tradeoffs should be made |
|
839 |
* when the implementation chooses from the available protocols. |
|
840 |
* |
|
841 |
* <p> Performance preferences are described by three integers |
|
842 |
* whose values indicate the relative importance of short connection time, |
|
843 |
* low latency, and high bandwidth. The absolute values of the integers |
|
844 |
* are irrelevant; in order to choose a protocol the values are simply |
|
845 |
* compared, with larger values indicating stronger preferences. If the |
|
846 |
* application prefers short connection time over both low latency and high |
|
847 |
* bandwidth, for example, then it could invoke this method with the values |
|
848 |
* <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low |
|
849 |
* latency, and low latency above short connection time, then it could |
|
850 |
* invoke this method with the values <tt>(0, 1, 2)</tt>. |
|
851 |
* |
|
852 |
* <p> Invoking this method after this socket has been bound |
|
853 |
* will have no effect. This implies that in order to use this capability |
|
854 |
* requires the socket to be created with the no-argument constructor. |
|
855 |
* |
|
856 |
* @param connectionTime |
|
857 |
* An <tt>int</tt> expressing the relative importance of a short |
|
858 |
* connection time |
|
859 |
* |
|
860 |
* @param latency |
|
861 |
* An <tt>int</tt> expressing the relative importance of low |
|
862 |
* latency |
|
863 |
* |
|
864 |
* @param bandwidth |
|
865 |
* An <tt>int</tt> expressing the relative importance of high |
|
866 |
* bandwidth |
|
867 |
* |
|
868 |
* @since 1.5 |
|
869 |
*/ |
|
870 |
public void setPerformancePreferences(int connectionTime, |
|
871 |
int latency, |
|
872 |
int bandwidth) |
|
873 |
{ |
|
874 |
/* Not implemented yet */ |
|
875 |
} |
|
876 |
||
877 |
} |