author | wetmore |
Tue, 11 Mar 2008 23:37:02 -0700 | |
changeset 94 | 842a7d676774 |
parent 81 | c2d2c408d6ed |
parent 51 | 6fe31bc95bbc |
child 475 | b8b79ef97ac8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InterruptedIOException; |
|
32 |
import java.nio.channels.SocketChannel; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.PrivilegedExceptionAction; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
||
37 |
/** |
|
38 |
* This class implements client sockets (also called just |
|
39 |
* "sockets"). A socket is an endpoint for communication |
|
40 |
* between two machines. |
|
41 |
* <p> |
|
42 |
* The actual work of the socket is performed by an instance of the |
|
43 |
* <code>SocketImpl</code> class. An application, by changing |
|
44 |
* the socket factory that creates the socket implementation, |
|
45 |
* can configure itself to create sockets appropriate to the local |
|
46 |
* firewall. |
|
47 |
* |
|
48 |
* @author unascribed |
|
49 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
50 |
* @see java.net.SocketImpl |
|
51 |
* @see java.nio.channels.SocketChannel |
|
52 |
* @since JDK1.0 |
|
53 |
*/ |
|
54 |
public |
|
55 |
class Socket implements java.io.Closeable { |
|
56 |
/** |
|
57 |
* Various states of this socket. |
|
58 |
*/ |
|
59 |
private boolean created = false; |
|
60 |
private boolean bound = false; |
|
61 |
private boolean connected = false; |
|
62 |
private boolean closed = false; |
|
63 |
private Object closeLock = new Object(); |
|
64 |
private boolean shutIn = false; |
|
65 |
private boolean shutOut = false; |
|
66 |
||
67 |
/** |
|
68 |
* The implementation of this Socket. |
|
69 |
*/ |
|
70 |
SocketImpl impl; |
|
71 |
||
72 |
/** |
|
73 |
* Are we using an older SocketImpl? |
|
74 |
*/ |
|
75 |
private boolean oldImpl = false; |
|
76 |
||
77 |
/** |
|
78 |
* Creates an unconnected socket, with the |
|
79 |
* system-default type of SocketImpl. |
|
80 |
* |
|
81 |
* @since JDK1.1 |
|
82 |
* @revised 1.4 |
|
83 |
*/ |
|
84 |
public Socket() { |
|
85 |
setImpl(); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Creates an unconnected socket, specifying the type of proxy, if any, |
|
90 |
* that should be used regardless of any other settings. |
|
91 |
* <P> |
|
92 |
* If there is a security manager, its <code>checkConnect</code> method |
|
93 |
* is called with the proxy host address and port number |
|
94 |
* as its arguments. This could result in a SecurityException. |
|
95 |
* <P> |
|
96 |
* Examples: |
|
97 |
* <UL> <LI><code>Socket s = new Socket(Proxy.NO_PROXY);</code> will create |
|
98 |
* a plain socket ignoring any other proxy configuration.</LI> |
|
99 |
* <LI><code>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));</code> |
|
100 |
* will create a socket connecting through the specified SOCKS proxy |
|
101 |
* server.</LI> |
|
102 |
* </UL> |
|
103 |
* |
|
104 |
* @param proxy a {@link java.net.Proxy Proxy} object specifying what kind |
|
105 |
* of proxying should be used. |
|
106 |
* @throws IllegalArgumentException if the proxy is of an invalid type |
|
107 |
* or <code>null</code>. |
|
108 |
* @throws SecurityException if a security manager is present and |
|
109 |
* permission to connect to the proxy is |
|
110 |
* denied. |
|
111 |
* @see java.net.ProxySelector |
|
112 |
* @see java.net.Proxy |
|
113 |
* |
|
114 |
* @since 1.5 |
|
115 |
*/ |
|
116 |
public Socket(Proxy proxy) { |
|
117 |
if (proxy != null && proxy.type() == Proxy.Type.SOCKS) { |
|
118 |
SecurityManager security = System.getSecurityManager(); |
|
119 |
InetSocketAddress epoint = (InetSocketAddress) proxy.address(); |
|
120 |
if (security != null) { |
|
121 |
if (epoint.isUnresolved()) |
|
122 |
security.checkConnect(epoint.getHostName(), |
|
123 |
epoint.getPort()); |
|
124 |
else |
|
125 |
security.checkConnect(epoint.getAddress().getHostAddress(), |
|
126 |
epoint.getPort()); |
|
127 |
} |
|
128 |
impl = new SocksSocketImpl(proxy); |
|
129 |
impl.setSocket(this); |
|
130 |
} else { |
|
131 |
if (proxy == Proxy.NO_PROXY) { |
|
132 |
if (factory == null) { |
|
133 |
impl = new PlainSocketImpl(); |
|
134 |
impl.setSocket(this); |
|
135 |
} else |
|
136 |
setImpl(); |
|
137 |
} else |
|
138 |
throw new IllegalArgumentException("Invalid Proxy"); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Creates an unconnected Socket with a user-specified |
|
144 |
* SocketImpl. |
|
145 |
* <P> |
|
146 |
* @param impl an instance of a <B>SocketImpl</B> |
|
147 |
* the subclass wishes to use on the Socket. |
|
148 |
* |
|
149 |
* @exception SocketException if there is an error in the underlying protocol, |
|
150 |
* such as a TCP error. |
|
151 |
* @since JDK1.1 |
|
152 |
*/ |
|
153 |
protected Socket(SocketImpl impl) throws SocketException { |
|
154 |
this.impl = impl; |
|
155 |
if (impl != null) { |
|
156 |
checkOldImpl(); |
|
157 |
this.impl.setSocket(this); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Creates a stream socket and connects it to the specified port |
|
163 |
* number on the named host. |
|
164 |
* <p> |
|
165 |
* If the specified host is <tt>null</tt> it is the equivalent of |
|
166 |
* specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
|
167 |
* In other words, it is equivalent to specifying an address of the |
|
168 |
* loopback interface. </p> |
|
169 |
* <p> |
|
170 |
* If the application has specified a server socket factory, that |
|
171 |
* factory's <code>createSocketImpl</code> method is called to create |
|
172 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
|
173 |
* <p> |
|
174 |
* If there is a security manager, its |
|
175 |
* <code>checkConnect</code> method is called |
|
176 |
* with the host address and <code>port</code> |
|
177 |
* as its arguments. This could result in a SecurityException. |
|
178 |
* |
|
179 |
* @param host the host name, or <code>null</code> for the loopback address. |
|
180 |
* @param port the port number. |
|
181 |
* |
|
182 |
* @exception UnknownHostException if the IP address of |
|
183 |
* the host could not be determined. |
|
184 |
* |
|
185 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
186 |
* @exception SecurityException if a security manager exists and its |
|
187 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
188 |
* @exception IllegalArgumentException if the port parameter is outside |
|
189 |
* the specified range of valid port values, which is between |
|
190 |
* 0 and 65535, inclusive. |
|
191 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
192 |
* @see java.net.SocketImpl |
|
193 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
194 |
* @see SecurityManager#checkConnect |
|
195 |
*/ |
|
196 |
public Socket(String host, int port) |
|
197 |
throws UnknownHostException, IOException |
|
198 |
{ |
|
199 |
this(host != null ? new InetSocketAddress(host, port) : |
|
200 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
201 |
(SocketAddress) null, true); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Creates a stream socket and connects it to the specified port |
|
206 |
* number at the specified IP address. |
|
207 |
* <p> |
|
208 |
* If the application has specified a socket factory, that factory's |
|
209 |
* <code>createSocketImpl</code> method is called to create the |
|
210 |
* actual socket implementation. Otherwise a "plain" socket is created. |
|
211 |
* <p> |
|
212 |
* If there is a security manager, its |
|
213 |
* <code>checkConnect</code> method is called |
|
214 |
* with the host address and <code>port</code> |
|
215 |
* as its arguments. This could result in a SecurityException. |
|
216 |
* |
|
217 |
* @param address the IP address. |
|
218 |
* @param port the port number. |
|
219 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
220 |
* @exception SecurityException if a security manager exists and its |
|
221 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
222 |
* @exception IllegalArgumentException if the port parameter is outside |
|
223 |
* the specified range of valid port values, which is between |
|
224 |
* 0 and 65535, inclusive. |
|
225 |
* @exception NullPointerException if <code>address</code> is null. |
|
226 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
227 |
* @see java.net.SocketImpl |
|
228 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
229 |
* @see SecurityManager#checkConnect |
|
230 |
*/ |
|
231 |
public Socket(InetAddress address, int port) throws IOException { |
|
232 |
this(address != null ? new InetSocketAddress(address, port) : null, |
|
233 |
(SocketAddress) null, true); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Creates a socket and connects it to the specified remote host on |
|
238 |
* the specified remote port. The Socket will also bind() to the local |
|
239 |
* address and port supplied. |
|
240 |
* <p> |
|
241 |
* If the specified host is <tt>null</tt> it is the equivalent of |
|
242 |
* specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
|
243 |
* In other words, it is equivalent to specifying an address of the |
|
244 |
* loopback interface. </p> |
|
245 |
* <p> |
|
246 |
* A local port number of <code>zero</code> will let the system pick up a |
|
247 |
* free port in the <code>bind</code> operation.</p> |
|
248 |
* <p> |
|
249 |
* If there is a security manager, its |
|
250 |
* <code>checkConnect</code> method is called |
|
251 |
* with the host address and <code>port</code> |
|
252 |
* as its arguments. This could result in a SecurityException. |
|
253 |
* |
|
254 |
* @param host the name of the remote host, or <code>null</code> for the loopback address. |
|
255 |
* @param port the remote port |
|
256 |
* @param localAddr the local address the socket is bound to |
|
257 |
* @param localPort the local port the socket is bound to, or |
|
258 |
* <code>zero</code> for a system selected free port. |
|
259 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
260 |
* @exception SecurityException if a security manager exists and its |
|
261 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
262 |
* @exception IllegalArgumentException if the port parameter or localPort |
|
263 |
* parameter is outside the specified range of valid port values, |
|
264 |
* which is between 0 and 65535, inclusive. |
|
265 |
* @see SecurityManager#checkConnect |
|
266 |
* @since JDK1.1 |
|
267 |
*/ |
|
268 |
public Socket(String host, int port, InetAddress localAddr, |
|
269 |
int localPort) throws IOException { |
|
270 |
this(host != null ? new InetSocketAddress(host, port) : |
|
271 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
272 |
new InetSocketAddress(localAddr, localPort), true); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Creates a socket and connects it to the specified remote address on |
|
277 |
* the specified remote port. The Socket will also bind() to the local |
|
278 |
* address and port supplied. |
|
279 |
* <p> |
|
280 |
* If the specified local address is <tt>null</tt> it is the equivalent of |
|
281 |
* specifying the address as the AnyLocal address (see <tt>{@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}()</tt>). |
|
282 |
* <p> |
|
283 |
* A local port number of <code>zero</code> will let the system pick up a |
|
284 |
* free port in the <code>bind</code> operation.</p> |
|
285 |
* <p> |
|
286 |
* If there is a security manager, its |
|
287 |
* <code>checkConnect</code> method is called |
|
288 |
* with the host address and <code>port</code> |
|
289 |
* as its arguments. This could result in a SecurityException. |
|
290 |
* |
|
291 |
* @param address the remote address |
|
292 |
* @param port the remote port |
|
293 |
* @param localAddr the local address the socket is bound to, or |
|
294 |
* <code>null</code> for the <code>anyLocal</code> address. |
|
295 |
* @param localPort the local port the socket is bound to or |
|
296 |
* <code>zero</code> for a system selected free port. |
|
297 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
298 |
* @exception SecurityException if a security manager exists and its |
|
299 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
300 |
* @exception IllegalArgumentException if the port parameter or localPort |
|
301 |
* parameter is outside the specified range of valid port values, |
|
302 |
* which is between 0 and 65535, inclusive. |
|
303 |
* @exception NullPointerException if <code>address</code> is null. |
|
304 |
* @see SecurityManager#checkConnect |
|
305 |
* @since JDK1.1 |
|
306 |
*/ |
|
307 |
public Socket(InetAddress address, int port, InetAddress localAddr, |
|
308 |
int localPort) throws IOException { |
|
309 |
this(address != null ? new InetSocketAddress(address, port) : null, |
|
310 |
new InetSocketAddress(localAddr, localPort), true); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Creates a stream socket and connects it to the specified port |
|
315 |
* number on the named host. |
|
316 |
* <p> |
|
317 |
* If the specified host is <tt>null</tt> it is the equivalent of |
|
318 |
* specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
|
319 |
* In other words, it is equivalent to specifying an address of the |
|
320 |
* loopback interface. </p> |
|
321 |
* <p> |
|
322 |
* If the stream argument is <code>true</code>, this creates a |
|
323 |
* stream socket. If the stream argument is <code>false</code>, it |
|
324 |
* creates a datagram socket. |
|
325 |
* <p> |
|
326 |
* If the application has specified a server socket factory, that |
|
327 |
* factory's <code>createSocketImpl</code> method is called to create |
|
328 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
|
329 |
* <p> |
|
330 |
* If there is a security manager, its |
|
331 |
* <code>checkConnect</code> method is called |
|
332 |
* with the host address and <code>port</code> |
|
333 |
* as its arguments. This could result in a SecurityException. |
|
334 |
* <p> |
|
335 |
* If a UDP socket is used, TCP/IP related socket options will not apply. |
|
336 |
* |
|
337 |
* @param host the host name, or <code>null</code> for the loopback address. |
|
338 |
* @param port the port number. |
|
339 |
* @param stream a <code>boolean</code> indicating whether this is |
|
340 |
* a stream socket or a datagram socket. |
|
341 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
342 |
* @exception SecurityException if a security manager exists and its |
|
343 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
344 |
* @exception IllegalArgumentException if the port parameter is outside |
|
345 |
* the specified range of valid port values, which is between |
|
346 |
* 0 and 65535, inclusive. |
|
347 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
348 |
* @see java.net.SocketImpl |
|
349 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
350 |
* @see SecurityManager#checkConnect |
|
351 |
* @deprecated Use DatagramSocket instead for UDP transport. |
|
352 |
*/ |
|
353 |
@Deprecated |
|
354 |
public Socket(String host, int port, boolean stream) throws IOException { |
|
355 |
this(host != null ? new InetSocketAddress(host, port) : |
|
356 |
new InetSocketAddress(InetAddress.getByName(null), port), |
|
357 |
(SocketAddress) null, stream); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Creates a socket and connects it to the specified port number at |
|
362 |
* the specified IP address. |
|
363 |
* <p> |
|
364 |
* If the stream argument is <code>true</code>, this creates a |
|
365 |
* stream socket. If the stream argument is <code>false</code>, it |
|
366 |
* creates a datagram socket. |
|
367 |
* <p> |
|
368 |
* If the application has specified a server socket factory, that |
|
369 |
* factory's <code>createSocketImpl</code> method is called to create |
|
370 |
* the actual socket implementation. Otherwise a "plain" socket is created. |
|
371 |
* |
|
372 |
* <p>If there is a security manager, its |
|
373 |
* <code>checkConnect</code> method is called |
|
374 |
* with <code>host.getHostAddress()</code> and <code>port</code> |
|
375 |
* as its arguments. This could result in a SecurityException. |
|
376 |
* <p> |
|
377 |
* If UDP socket is used, TCP/IP related socket options will not apply. |
|
378 |
* |
|
379 |
* @param host the IP address. |
|
380 |
* @param port the port number. |
|
381 |
* @param stream if <code>true</code>, create a stream socket; |
|
382 |
* otherwise, create a datagram socket. |
|
383 |
* @exception IOException if an I/O error occurs when creating the socket. |
|
384 |
* @exception SecurityException if a security manager exists and its |
|
385 |
* <code>checkConnect</code> method doesn't allow the operation. |
|
386 |
* @exception IllegalArgumentException if the port parameter is outside |
|
387 |
* the specified range of valid port values, which is between |
|
388 |
* 0 and 65535, inclusive. |
|
389 |
* @exception NullPointerException if <code>host</code> is null. |
|
390 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
391 |
* @see java.net.SocketImpl |
|
392 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
393 |
* @see SecurityManager#checkConnect |
|
394 |
* @deprecated Use DatagramSocket instead for UDP transport. |
|
395 |
*/ |
|
396 |
@Deprecated |
|
397 |
public Socket(InetAddress host, int port, boolean stream) throws IOException { |
|
398 |
this(host != null ? new InetSocketAddress(host, port) : null, |
|
399 |
new InetSocketAddress(0), stream); |
|
400 |
} |
|
401 |
||
402 |
private Socket(SocketAddress address, SocketAddress localAddr, |
|
403 |
boolean stream) throws IOException { |
|
404 |
setImpl(); |
|
405 |
||
406 |
// backward compatibility |
|
407 |
if (address == null) |
|
408 |
throw new NullPointerException(); |
|
409 |
||
410 |
try { |
|
411 |
createImpl(stream); |
|
412 |
if (localAddr != null) |
|
413 |
bind(localAddr); |
|
414 |
if (address != null) |
|
415 |
connect(address); |
|
416 |
} catch (IOException e) { |
|
417 |
close(); |
|
418 |
throw e; |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* Creates the socket implementation. |
|
424 |
* |
|
425 |
* @param stream a <code>boolean</code> value : <code>true</code> for a TCP socket, |
|
426 |
* <code>false</code> for UDP. |
|
427 |
* @throws IOException if creation fails |
|
428 |
* @since 1.4 |
|
429 |
*/ |
|
430 |
void createImpl(boolean stream) throws SocketException { |
|
431 |
if (impl == null) |
|
432 |
setImpl(); |
|
433 |
try { |
|
434 |
impl.create(stream); |
|
435 |
created = true; |
|
436 |
} catch (IOException e) { |
|
437 |
throw new SocketException(e.getMessage()); |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
private void checkOldImpl() { |
|
442 |
if (impl == null) |
|
443 |
return; |
|
444 |
// SocketImpl.connect() is a protected method, therefore we need to use |
|
445 |
// getDeclaredMethod, therefore we need permission to access the member |
|
446 |
||
447 |
oldImpl = AccessController.doPrivileged |
|
448 |
(new PrivilegedAction<Boolean>() { |
|
449 |
public Boolean run() { |
|
450 |
Class[] cl = new Class[2]; |
|
451 |
cl[0] = SocketAddress.class; |
|
452 |
cl[1] = Integer.TYPE; |
|
453 |
Class clazz = impl.getClass(); |
|
454 |
while (true) { |
|
455 |
try { |
|
456 |
clazz.getDeclaredMethod("connect", cl); |
|
457 |
return Boolean.FALSE; |
|
458 |
} catch (NoSuchMethodException e) { |
|
459 |
clazz = clazz.getSuperclass(); |
|
460 |
// java.net.SocketImpl class will always have this abstract method. |
|
461 |
// If we have not found it by now in the hierarchy then it does not |
|
462 |
// exist, we are an old style impl. |
|
463 |
if (clazz.equals(java.net.SocketImpl.class)) { |
|
464 |
return Boolean.TRUE; |
|
465 |
} |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
}); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Sets impl to the system-default type of SocketImpl. |
|
474 |
* @since 1.4 |
|
475 |
*/ |
|
476 |
void setImpl() { |
|
477 |
if (factory != null) { |
|
478 |
impl = factory.createSocketImpl(); |
|
479 |
checkOldImpl(); |
|
480 |
} else { |
|
481 |
// No need to do a checkOldImpl() here, we know it's an up to date |
|
482 |
// SocketImpl! |
|
483 |
impl = new SocksSocketImpl(); |
|
484 |
} |
|
485 |
if (impl != null) |
|
486 |
impl.setSocket(this); |
|
487 |
} |
|
488 |
||
489 |
||
490 |
/** |
|
491 |
* Get the <code>SocketImpl</code> attached to this socket, creating |
|
492 |
* it if necessary. |
|
493 |
* |
|
494 |
* @return the <code>SocketImpl</code> attached to that ServerSocket. |
|
495 |
* @throws SocketException if creation fails |
|
496 |
* @since 1.4 |
|
497 |
*/ |
|
498 |
SocketImpl getImpl() throws SocketException { |
|
499 |
if (!created) |
|
500 |
createImpl(true); |
|
501 |
return impl; |
|
502 |
} |
|
503 |
||
504 |
/** |
|
505 |
* Connects this socket to the server. |
|
506 |
* |
|
507 |
* @param endpoint the <code>SocketAddress</code> |
|
508 |
* @throws IOException if an error occurs during the connection |
|
509 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
510 |
* if this socket has an associated channel, |
|
511 |
* and the channel is in non-blocking mode |
|
512 |
* @throws IllegalArgumentException if endpoint is null or is a |
|
513 |
* SocketAddress subclass not supported by this socket |
|
514 |
* @since 1.4 |
|
515 |
* @spec JSR-51 |
|
516 |
*/ |
|
517 |
public void connect(SocketAddress endpoint) throws IOException { |
|
518 |
connect(endpoint, 0); |
|
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* Connects this socket to the server with a specified timeout value. |
|
523 |
* A timeout of zero is interpreted as an infinite timeout. The connection |
|
524 |
* will then block until established or an error occurs. |
|
525 |
* |
|
526 |
* @param endpoint the <code>SocketAddress</code> |
|
527 |
* @param timeout the timeout value to be used in milliseconds. |
|
528 |
* @throws IOException if an error occurs during the connection |
|
529 |
* @throws SocketTimeoutException if timeout expires before connecting |
|
530 |
* @throws java.nio.channels.IllegalBlockingModeException |
|
531 |
* if this socket has an associated channel, |
|
532 |
* and the channel is in non-blocking mode |
|
533 |
* @throws IllegalArgumentException if endpoint is null or is a |
|
534 |
* SocketAddress subclass not supported by this socket |
|
535 |
* @since 1.4 |
|
536 |
* @spec JSR-51 |
|
537 |
*/ |
|
538 |
public void connect(SocketAddress endpoint, int timeout) throws IOException { |
|
539 |
if (endpoint == null) |
|
540 |
throw new IllegalArgumentException("connect: The address can't be null"); |
|
541 |
||
542 |
if (timeout < 0) |
|
543 |
throw new IllegalArgumentException("connect: timeout can't be negative"); |
|
544 |
||
545 |
if (isClosed()) |
|
546 |
throw new SocketException("Socket is closed"); |
|
547 |
||
548 |
if (!oldImpl && isConnected()) |
|
549 |
throw new SocketException("already connected"); |
|
550 |
||
551 |
if (!(endpoint instanceof InetSocketAddress)) |
|
552 |
throw new IllegalArgumentException("Unsupported address type"); |
|
553 |
||
554 |
InetSocketAddress epoint = (InetSocketAddress) endpoint; |
|
555 |
||
556 |
SecurityManager security = System.getSecurityManager(); |
|
557 |
if (security != null) { |
|
558 |
if (epoint.isUnresolved()) |
|
559 |
security.checkConnect(epoint.getHostName(), |
|
560 |
epoint.getPort()); |
|
561 |
else |
|
562 |
security.checkConnect(epoint.getAddress().getHostAddress(), |
|
563 |
epoint.getPort()); |
|
564 |
} |
|
565 |
if (!created) |
|
566 |
createImpl(true); |
|
567 |
if (!oldImpl) |
|
568 |
impl.connect(epoint, timeout); |
|
569 |
else if (timeout == 0) { |
|
570 |
if (epoint.isUnresolved()) |
|
571 |
impl.connect(epoint.getAddress().getHostName(), |
|
572 |
epoint.getPort()); |
|
573 |
else |
|
574 |
impl.connect(epoint.getAddress(), epoint.getPort()); |
|
575 |
} else |
|
576 |
throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)"); |
|
577 |
connected = true; |
|
578 |
/* |
|
579 |
* If the socket was not bound before the connect, it is now because |
|
580 |
* the kernel will have picked an ephemeral port & a local address |
|
581 |
*/ |
|
582 |
bound = true; |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* Binds the socket to a local address. |
|
587 |
* <P> |
|
588 |
* If the address is <code>null</code>, then the system will pick up |
|
589 |
* an ephemeral port and a valid local address to bind the socket. |
|
590 |
* |
|
591 |
* @param bindpoint the <code>SocketAddress</code> to bind to |
|
592 |
* @throws IOException if the bind operation fails, or if the socket |
|
593 |
* is already bound. |
|
594 |
* @throws IllegalArgumentException if bindpoint is a |
|
595 |
* SocketAddress subclass not supported by this socket |
|
596 |
* |
|
597 |
* @since 1.4 |
|
598 |
* @see #isBound |
|
599 |
*/ |
|
600 |
public void bind(SocketAddress bindpoint) throws IOException { |
|
601 |
if (isClosed()) |
|
602 |
throw new SocketException("Socket is closed"); |
|
603 |
if (!oldImpl && isBound()) |
|
604 |
throw new SocketException("Already bound"); |
|
605 |
||
606 |
if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress))) |
|
607 |
throw new IllegalArgumentException("Unsupported address type"); |
|
608 |
InetSocketAddress epoint = (InetSocketAddress) bindpoint; |
|
609 |
if (epoint != null && epoint.isUnresolved()) |
|
610 |
throw new SocketException("Unresolved address"); |
|
611 |
if (bindpoint == null) |
|
612 |
getImpl().bind(InetAddress.anyLocalAddress(), 0); |
|
613 |
else |
|
614 |
getImpl().bind(epoint.getAddress(), |
|
615 |
epoint.getPort()); |
|
616 |
bound = true; |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* set the flags after an accept() call. |
|
621 |
*/ |
|
622 |
final void postAccept() { |
|
623 |
connected = true; |
|
624 |
created = true; |
|
625 |
bound = true; |
|
626 |
} |
|
627 |
||
628 |
void setCreated() { |
|
629 |
created = true; |
|
630 |
} |
|
631 |
||
632 |
void setBound() { |
|
633 |
bound = true; |
|
634 |
} |
|
635 |
||
636 |
void setConnected() { |
|
637 |
connected = true; |
|
638 |
} |
|
639 |
||
640 |
/** |
|
641 |
* Returns the address to which the socket is connected. |
|
642 |
* <p> |
|
643 |
* If the socket was connected prior to being {@link #close closed}, |
|
644 |
* then this method will continue to return the connected address |
|
645 |
* after the socket is closed. |
|
646 |
* |
|
647 |
* @return the remote IP address to which this socket is connected, |
|
648 |
* or <code>null</code> if the socket is not connected. |
|
649 |
*/ |
|
650 |
public InetAddress getInetAddress() { |
|
651 |
if (!isConnected()) |
|
652 |
return null; |
|
653 |
try { |
|
654 |
return getImpl().getInetAddress(); |
|
655 |
} catch (SocketException e) { |
|
656 |
} |
|
657 |
return null; |
|
658 |
} |
|
659 |
||
660 |
/** |
|
661 |
* Gets the local address to which the socket is bound. |
|
662 |
* |
|
663 |
* @return the local address to which the socket is bound, or |
|
664 |
* the {@link InetAddress#isAnyLocalAddress wildcard} address |
|
665 |
* if the socket is closed or not bound yet. |
|
666 |
* @since JDK1.1 |
|
667 |
*/ |
|
668 |
public InetAddress getLocalAddress() { |
|
669 |
// This is for backward compatibility |
|
670 |
if (!isBound()) |
|
671 |
return InetAddress.anyLocalAddress(); |
|
672 |
InetAddress in = null; |
|
673 |
try { |
|
674 |
in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR); |
|
675 |
if (in.isAnyLocalAddress()) { |
|
676 |
in = InetAddress.anyLocalAddress(); |
|
677 |
} |
|
678 |
} catch (Exception e) { |
|
679 |
in = InetAddress.anyLocalAddress(); // "0.0.0.0" |
|
680 |
} |
|
681 |
return in; |
|
682 |
} |
|
683 |
||
684 |
/** |
|
685 |
* Returns the remote port number to which this socket is connected. |
|
686 |
* <p> |
|
687 |
* If the socket was connected prior to being {@link #close closed}, |
|
688 |
* then this method will continue to return the connected port number |
|
689 |
* after the socket is closed. |
|
690 |
* |
|
691 |
* @return the remote port number to which this socket is connected, or |
|
692 |
* 0 if the socket is not connected yet. |
|
693 |
*/ |
|
694 |
public int getPort() { |
|
695 |
if (!isConnected()) |
|
696 |
return 0; |
|
697 |
try { |
|
698 |
return getImpl().getPort(); |
|
699 |
} catch (SocketException e) { |
|
700 |
// Shouldn't happen as we're connected |
|
701 |
} |
|
702 |
return -1; |
|
703 |
} |
|
704 |
||
705 |
/** |
|
706 |
* Returns the local port number to which this socket is bound. |
|
707 |
* <p> |
|
708 |
* If the socket was bound prior to being {@link #close closed}, |
|
709 |
* then this method will continue to return the local port number |
|
710 |
* after the socket is closed. |
|
711 |
* |
|
712 |
* @return the local port number to which this socket is bound or -1 |
|
713 |
* if the socket is not bound yet. |
|
714 |
*/ |
|
715 |
public int getLocalPort() { |
|
716 |
if (!isBound()) |
|
717 |
return -1; |
|
718 |
try { |
|
719 |
return getImpl().getLocalPort(); |
|
720 |
} catch(SocketException e) { |
|
721 |
// shouldn't happen as we're bound |
|
722 |
} |
|
723 |
return -1; |
|
724 |
} |
|
725 |
||
726 |
/** |
|
727 |
* Returns the address of the endpoint this socket is connected to, or |
|
728 |
* <code>null</code> if it is unconnected. |
|
729 |
* <p> |
|
730 |
* If the socket was connected prior to being {@link #close closed}, |
|
731 |
* then this method will continue to return the connected address |
|
732 |
* after the socket is closed. |
|
733 |
* |
|
94 | 734 |
|
81
c2d2c408d6ed
6667108: typo in javadoc for java.net.Socket.getRemoteSocketAddress()
chegar
parents:
2
diff
changeset
|
735 |
* @return a <code>SocketAddress</code> representing the remote endpoint of this |
2 | 736 |
* socket, or <code>null</code> if it is not connected yet. |
737 |
* @see #getInetAddress() |
|
738 |
* @see #getPort() |
|
739 |
* @see #connect(SocketAddress, int) |
|
740 |
* @see #connect(SocketAddress) |
|
741 |
* @since 1.4 |
|
742 |
*/ |
|
743 |
public SocketAddress getRemoteSocketAddress() { |
|
744 |
if (!isConnected()) |
|
745 |
return null; |
|
746 |
return new InetSocketAddress(getInetAddress(), getPort()); |
|
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Returns the address of the endpoint this socket is bound to, or |
|
751 |
* <code>null</code> if it is not bound yet. |
|
752 |
* <p> |
|
753 |
* If a socket bound to an endpoint represented by an |
|
754 |
* <code>InetSocketAddress </code> is {@link #close closed}, |
|
755 |
* then this method will continue to return an <code>InetSocketAddress</code> |
|
756 |
* after the socket is closed. In that case the returned |
|
757 |
* <code>InetSocketAddress</code>'s address is the |
|
758 |
* {@link InetAddress#isAnyLocalAddress wildcard} address |
|
759 |
* and its port is the local port that it was bound to. |
|
760 |
* |
|
761 |
* @return a <code>SocketAddress</code> representing the local endpoint of this |
|
762 |
* socket, or <code>null</code> if it is not bound yet. |
|
763 |
* @see #getLocalAddress() |
|
764 |
* @see #getLocalPort() |
|
765 |
* @see #bind(SocketAddress) |
|
766 |
* @since 1.4 |
|
767 |
*/ |
|
768 |
||
769 |
public SocketAddress getLocalSocketAddress() { |
|
770 |
if (!isBound()) |
|
771 |
return null; |
|
772 |
return new InetSocketAddress(getLocalAddress(), getLocalPort()); |
|
773 |
} |
|
774 |
||
775 |
/** |
|
776 |
* Returns the unique {@link java.nio.channels.SocketChannel SocketChannel} |
|
777 |
* object associated with this socket, if any. |
|
778 |
* |
|
779 |
* <p> A socket will have a channel if, and only if, the channel itself was |
|
780 |
* created via the {@link java.nio.channels.SocketChannel#open |
|
781 |
* SocketChannel.open} or {@link |
|
782 |
* java.nio.channels.ServerSocketChannel#accept ServerSocketChannel.accept} |
|
783 |
* methods. |
|
784 |
* |
|
785 |
* @return the socket channel associated with this socket, |
|
786 |
* or <tt>null</tt> if this socket was not created |
|
787 |
* for a channel |
|
788 |
* |
|
789 |
* @since 1.4 |
|
790 |
* @spec JSR-51 |
|
791 |
*/ |
|
792 |
public SocketChannel getChannel() { |
|
793 |
return null; |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Returns an input stream for this socket. |
|
798 |
* |
|
799 |
* <p> If this socket has an associated channel then the resulting input |
|
800 |
* stream delegates all of its operations to the channel. If the channel |
|
801 |
* is in non-blocking mode then the input stream's <tt>read</tt> operations |
|
802 |
* will throw an {@link java.nio.channels.IllegalBlockingModeException}. |
|
803 |
* |
|
804 |
* <p>Under abnormal conditions the underlying connection may be |
|
805 |
* broken by the remote host or the network software (for example |
|
806 |
* a connection reset in the case of TCP connections). When a |
|
807 |
* broken connection is detected by the network software the |
|
808 |
* following applies to the returned input stream :- |
|
809 |
* |
|
810 |
* <ul> |
|
811 |
* |
|
812 |
* <li><p>The network software may discard bytes that are buffered |
|
813 |
* by the socket. Bytes that aren't discarded by the network |
|
814 |
* software can be read using {@link java.io.InputStream#read read}. |
|
815 |
* |
|
816 |
* <li><p>If there are no bytes buffered on the socket, or all |
|
817 |
* buffered bytes have been consumed by |
|
818 |
* {@link java.io.InputStream#read read}, then all subsequent |
|
819 |
* calls to {@link java.io.InputStream#read read} will throw an |
|
820 |
* {@link java.io.IOException IOException}. |
|
821 |
* |
|
822 |
* <li><p>If there are no bytes buffered on the socket, and the |
|
823 |
* socket has not been closed using {@link #close close}, then |
|
824 |
* {@link java.io.InputStream#available available} will |
|
825 |
* return <code>0</code>. |
|
826 |
* |
|
827 |
* </ul> |
|
828 |
* |
|
829 |
* <p> Closing the returned {@link java.io.InputStream InputStream} |
|
830 |
* will close the associated socket. |
|
831 |
* |
|
832 |
* @return an input stream for reading bytes from this socket. |
|
833 |
* @exception IOException if an I/O error occurs when creating the |
|
834 |
* input stream, the socket is closed, the socket is |
|
835 |
* not connected, or the socket input has been shutdown |
|
836 |
* using {@link #shutdownInput()} |
|
837 |
* |
|
838 |
* @revised 1.4 |
|
839 |
* @spec JSR-51 |
|
840 |
*/ |
|
841 |
public InputStream getInputStream() throws IOException { |
|
842 |
if (isClosed()) |
|
843 |
throw new SocketException("Socket is closed"); |
|
844 |
if (!isConnected()) |
|
845 |
throw new SocketException("Socket is not connected"); |
|
846 |
if (isInputShutdown()) |
|
847 |
throw new SocketException("Socket input is shutdown"); |
|
848 |
final Socket s = this; |
|
849 |
InputStream is = null; |
|
850 |
try { |
|
51 | 851 |
is = AccessController.doPrivileged( |
852 |
new PrivilegedExceptionAction<InputStream>() { |
|
853 |
public InputStream run() throws IOException { |
|
2 | 854 |
return impl.getInputStream(); |
855 |
} |
|
856 |
}); |
|
857 |
} catch (java.security.PrivilegedActionException e) { |
|
858 |
throw (IOException) e.getException(); |
|
859 |
} |
|
860 |
return is; |
|
861 |
} |
|
862 |
||
863 |
/** |
|
864 |
* Returns an output stream for this socket. |
|
865 |
* |
|
866 |
* <p> If this socket has an associated channel then the resulting output |
|
867 |
* stream delegates all of its operations to the channel. If the channel |
|
868 |
* is in non-blocking mode then the output stream's <tt>write</tt> |
|
869 |
* operations will throw an {@link |
|
870 |
* java.nio.channels.IllegalBlockingModeException}. |
|
871 |
* |
|
872 |
* <p> Closing the returned {@link java.io.OutputStream OutputStream} |
|
873 |
* will close the associated socket. |
|
874 |
* |
|
875 |
* @return an output stream for writing bytes to this socket. |
|
876 |
* @exception IOException if an I/O error occurs when creating the |
|
877 |
* output stream or if the socket is not connected. |
|
878 |
* @revised 1.4 |
|
879 |
* @spec JSR-51 |
|
880 |
*/ |
|
881 |
public OutputStream getOutputStream() throws IOException { |
|
882 |
if (isClosed()) |
|
883 |
throw new SocketException("Socket is closed"); |
|
884 |
if (!isConnected()) |
|
885 |
throw new SocketException("Socket is not connected"); |
|
886 |
if (isOutputShutdown()) |
|
887 |
throw new SocketException("Socket output is shutdown"); |
|
888 |
final Socket s = this; |
|
889 |
OutputStream os = null; |
|
890 |
try { |
|
51 | 891 |
os = AccessController.doPrivileged( |
892 |
new PrivilegedExceptionAction<OutputStream>() { |
|
893 |
public OutputStream run() throws IOException { |
|
2 | 894 |
return impl.getOutputStream(); |
895 |
} |
|
896 |
}); |
|
897 |
} catch (java.security.PrivilegedActionException e) { |
|
898 |
throw (IOException) e.getException(); |
|
899 |
} |
|
900 |
return os; |
|
901 |
} |
|
902 |
||
903 |
/** |
|
904 |
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). |
|
905 |
* |
|
906 |
* @param on <code>true</code> to enable TCP_NODELAY, |
|
907 |
* <code>false</code> to disable. |
|
908 |
* |
|
909 |
* @exception SocketException if there is an error |
|
910 |
* in the underlying protocol, such as a TCP error. |
|
911 |
* |
|
912 |
* @since JDK1.1 |
|
913 |
* |
|
914 |
* @see #getTcpNoDelay() |
|
915 |
*/ |
|
916 |
public void setTcpNoDelay(boolean on) throws SocketException { |
|
917 |
if (isClosed()) |
|
918 |
throw new SocketException("Socket is closed"); |
|
919 |
getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on)); |
|
920 |
} |
|
921 |
||
922 |
/** |
|
923 |
* Tests if TCP_NODELAY is enabled. |
|
924 |
* |
|
925 |
* @return a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled. |
|
926 |
* @exception SocketException if there is an error |
|
927 |
* in the underlying protocol, such as a TCP error. |
|
928 |
* @since JDK1.1 |
|
929 |
* @see #setTcpNoDelay(boolean) |
|
930 |
*/ |
|
931 |
public boolean getTcpNoDelay() throws SocketException { |
|
932 |
if (isClosed()) |
|
933 |
throw new SocketException("Socket is closed"); |
|
934 |
return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue(); |
|
935 |
} |
|
936 |
||
937 |
/** |
|
938 |
* Enable/disable SO_LINGER with the specified linger time in seconds. |
|
939 |
* The maximum timeout value is platform specific. |
|
940 |
* |
|
941 |
* The setting only affects socket close. |
|
942 |
* |
|
943 |
* @param on whether or not to linger on. |
|
944 |
* @param linger how long to linger for, if on is true. |
|
945 |
* @exception SocketException if there is an error |
|
946 |
* in the underlying protocol, such as a TCP error. |
|
947 |
* @exception IllegalArgumentException if the linger value is negative. |
|
948 |
* @since JDK1.1 |
|
949 |
* @see #getSoLinger() |
|
950 |
*/ |
|
951 |
public void setSoLinger(boolean on, int linger) throws SocketException { |
|
952 |
if (isClosed()) |
|
953 |
throw new SocketException("Socket is closed"); |
|
954 |
if (!on) { |
|
955 |
getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on)); |
|
956 |
} else { |
|
957 |
if (linger < 0) { |
|
958 |
throw new IllegalArgumentException("invalid value for SO_LINGER"); |
|
959 |
} |
|
960 |
if (linger > 65535) |
|
961 |
linger = 65535; |
|
962 |
getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger)); |
|
963 |
} |
|
964 |
} |
|
965 |
||
966 |
/** |
|
967 |
* Returns setting for SO_LINGER. -1 returns implies that the |
|
968 |
* option is disabled. |
|
969 |
* |
|
970 |
* The setting only affects socket close. |
|
971 |
* |
|
972 |
* @return the setting for SO_LINGER. |
|
973 |
* @exception SocketException if there is an error |
|
974 |
* in the underlying protocol, such as a TCP error. |
|
975 |
* @since JDK1.1 |
|
976 |
* @see #setSoLinger(boolean, int) |
|
977 |
*/ |
|
978 |
public int getSoLinger() throws SocketException { |
|
979 |
if (isClosed()) |
|
980 |
throw new SocketException("Socket is closed"); |
|
981 |
Object o = getImpl().getOption(SocketOptions.SO_LINGER); |
|
982 |
if (o instanceof Integer) { |
|
983 |
return ((Integer) o).intValue(); |
|
984 |
} else { |
|
985 |
return -1; |
|
986 |
} |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* Send one byte of urgent data on the socket. The byte to be sent is the lowest eight |
|
991 |
* bits of the data parameter. The urgent byte is |
|
992 |
* sent after any preceding writes to the socket OutputStream |
|
993 |
* and before any future writes to the OutputStream. |
|
994 |
* @param data The byte of data to send |
|
995 |
* @exception IOException if there is an error |
|
996 |
* sending the data. |
|
997 |
* @since 1.4 |
|
998 |
*/ |
|
999 |
public void sendUrgentData (int data) throws IOException { |
|
1000 |
if (!getImpl().supportsUrgentData ()) { |
|
1001 |
throw new SocketException ("Urgent data not supported"); |
|
1002 |
} |
|
1003 |
getImpl().sendUrgentData (data); |
|
1004 |
} |
|
1005 |
||
1006 |
/** |
|
1007 |
* Enable/disable OOBINLINE (receipt of TCP urgent data) |
|
1008 |
* |
|
1009 |
* By default, this option is disabled and TCP urgent data received on a |
|
1010 |
* socket is silently discarded. If the user wishes to receive urgent data, then |
|
1011 |
* this option must be enabled. When enabled, urgent data is received |
|
1012 |
* inline with normal data. |
|
1013 |
* <p> |
|
1014 |
* Note, only limited support is provided for handling incoming urgent |
|
1015 |
* data. In particular, no notification of incoming urgent data is provided |
|
1016 |
* and there is no capability to distinguish between normal data and urgent |
|
1017 |
* data unless provided by a higher level protocol. |
|
1018 |
* |
|
1019 |
* @param on <code>true</code> to enable OOBINLINE, |
|
1020 |
* <code>false</code> to disable. |
|
1021 |
* |
|
1022 |
* @exception SocketException if there is an error |
|
1023 |
* in the underlying protocol, such as a TCP error. |
|
1024 |
* |
|
1025 |
* @since 1.4 |
|
1026 |
* |
|
1027 |
* @see #getOOBInline() |
|
1028 |
*/ |
|
1029 |
public void setOOBInline(boolean on) throws SocketException { |
|
1030 |
if (isClosed()) |
|
1031 |
throw new SocketException("Socket is closed"); |
|
1032 |
getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on)); |
|
1033 |
} |
|
1034 |
||
1035 |
/** |
|
1036 |
* Tests if OOBINLINE is enabled. |
|
1037 |
* |
|
1038 |
* @return a <code>boolean</code> indicating whether or not OOBINLINE is enabled. |
|
1039 |
* @exception SocketException if there is an error |
|
1040 |
* in the underlying protocol, such as a TCP error. |
|
1041 |
* @since 1.4 |
|
1042 |
* @see #setOOBInline(boolean) |
|
1043 |
*/ |
|
1044 |
public boolean getOOBInline() throws SocketException { |
|
1045 |
if (isClosed()) |
|
1046 |
throw new SocketException("Socket is closed"); |
|
1047 |
return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue(); |
|
1048 |
} |
|
1049 |
||
1050 |
/** |
|
1051 |
* Enable/disable SO_TIMEOUT with the specified timeout, in |
|
1052 |
* milliseconds. With this option set to a non-zero timeout, |
|
1053 |
* a read() call on the InputStream associated with this Socket |
|
1054 |
* will block for only this amount of time. If the timeout expires, |
|
1055 |
* a <B>java.net.SocketTimeoutException</B> is raised, though the |
|
1056 |
* Socket is still valid. The option <B>must</B> be enabled |
|
1057 |
* prior to entering the blocking operation to have effect. The |
|
1058 |
* timeout must be > 0. |
|
1059 |
* A timeout of zero is interpreted as an infinite timeout. |
|
1060 |
* @param timeout the specified timeout, in milliseconds. |
|
1061 |
* @exception SocketException if there is an error |
|
1062 |
* in the underlying protocol, such as a TCP error. |
|
1063 |
* @since JDK 1.1 |
|
1064 |
* @see #getSoTimeout() |
|
1065 |
*/ |
|
1066 |
public synchronized void setSoTimeout(int timeout) throws SocketException { |
|
1067 |
if (isClosed()) |
|
1068 |
throw new SocketException("Socket is closed"); |
|
1069 |
if (timeout < 0) |
|
1070 |
throw new IllegalArgumentException("timeout can't be negative"); |
|
1071 |
||
1072 |
getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); |
|
1073 |
} |
|
1074 |
||
1075 |
/** |
|
1076 |
* Returns setting for SO_TIMEOUT. 0 returns implies that the |
|
1077 |
* option is disabled (i.e., timeout of infinity). |
|
1078 |
* @return the setting for SO_TIMEOUT |
|
1079 |
* @exception SocketException if there is an error |
|
1080 |
* in the underlying protocol, such as a TCP error. |
|
1081 |
* @since JDK1.1 |
|
1082 |
* @see #setSoTimeout(int) |
|
1083 |
*/ |
|
1084 |
public synchronized int getSoTimeout() throws SocketException { |
|
1085 |
if (isClosed()) |
|
1086 |
throw new SocketException("Socket is closed"); |
|
1087 |
Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT); |
|
1088 |
/* extra type safety */ |
|
1089 |
if (o instanceof Integer) { |
|
1090 |
return ((Integer) o).intValue(); |
|
1091 |
} else { |
|
1092 |
return 0; |
|
1093 |
} |
|
1094 |
} |
|
1095 |
||
1096 |
/** |
|
1097 |
* Sets the SO_SNDBUF option to the specified value for this |
|
1098 |
* <tt>Socket</tt>. The SO_SNDBUF option is used by the platform's |
|
1099 |
* networking code as a hint for the size to set |
|
1100 |
* the underlying network I/O buffers. |
|
1101 |
* |
|
1102 |
* <p>Because SO_SNDBUF is a hint, applications that want to |
|
1103 |
* verify what size the buffers were set to should call |
|
1104 |
* {@link #getSendBufferSize()}. |
|
1105 |
* |
|
1106 |
* @exception SocketException if there is an error |
|
1107 |
* in the underlying protocol, such as a TCP error. |
|
1108 |
* |
|
1109 |
* @param size the size to which to set the send buffer |
|
1110 |
* size. This value must be greater than 0. |
|
1111 |
* |
|
1112 |
* @exception IllegalArgumentException if the |
|
1113 |
* value is 0 or is negative. |
|
1114 |
* |
|
1115 |
* @see #getSendBufferSize() |
|
1116 |
* @since 1.2 |
|
1117 |
*/ |
|
1118 |
public synchronized void setSendBufferSize(int size) |
|
1119 |
throws SocketException{ |
|
1120 |
if (!(size > 0)) { |
|
1121 |
throw new IllegalArgumentException("negative send size"); |
|
1122 |
} |
|
1123 |
if (isClosed()) |
|
1124 |
throw new SocketException("Socket is closed"); |
|
1125 |
getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size)); |
|
1126 |
} |
|
1127 |
||
1128 |
/** |
|
1129 |
* Get value of the SO_SNDBUF option for this <tt>Socket</tt>, |
|
1130 |
* that is the buffer size used by the platform |
|
1131 |
* for output on this <tt>Socket</tt>. |
|
1132 |
* @return the value of the SO_SNDBUF option for this <tt>Socket</tt>. |
|
1133 |
* |
|
1134 |
* @exception SocketException if there is an error |
|
1135 |
* in the underlying protocol, such as a TCP error. |
|
1136 |
* |
|
1137 |
* @see #setSendBufferSize(int) |
|
1138 |
* @since 1.2 |
|
1139 |
*/ |
|
1140 |
public synchronized int getSendBufferSize() throws SocketException { |
|
1141 |
if (isClosed()) |
|
1142 |
throw new SocketException("Socket is closed"); |
|
1143 |
int result = 0; |
|
1144 |
Object o = getImpl().getOption(SocketOptions.SO_SNDBUF); |
|
1145 |
if (o instanceof Integer) { |
|
1146 |
result = ((Integer)o).intValue(); |
|
1147 |
} |
|
1148 |
return result; |
|
1149 |
} |
|
1150 |
||
1151 |
/** |
|
1152 |
* Sets the SO_RCVBUF option to the specified value for this |
|
1153 |
* <tt>Socket</tt>. The SO_RCVBUF option is used by the platform's |
|
1154 |
* networking code as a hint for the size to set |
|
1155 |
* the underlying network I/O buffers. |
|
1156 |
* |
|
1157 |
* <p>Increasing the receive buffer size can increase the performance of |
|
1158 |
* network I/O for high-volume connection, while decreasing it can |
|
1159 |
* help reduce the backlog of incoming data. |
|
1160 |
* |
|
1161 |
* <p>Because SO_RCVBUF is a hint, applications that want to |
|
1162 |
* verify what size the buffers were set to should call |
|
1163 |
* {@link #getReceiveBufferSize()}. |
|
1164 |
* |
|
1165 |
* <p>The value of SO_RCVBUF is also used to set the TCP receive window |
|
1166 |
* that is advertized to the remote peer. Generally, the window size |
|
1167 |
* can be modified at any time when a socket is connected. However, if |
|
1168 |
* a receive window larger than 64K is required then this must be requested |
|
1169 |
* <B>before</B> the socket is connected to the remote peer. There are two |
|
1170 |
* cases to be aware of:<p> |
|
1171 |
* <ol> |
|
1172 |
* <li>For sockets accepted from a ServerSocket, this must be done by calling |
|
1173 |
* {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket |
|
1174 |
* is bound to a local address.<p></li> |
|
1175 |
* <li>For client sockets, setReceiveBufferSize() must be called before |
|
1176 |
* connecting the socket to its remote peer.<p></li></ol> |
|
1177 |
* @param size the size to which to set the receive buffer |
|
1178 |
* size. This value must be greater than 0. |
|
1179 |
* |
|
1180 |
* @exception IllegalArgumentException if the value is 0 or is |
|
1181 |
* negative. |
|
1182 |
* |
|
1183 |
* @exception SocketException if there is an error |
|
1184 |
* in the underlying protocol, such as a TCP error. |
|
1185 |
* |
|
1186 |
* @see #getReceiveBufferSize() |
|
1187 |
* @see ServerSocket#setReceiveBufferSize(int) |
|
1188 |
* @since 1.2 |
|
1189 |
*/ |
|
1190 |
public synchronized void setReceiveBufferSize(int size) |
|
1191 |
throws SocketException{ |
|
1192 |
if (size <= 0) { |
|
1193 |
throw new IllegalArgumentException("invalid receive size"); |
|
1194 |
} |
|
1195 |
if (isClosed()) |
|
1196 |
throw new SocketException("Socket is closed"); |
|
1197 |
getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size)); |
|
1198 |
} |
|
1199 |
||
1200 |
/** |
|
1201 |
* Gets the value of the SO_RCVBUF option for this <tt>Socket</tt>, |
|
1202 |
* that is the buffer size used by the platform for |
|
1203 |
* input on this <tt>Socket</tt>. |
|
1204 |
* |
|
1205 |
* @return the value of the SO_RCVBUF option for this <tt>Socket</tt>. |
|
1206 |
* @exception SocketException if there is an error |
|
1207 |
* in the underlying protocol, such as a TCP error. |
|
1208 |
* @see #setReceiveBufferSize(int) |
|
1209 |
* @since 1.2 |
|
1210 |
*/ |
|
1211 |
public synchronized int getReceiveBufferSize() |
|
1212 |
throws SocketException{ |
|
1213 |
if (isClosed()) |
|
1214 |
throw new SocketException("Socket is closed"); |
|
1215 |
int result = 0; |
|
1216 |
Object o = getImpl().getOption(SocketOptions.SO_RCVBUF); |
|
1217 |
if (o instanceof Integer) { |
|
1218 |
result = ((Integer)o).intValue(); |
|
1219 |
} |
|
1220 |
return result; |
|
1221 |
} |
|
1222 |
||
1223 |
/** |
|
1224 |
* Enable/disable SO_KEEPALIVE. |
|
1225 |
* |
|
1226 |
* @param on whether or not to have socket keep alive turned on. |
|
1227 |
* @exception SocketException if there is an error |
|
1228 |
* in the underlying protocol, such as a TCP error. |
|
1229 |
* @since 1.3 |
|
1230 |
* @see #getKeepAlive() |
|
1231 |
*/ |
|
1232 |
public void setKeepAlive(boolean on) throws SocketException { |
|
1233 |
if (isClosed()) |
|
1234 |
throw new SocketException("Socket is closed"); |
|
1235 |
getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on)); |
|
1236 |
} |
|
1237 |
||
1238 |
/** |
|
1239 |
* Tests if SO_KEEPALIVE is enabled. |
|
1240 |
* |
|
1241 |
* @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is enabled. |
|
1242 |
* @exception SocketException if there is an error |
|
1243 |
* in the underlying protocol, such as a TCP error. |
|
1244 |
* @since 1.3 |
|
1245 |
* @see #setKeepAlive(boolean) |
|
1246 |
*/ |
|
1247 |
public boolean getKeepAlive() throws SocketException { |
|
1248 |
if (isClosed()) |
|
1249 |
throw new SocketException("Socket is closed"); |
|
1250 |
return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue(); |
|
1251 |
} |
|
1252 |
||
1253 |
/** |
|
1254 |
* Sets traffic class or type-of-service octet in the IP |
|
1255 |
* header for packets sent from this Socket. |
|
1256 |
* As the underlying network implementation may ignore this |
|
1257 |
* value applications should consider it a hint. |
|
1258 |
* |
|
1259 |
* <P> The tc <B>must</B> be in the range <code> 0 <= tc <= |
|
1260 |
* 255</code> or an IllegalArgumentException will be thrown. |
|
1261 |
* <p>Notes: |
|
1262 |
* <p>For Internet Protocol v4 the value consists of an |
|
1263 |
* <code>integer</code>, the least significant 8 bits of which |
|
1264 |
* represent the value of the TOS octet in IP packets sent by |
|
1265 |
* the socket. |
|
1266 |
* RFC 1349 defines the TOS values as follows: |
|
1267 |
* <p> |
|
1268 |
* <UL> |
|
1269 |
* <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI> |
|
1270 |
* <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI> |
|
1271 |
* <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI> |
|
1272 |
* <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI> |
|
1273 |
* </UL> |
|
1274 |
* The last low order bit is always ignored as this |
|
1275 |
* corresponds to the MBZ (must be zero) bit. |
|
1276 |
* <p> |
|
1277 |
* Setting bits in the precedence field may result in a |
|
1278 |
* SocketException indicating that the operation is not |
|
1279 |
* permitted. |
|
1280 |
* <p> |
|
1281 |
* As RFC 1122 section 4.2.4.2 indicates, a compliant TCP |
|
1282 |
* implementation should, but is not required to, let application |
|
1283 |
* change the TOS field during the lifetime of a connection. |
|
1284 |
* So whether the type-of-service field can be changed after the |
|
1285 |
* TCP connection has been established depends on the implementation |
|
1286 |
* in the underlying platform. Applications should not assume that |
|
1287 |
* they can change the TOS field after the connection. |
|
1288 |
* <p> |
|
1289 |
* For Internet Protocol v6 <code>tc</code> is the value that |
|
1290 |
* would be placed into the sin6_flowinfo field of the IP header. |
|
1291 |
* |
|
1292 |
* @param tc an <code>int</code> value for the bitset. |
|
1293 |
* @throws SocketException if there is an error setting the |
|
1294 |
* traffic class or type-of-service |
|
1295 |
* @since 1.4 |
|
1296 |
* @see #getTrafficClass |
|
1297 |
*/ |
|
1298 |
public void setTrafficClass(int tc) throws SocketException { |
|
1299 |
if (tc < 0 || tc > 255) |
|
1300 |
throw new IllegalArgumentException("tc is not in range 0 -- 255"); |
|
1301 |
||
1302 |
if (isClosed()) |
|
1303 |
throw new SocketException("Socket is closed"); |
|
1304 |
getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc)); |
|
1305 |
} |
|
1306 |
||
1307 |
/** |
|
1308 |
* Gets traffic class or type-of-service in the IP header |
|
1309 |
* for packets sent from this Socket |
|
1310 |
* <p> |
|
1311 |
* As the underlying network implementation may ignore the |
|
1312 |
* traffic class or type-of-service set using {@link #setTrafficClass(int)} |
|
1313 |
* this method may return a different value than was previously |
|
1314 |
* set using the {@link #setTrafficClass(int)} method on this Socket. |
|
1315 |
* |
|
1316 |
* @return the traffic class or type-of-service already set |
|
1317 |
* @throws SocketException if there is an error obtaining the |
|
1318 |
* traffic class or type-of-service value. |
|
1319 |
* @since 1.4 |
|
1320 |
* @see #setTrafficClass(int) |
|
1321 |
*/ |
|
1322 |
public int getTrafficClass() throws SocketException { |
|
1323 |
return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue(); |
|
1324 |
} |
|
1325 |
||
1326 |
/** |
|
1327 |
* Enable/disable the SO_REUSEADDR socket option. |
|
1328 |
* <p> |
|
1329 |
* When a TCP connection is closed the connection may remain |
|
1330 |
* in a timeout state for a period of time after the connection |
|
1331 |
* is closed (typically known as the <tt>TIME_WAIT</tt> state |
|
1332 |
* or <tt>2MSL</tt> wait state). |
|
1333 |
* For applications using a well known socket address or port |
|
1334 |
* it may not be possible to bind a socket to the required |
|
1335 |
* <tt>SocketAddress</tt> if there is a connection in the |
|
1336 |
* timeout state involving the socket address or port. |
|
1337 |
* <p> |
|
1338 |
* Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket |
|
1339 |
* using {@link #bind(SocketAddress)} allows the socket to be |
|
1340 |
* bound even though a previous connection is in a timeout |
|
1341 |
* state. |
|
1342 |
* <p> |
|
1343 |
* When a <tt>Socket</tt> is created the initial setting |
|
1344 |
* of <tt>SO_REUSEADDR</tt> is disabled. |
|
1345 |
* <p> |
|
1346 |
* The behaviour when <tt>SO_REUSEADDR</tt> is enabled or |
|
1347 |
* disabled after a socket is bound (See {@link #isBound()}) |
|
1348 |
* is not defined. |
|
1349 |
* |
|
1350 |
* @param on whether to enable or disable the socket option |
|
1351 |
* @exception SocketException if an error occurs enabling or |
|
1352 |
* disabling the <tt>SO_RESUEADDR</tt> socket option, |
|
1353 |
* or the socket is closed. |
|
1354 |
* @since 1.4 |
|
1355 |
* @see #getReuseAddress() |
|
1356 |
* @see #bind(SocketAddress) |
|
1357 |
* @see #isClosed() |
|
1358 |
* @see #isBound() |
|
1359 |
*/ |
|
1360 |
public void setReuseAddress(boolean on) throws SocketException { |
|
1361 |
if (isClosed()) |
|
1362 |
throw new SocketException("Socket is closed"); |
|
1363 |
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); |
|
1364 |
} |
|
1365 |
||
1366 |
/** |
|
1367 |
* Tests if SO_REUSEADDR is enabled. |
|
1368 |
* |
|
1369 |
* @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled. |
|
1370 |
* @exception SocketException if there is an error |
|
1371 |
* in the underlying protocol, such as a TCP error. |
|
1372 |
* @since 1.4 |
|
1373 |
* @see #setReuseAddress(boolean) |
|
1374 |
*/ |
|
1375 |
public boolean getReuseAddress() throws SocketException { |
|
1376 |
if (isClosed()) |
|
1377 |
throw new SocketException("Socket is closed"); |
|
1378 |
return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); |
|
1379 |
} |
|
1380 |
||
1381 |
/** |
|
1382 |
* Closes this socket. |
|
1383 |
* <p> |
|
1384 |
* Any thread currently blocked in an I/O operation upon this socket |
|
1385 |
* will throw a {@link SocketException}. |
|
1386 |
* <p> |
|
1387 |
* Once a socket has been closed, it is not available for further networking |
|
1388 |
* use (i.e. can't be reconnected or rebound). A new socket needs to be |
|
1389 |
* created. |
|
1390 |
* |
|
1391 |
* <p> Closing this socket will also close the socket's |
|
1392 |
* {@link java.io.InputStream InputStream} and |
|
1393 |
* {@link java.io.OutputStream OutputStream}. |
|
1394 |
* |
|
1395 |
* <p> If this socket has an associated channel then the channel is closed |
|
1396 |
* as well. |
|
1397 |
* |
|
1398 |
* @exception IOException if an I/O error occurs when closing this socket. |
|
1399 |
* @revised 1.4 |
|
1400 |
* @spec JSR-51 |
|
1401 |
* @see #isClosed |
|
1402 |
*/ |
|
1403 |
public synchronized void close() throws IOException { |
|
1404 |
synchronized(closeLock) { |
|
1405 |
if (isClosed()) |
|
1406 |
return; |
|
1407 |
if (created) |
|
1408 |
impl.close(); |
|
1409 |
closed = true; |
|
1410 |
} |
|
1411 |
} |
|
1412 |
||
1413 |
/** |
|
1414 |
* Places the input stream for this socket at "end of stream". |
|
1415 |
* Any data sent to the input stream side of the socket is acknowledged |
|
1416 |
* and then silently discarded. |
|
1417 |
* <p> |
|
1418 |
* If you read from a socket input stream after invoking |
|
1419 |
* shutdownInput() on the socket, the stream will return EOF. |
|
1420 |
* |
|
1421 |
* @exception IOException if an I/O error occurs when shutting down this |
|
1422 |
* socket. |
|
1423 |
* |
|
1424 |
* @since 1.3 |
|
1425 |
* @see java.net.Socket#shutdownOutput() |
|
1426 |
* @see java.net.Socket#close() |
|
1427 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1428 |
* @see #isInputShutdown |
|
1429 |
*/ |
|
1430 |
public void shutdownInput() throws IOException |
|
1431 |
{ |
|
1432 |
if (isClosed()) |
|
1433 |
throw new SocketException("Socket is closed"); |
|
1434 |
if (!isConnected()) |
|
1435 |
throw new SocketException("Socket is not connected"); |
|
1436 |
if (isInputShutdown()) |
|
1437 |
throw new SocketException("Socket input is already shutdown"); |
|
1438 |
getImpl().shutdownInput(); |
|
1439 |
shutIn = true; |
|
1440 |
} |
|
1441 |
||
1442 |
/** |
|
1443 |
* Disables the output stream for this socket. |
|
1444 |
* For a TCP socket, any previously written data will be sent |
|
1445 |
* followed by TCP's normal connection termination sequence. |
|
1446 |
* |
|
1447 |
* If you write to a socket output stream after invoking |
|
1448 |
* shutdownOutput() on the socket, the stream will throw |
|
1449 |
* an IOException. |
|
1450 |
* |
|
1451 |
* @exception IOException if an I/O error occurs when shutting down this |
|
1452 |
* socket. |
|
1453 |
* |
|
1454 |
* @since 1.3 |
|
1455 |
* @see java.net.Socket#shutdownInput() |
|
1456 |
* @see java.net.Socket#close() |
|
1457 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1458 |
* @see #isOutputShutdown |
|
1459 |
*/ |
|
1460 |
public void shutdownOutput() throws IOException |
|
1461 |
{ |
|
1462 |
if (isClosed()) |
|
1463 |
throw new SocketException("Socket is closed"); |
|
1464 |
if (!isConnected()) |
|
1465 |
throw new SocketException("Socket is not connected"); |
|
1466 |
if (isOutputShutdown()) |
|
1467 |
throw new SocketException("Socket output is already shutdown"); |
|
1468 |
getImpl().shutdownOutput(); |
|
1469 |
shutOut = true; |
|
1470 |
} |
|
1471 |
||
1472 |
/** |
|
1473 |
* Converts this socket to a <code>String</code>. |
|
1474 |
* |
|
1475 |
* @return a string representation of this socket. |
|
1476 |
*/ |
|
1477 |
public String toString() { |
|
1478 |
try { |
|
1479 |
if (isConnected()) |
|
1480 |
return "Socket[addr=" + getImpl().getInetAddress() + |
|
1481 |
",port=" + getImpl().getPort() + |
|
1482 |
",localport=" + getImpl().getLocalPort() + "]"; |
|
1483 |
} catch (SocketException e) { |
|
1484 |
} |
|
1485 |
return "Socket[unconnected]"; |
|
1486 |
} |
|
1487 |
||
1488 |
/** |
|
1489 |
* Returns the connection state of the socket. |
|
1490 |
* <p> |
|
1491 |
* Note: Closing a socket doesn't clear its connection state, which means |
|
1492 |
* this method will return <code>true</code> for a closed socket |
|
1493 |
* (see {@link #isClosed()}) if it was successfuly connected prior |
|
1494 |
* to being closed. |
|
1495 |
* |
|
1496 |
* @return true if the socket was successfuly connected to a server |
|
1497 |
* @since 1.4 |
|
1498 |
*/ |
|
1499 |
public boolean isConnected() { |
|
1500 |
// Before 1.3 Sockets were always connected during creation |
|
1501 |
return connected || oldImpl; |
|
1502 |
} |
|
1503 |
||
1504 |
/** |
|
1505 |
* Returns the binding state of the socket. |
|
1506 |
* <p> |
|
1507 |
* Note: Closing a socket doesn't clear its binding state, which means |
|
1508 |
* this method will return <code>true</code> for a closed socket |
|
1509 |
* (see {@link #isClosed()}) if it was successfuly bound prior |
|
1510 |
* to being closed. |
|
1511 |
* |
|
1512 |
* @return true if the socket was successfuly bound to an address |
|
1513 |
* @since 1.4 |
|
1514 |
* @see #bind |
|
1515 |
*/ |
|
1516 |
public boolean isBound() { |
|
1517 |
// Before 1.3 Sockets were always bound during creation |
|
1518 |
return bound || oldImpl; |
|
1519 |
} |
|
1520 |
||
1521 |
/** |
|
1522 |
* Returns the closed state of the socket. |
|
1523 |
* |
|
1524 |
* @return true if the socket has been closed |
|
1525 |
* @since 1.4 |
|
1526 |
* @see #close |
|
1527 |
*/ |
|
1528 |
public boolean isClosed() { |
|
1529 |
synchronized(closeLock) { |
|
1530 |
return closed; |
|
1531 |
} |
|
1532 |
} |
|
1533 |
||
1534 |
/** |
|
1535 |
* Returns whether the read-half of the socket connection is closed. |
|
1536 |
* |
|
1537 |
* @return true if the input of the socket has been shutdown |
|
1538 |
* @since 1.4 |
|
1539 |
* @see #shutdownInput |
|
1540 |
*/ |
|
1541 |
public boolean isInputShutdown() { |
|
1542 |
return shutIn; |
|
1543 |
} |
|
1544 |
||
1545 |
/** |
|
1546 |
* Returns whether the write-half of the socket connection is closed. |
|
1547 |
* |
|
1548 |
* @return true if the output of the socket has been shutdown |
|
1549 |
* @since 1.4 |
|
1550 |
* @see #shutdownOutput |
|
1551 |
*/ |
|
1552 |
public boolean isOutputShutdown() { |
|
1553 |
return shutOut; |
|
1554 |
} |
|
1555 |
||
1556 |
/** |
|
1557 |
* The factory for all client sockets. |
|
1558 |
*/ |
|
1559 |
private static SocketImplFactory factory = null; |
|
1560 |
||
1561 |
/** |
|
1562 |
* Sets the client socket implementation factory for the |
|
1563 |
* application. The factory can be specified only once. |
|
1564 |
* <p> |
|
1565 |
* When an application creates a new client socket, the socket |
|
1566 |
* implementation factory's <code>createSocketImpl</code> method is |
|
1567 |
* called to create the actual socket implementation. |
|
1568 |
* <p> |
|
1569 |
* Passing <code>null</code> to the method is a no-op unless the factory |
|
1570 |
* was already set. |
|
1571 |
* <p>If there is a security manager, this method first calls |
|
1572 |
* the security manager's <code>checkSetFactory</code> method |
|
1573 |
* to ensure the operation is allowed. |
|
1574 |
* This could result in a SecurityException. |
|
1575 |
* |
|
1576 |
* @param fac the desired factory. |
|
1577 |
* @exception IOException if an I/O error occurs when setting the |
|
1578 |
* socket factory. |
|
1579 |
* @exception SocketException if the factory is already defined. |
|
1580 |
* @exception SecurityException if a security manager exists and its |
|
1581 |
* <code>checkSetFactory</code> method doesn't allow the operation. |
|
1582 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
1583 |
* @see SecurityManager#checkSetFactory |
|
1584 |
*/ |
|
1585 |
public static synchronized void setSocketImplFactory(SocketImplFactory fac) |
|
1586 |
throws IOException |
|
1587 |
{ |
|
1588 |
if (factory != null) { |
|
1589 |
throw new SocketException("factory already defined"); |
|
1590 |
} |
|
1591 |
SecurityManager security = System.getSecurityManager(); |
|
1592 |
if (security != null) { |
|
1593 |
security.checkSetFactory(); |
|
1594 |
} |
|
1595 |
factory = fac; |
|
1596 |
} |
|
1597 |
||
1598 |
/** |
|
1599 |
* Sets performance preferences for this socket. |
|
1600 |
* |
|
1601 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
1602 |
* may offer alternative protocols which have different performance |
|
1603 |
* characteristics than TCP/IP. This method allows the application to |
|
1604 |
* express its own preferences as to how these tradeoffs should be made |
|
1605 |
* when the implementation chooses from the available protocols. |
|
1606 |
* |
|
1607 |
* <p> Performance preferences are described by three integers |
|
1608 |
* whose values indicate the relative importance of short connection time, |
|
1609 |
* low latency, and high bandwidth. The absolute values of the integers |
|
1610 |
* are irrelevant; in order to choose a protocol the values are simply |
|
1611 |
* compared, with larger values indicating stronger preferences. Negative |
|
1612 |
* values represent a lower priority than positive values. If the |
|
1613 |
* application prefers short connection time over both low latency and high |
|
1614 |
* bandwidth, for example, then it could invoke this method with the values |
|
1615 |
* <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low |
|
1616 |
* latency, and low latency above short connection time, then it could |
|
1617 |
* invoke this method with the values <tt>(0, 1, 2)</tt>. |
|
1618 |
* |
|
1619 |
* <p> Invoking this method after this socket has been connected |
|
1620 |
* will have no effect. |
|
1621 |
* |
|
1622 |
* @param connectionTime |
|
1623 |
* An <tt>int</tt> expressing the relative importance of a short |
|
1624 |
* connection time |
|
1625 |
* |
|
1626 |
* @param latency |
|
1627 |
* An <tt>int</tt> expressing the relative importance of low |
|
1628 |
* latency |
|
1629 |
* |
|
1630 |
* @param bandwidth |
|
1631 |
* An <tt>int</tt> expressing the relative importance of high |
|
1632 |
* bandwidth |
|
1633 |
* |
|
1634 |
* @since 1.5 |
|
1635 |
*/ |
|
1636 |
public void setPerformancePreferences(int connectionTime, |
|
1637 |
int latency, |
|
1638 |
int bandwidth) |
|
1639 |
{ |
|
1640 |
/* Not implemented yet */ |
|
1641 |
} |
|
1642 |
} |