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