author | michaelm |
Mon, 02 May 2011 20:11:18 +0100 | |
changeset 9550 | c3a275ce56d3 |
parent 9544 | 89c6ba8bb6c7 |
child 9775 | 1b128726e887 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InterruptedIOException; |
|
32 |
import java.nio.channels.SocketChannel; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.PrivilegedExceptionAction; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
||
37 |
/** |
|
38 |
* This class implements client sockets (also called just |
|
39 |
* "sockets"). A socket is an endpoint for communication |
|
40 |
* between two machines. |
|
41 |
* <p> |
|
42 |
* The actual work of the socket is performed by an instance of the |
|
43 |
* <code>SocketImpl</code> class. An application, by changing |
|
44 |
* the socket factory that creates the socket implementation, |
|
45 |
* can configure itself to create sockets appropriate to the local |
|
46 |
* firewall. |
|
47 |
* |
|
48 |
* @author unascribed |
|
49 |
* @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
|
50 |
* @see java.net.SocketImpl |
|
51 |
* @see java.nio.channels.SocketChannel |
|
52 |
* @since JDK1.0 |
|
53 |
*/ |
|
54 |
public |
|
55 |
class Socket implements java.io.Closeable { |
|
56 |
/** |
|
57 |
* Various states of this socket. |
|
58 |
*/ |
|
59 |
private boolean created = false; |
|
60 |
private boolean bound = false; |
|
61 |
private boolean connected = false; |
|
62 |
private boolean closed = false; |
|
63 |
private Object closeLock = new Object(); |
|
64 |
private boolean shutIn = false; |
|
65 |
private boolean shutOut = false; |
|
66 |
||
67 |
/** |
|
68 |
* The implementation of this Socket. |
|
69 |
*/ |
|
70 |
SocketImpl impl; |
|
71 |
||
72 |
/** |
|
73 |
* Are we using an older SocketImpl? |
|
74 |
*/ |
|
75 |
private boolean oldImpl = false; |
|
76 |
||
77 |
/** |
|
78 |
* Creates an unconnected socket, with the |
|
79 |
* system-default type of SocketImpl. |
|
80 |
* |
|
81 |
* @since JDK1.1 |
|
82 |
* @revised 1.4 |
|
83 |
*/ |
|
84 |
public Socket() { |
|
85 |
setImpl(); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Creates an unconnected socket, specifying the type of proxy, if any, |
|
90 |
* that should be used regardless of any other settings. |
|
91 |
* <P> |
|
92 |
* If there is a security manager, its <code>checkConnect</code> method |
|
93 |
* is called with the proxy host address and port number |
|
94 |
* as its arguments. This could result in a SecurityException. |
|
95 |
* <P> |
|
96 |
* Examples: |
|
97 |
* <UL> <LI><code>Socket s = new Socket(Proxy.NO_PROXY);</code> will create |
|
98 |
* a plain socket ignoring any other proxy configuration.</LI> |
|
99 |
* <LI><code>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));</code> |
|
100 |
* will create a socket connecting through the specified SOCKS proxy |
|
101 |
* server.</LI> |
|
102 |
* </UL> |
|
103 |
* |
|
104 |
* @param proxy a {@link java.net.Proxy Proxy} object specifying what kind |
|
105 |
* of proxying should be used. |
|
106 |
* @throws IllegalArgumentException if the proxy is of an invalid type |
|
107 |
* or <code>null</code>. |
|
108 |
* @throws SecurityException if a security manager is present and |
|
109 |
* permission to connect to the proxy is |
|
110 |
* denied. |
|
111 |
* @see java.net.ProxySelector |
|
112 |
* @see java.net.Proxy |
|
113 |
* |
|
114 |
* @since 1.5 |
|
115 |
*/ |
|
116 |
public Socket(Proxy proxy) { |
|
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
117 |
// 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
|
118 |
if (proxy == null) { |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
119 |
throw new IllegalArgumentException("Invalid Proxy"); |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
120 |
} |
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
3442
diff
changeset
|
121 |
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
|
122 |
if (p.type() == Proxy.Type.SOCKS) { |
2 | 123 |
SecurityManager security = System.getSecurityManager(); |
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
475
diff
changeset
|
124 |
InetSocketAddress epoint = (InetSocketAddress) p.address(); |
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
125 |
if (epoint.getAddress() != null) { |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
126 |
checkAddress (epoint.getAddress(), "Socket"); |
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
3450
diff
changeset
|
127 |
} |
2 | 128 |
if (security != null) { |
129 |
if (epoint.isUnresolved()) |
|
9550
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
130 |
security.checkConnect(epoint.getHostName(), |
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
131 |
epoint.getPort()); |
2 | 132 |
else |
133 |
security.checkConnect(epoint.getAddress().getHostAddress(), |
|
9550
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
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); |
|
423 |
if (address != null) |
|
424 |
connect(address); |
|
425 |
} catch (IOException e) { |
|
426 |
close(); |
|
427 |
throw e; |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* Creates the socket implementation. |
|
433 |
* |
|
434 |
* @param stream a <code>boolean</code> value : <code>true</code> for a TCP socket, |
|
435 |
* <code>false</code> for UDP. |
|
436 |
* @throws IOException if creation fails |
|
437 |
* @since 1.4 |
|
438 |
*/ |
|
439 |
void createImpl(boolean stream) throws SocketException { |
|
440 |
if (impl == null) |
|
441 |
setImpl(); |
|
442 |
try { |
|
443 |
impl.create(stream); |
|
444 |
created = true; |
|
445 |
} catch (IOException e) { |
|
446 |
throw new SocketException(e.getMessage()); |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
private void checkOldImpl() { |
|
451 |
if (impl == null) |
|
452 |
return; |
|
453 |
// SocketImpl.connect() is a protected method, therefore we need to use |
|
454 |
// getDeclaredMethod, therefore we need permission to access the member |
|
455 |
||
456 |
oldImpl = AccessController.doPrivileged |
|
457 |
(new PrivilegedAction<Boolean>() { |
|
458 |
public Boolean run() { |
|
459 |
Class[] cl = new Class[2]; |
|
460 |
cl[0] = SocketAddress.class; |
|
461 |
cl[1] = Integer.TYPE; |
|
462 |
Class clazz = impl.getClass(); |
|
463 |
while (true) { |
|
464 |
try { |
|
465 |
clazz.getDeclaredMethod("connect", cl); |
|
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> |
|
1438 |
* If you read from a socket input stream after invoking |
|
1439 |
* shutdownInput() on the socket, the stream will return EOF. |
|
1440 |
* |
|
1441 |
* @exception IOException if an I/O error occurs when shutting down this |
|
1442 |
* socket. |
|
1443 |
* |
|
1444 |
* @since 1.3 |
|
1445 |
* @see java.net.Socket#shutdownOutput() |
|
1446 |
* @see java.net.Socket#close() |
|
1447 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1448 |
* @see #isInputShutdown |
|
1449 |
*/ |
|
1450 |
public void shutdownInput() throws IOException |
|
1451 |
{ |
|
1452 |
if (isClosed()) |
|
1453 |
throw new SocketException("Socket is closed"); |
|
1454 |
if (!isConnected()) |
|
1455 |
throw new SocketException("Socket is not connected"); |
|
1456 |
if (isInputShutdown()) |
|
1457 |
throw new SocketException("Socket input is already shutdown"); |
|
1458 |
getImpl().shutdownInput(); |
|
1459 |
shutIn = true; |
|
1460 |
} |
|
1461 |
||
1462 |
/** |
|
1463 |
* Disables the output stream for this socket. |
|
1464 |
* For a TCP socket, any previously written data will be sent |
|
1465 |
* followed by TCP's normal connection termination sequence. |
|
1466 |
* |
|
1467 |
* If you write to a socket output stream after invoking |
|
1468 |
* shutdownOutput() on the socket, the stream will throw |
|
1469 |
* an IOException. |
|
1470 |
* |
|
1471 |
* @exception IOException if an I/O error occurs when shutting down this |
|
1472 |
* socket. |
|
1473 |
* |
|
1474 |
* @since 1.3 |
|
1475 |
* @see java.net.Socket#shutdownInput() |
|
1476 |
* @see java.net.Socket#close() |
|
1477 |
* @see java.net.Socket#setSoLinger(boolean, int) |
|
1478 |
* @see #isOutputShutdown |
|
1479 |
*/ |
|
1480 |
public void shutdownOutput() throws IOException |
|
1481 |
{ |
|
1482 |
if (isClosed()) |
|
1483 |
throw new SocketException("Socket is closed"); |
|
1484 |
if (!isConnected()) |
|
1485 |
throw new SocketException("Socket is not connected"); |
|
1486 |
if (isOutputShutdown()) |
|
1487 |
throw new SocketException("Socket output is already shutdown"); |
|
1488 |
getImpl().shutdownOutput(); |
|
1489 |
shutOut = true; |
|
1490 |
} |
|
1491 |
||
1492 |
/** |
|
1493 |
* Converts this socket to a <code>String</code>. |
|
1494 |
* |
|
1495 |
* @return a string representation of this socket. |
|
1496 |
*/ |
|
1497 |
public String toString() { |
|
1498 |
try { |
|
1499 |
if (isConnected()) |
|
1500 |
return "Socket[addr=" + getImpl().getInetAddress() + |
|
1501 |
",port=" + getImpl().getPort() + |
|
1502 |
",localport=" + getImpl().getLocalPort() + "]"; |
|
1503 |
} catch (SocketException e) { |
|
1504 |
} |
|
1505 |
return "Socket[unconnected]"; |
|
1506 |
} |
|
1507 |
||
1508 |
/** |
|
1509 |
* Returns the connection state of the socket. |
|
1510 |
* <p> |
|
1511 |
* Note: Closing a socket doesn't clear its connection state, which means |
|
1512 |
* this method will return <code>true</code> for a closed socket |
|
1513 |
* (see {@link #isClosed()}) if it was successfuly connected prior |
|
1514 |
* to being closed. |
|
1515 |
* |
|
1516 |
* @return true if the socket was successfuly connected to a server |
|
1517 |
* @since 1.4 |
|
1518 |
*/ |
|
1519 |
public boolean isConnected() { |
|
1520 |
// Before 1.3 Sockets were always connected during creation |
|
1521 |
return connected || oldImpl; |
|
1522 |
} |
|
1523 |
||
1524 |
/** |
|
1525 |
* Returns the binding state of the socket. |
|
1526 |
* <p> |
|
1527 |
* Note: Closing a socket doesn't clear its binding state, which means |
|
1528 |
* this method will return <code>true</code> for a closed socket |
|
1529 |
* (see {@link #isClosed()}) if it was successfuly bound prior |
|
1530 |
* to being closed. |
|
1531 |
* |
|
1532 |
* @return true if the socket was successfuly bound to an address |
|
1533 |
* @since 1.4 |
|
1534 |
* @see #bind |
|
1535 |
*/ |
|
1536 |
public boolean isBound() { |
|
1537 |
// Before 1.3 Sockets were always bound during creation |
|
1538 |
return bound || oldImpl; |
|
1539 |
} |
|
1540 |
||
1541 |
/** |
|
1542 |
* Returns the closed state of the socket. |
|
1543 |
* |
|
1544 |
* @return true if the socket has been closed |
|
1545 |
* @since 1.4 |
|
1546 |
* @see #close |
|
1547 |
*/ |
|
1548 |
public boolean isClosed() { |
|
1549 |
synchronized(closeLock) { |
|
1550 |
return closed; |
|
1551 |
} |
|
1552 |
} |
|
1553 |
||
1554 |
/** |
|
1555 |
* Returns whether the read-half of the socket connection is closed. |
|
1556 |
* |
|
1557 |
* @return true if the input of the socket has been shutdown |
|
1558 |
* @since 1.4 |
|
1559 |
* @see #shutdownInput |
|
1560 |
*/ |
|
1561 |
public boolean isInputShutdown() { |
|
1562 |
return shutIn; |
|
1563 |
} |
|
1564 |
||
1565 |
/** |
|
1566 |
* Returns whether the write-half of the socket connection is closed. |
|
1567 |
* |
|
1568 |
* @return true if the output of the socket has been shutdown |
|
1569 |
* @since 1.4 |
|
1570 |
* @see #shutdownOutput |
|
1571 |
*/ |
|
1572 |
public boolean isOutputShutdown() { |
|
1573 |
return shutOut; |
|
1574 |
} |
|
1575 |
||
1576 |
/** |
|
1577 |
* The factory for all client sockets. |
|
1578 |
*/ |
|
1579 |
private static SocketImplFactory factory = null; |
|
1580 |
||
1581 |
/** |
|
1582 |
* Sets the client socket implementation factory for the |
|
1583 |
* application. The factory can be specified only once. |
|
1584 |
* <p> |
|
1585 |
* When an application creates a new client socket, the socket |
|
1586 |
* implementation factory's <code>createSocketImpl</code> method is |
|
1587 |
* called to create the actual socket implementation. |
|
1588 |
* <p> |
|
1589 |
* Passing <code>null</code> to the method is a no-op unless the factory |
|
1590 |
* was already set. |
|
1591 |
* <p>If there is a security manager, this method first calls |
|
1592 |
* the security manager's <code>checkSetFactory</code> method |
|
1593 |
* to ensure the operation is allowed. |
|
1594 |
* This could result in a SecurityException. |
|
1595 |
* |
|
1596 |
* @param fac the desired factory. |
|
1597 |
* @exception IOException if an I/O error occurs when setting the |
|
1598 |
* socket factory. |
|
1599 |
* @exception SocketException if the factory is already defined. |
|
1600 |
* @exception SecurityException if a security manager exists and its |
|
1601 |
* <code>checkSetFactory</code> method doesn't allow the operation. |
|
1602 |
* @see java.net.SocketImplFactory#createSocketImpl() |
|
1603 |
* @see SecurityManager#checkSetFactory |
|
1604 |
*/ |
|
1605 |
public static synchronized void setSocketImplFactory(SocketImplFactory fac) |
|
1606 |
throws IOException |
|
1607 |
{ |
|
1608 |
if (factory != null) { |
|
1609 |
throw new SocketException("factory already defined"); |
|
1610 |
} |
|
1611 |
SecurityManager security = System.getSecurityManager(); |
|
1612 |
if (security != null) { |
|
1613 |
security.checkSetFactory(); |
|
1614 |
} |
|
1615 |
factory = fac; |
|
1616 |
} |
|
1617 |
||
1618 |
/** |
|
1619 |
* Sets performance preferences for this socket. |
|
1620 |
* |
|
1621 |
* <p> Sockets use the TCP/IP protocol by default. Some implementations |
|
1622 |
* may offer alternative protocols which have different performance |
|
1623 |
* characteristics than TCP/IP. This method allows the application to |
|
1624 |
* express its own preferences as to how these tradeoffs should be made |
|
1625 |
* when the implementation chooses from the available protocols. |
|
1626 |
* |
|
1627 |
* <p> Performance preferences are described by three integers |
|
1628 |
* whose values indicate the relative importance of short connection time, |
|
1629 |
* low latency, and high bandwidth. The absolute values of the integers |
|
1630 |
* are irrelevant; in order to choose a protocol the values are simply |
|
1631 |
* compared, with larger values indicating stronger preferences. Negative |
|
1632 |
* values represent a lower priority than positive values. If the |
|
1633 |
* application prefers short connection time over both low latency and high |
|
1634 |
* bandwidth, for example, then it could invoke this method with the values |
|
1635 |
* <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low |
|
1636 |
* latency, and low latency above short connection time, then it could |
|
1637 |
* invoke this method with the values <tt>(0, 1, 2)</tt>. |
|
1638 |
* |
|
1639 |
* <p> Invoking this method after this socket has been connected |
|
1640 |
* will have no effect. |
|
1641 |
* |
|
1642 |
* @param connectionTime |
|
1643 |
* An <tt>int</tt> expressing the relative importance of a short |
|
1644 |
* connection time |
|
1645 |
* |
|
1646 |
* @param latency |
|
1647 |
* An <tt>int</tt> expressing the relative importance of low |
|
1648 |
* latency |
|
1649 |
* |
|
1650 |
* @param bandwidth |
|
1651 |
* An <tt>int</tt> expressing the relative importance of high |
|
1652 |
* bandwidth |
|
1653 |
* |
|
1654 |
* @since 1.5 |
|
1655 |
*/ |
|
1656 |
public void setPerformancePreferences(int connectionTime, |
|
1657 |
int latency, |
|
1658 |
int bandwidth) |
|
1659 |
{ |
|
1660 |
/* Not implemented yet */ |
|
1661 |
} |
|
1662 |
} |