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