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