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