1 /* |
1 /* |
2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
37 * This class implements client sockets (also called just |
37 * This class implements client sockets (also called just |
38 * "sockets"). A socket is an endpoint for communication |
38 * "sockets"). A socket is an endpoint for communication |
39 * between two machines. |
39 * between two machines. |
40 * <p> |
40 * <p> |
41 * The actual work of the socket is performed by an instance of the |
41 * The actual work of the socket is performed by an instance of the |
42 * <code>SocketImpl</code> class. An application, by changing |
42 * {@code SocketImpl} class. An application, by changing |
43 * the socket factory that creates the socket implementation, |
43 * the socket factory that creates the socket implementation, |
44 * can configure itself to create sockets appropriate to the local |
44 * can configure itself to create sockets appropriate to the local |
45 * firewall. |
45 * firewall. |
46 * |
46 * |
47 * @author unascribed |
47 * @author unascribed |
86 |
86 |
87 /** |
87 /** |
88 * Creates an unconnected socket, specifying the type of proxy, if any, |
88 * Creates an unconnected socket, specifying the type of proxy, if any, |
89 * that should be used regardless of any other settings. |
89 * that should be used regardless of any other settings. |
90 * <P> |
90 * <P> |
91 * If there is a security manager, its <code>checkConnect</code> method |
91 * If there is a security manager, its {@code checkConnect} method |
92 * is called with the proxy host address and port number |
92 * is called with the proxy host address and port number |
93 * as its arguments. This could result in a SecurityException. |
93 * as its arguments. This could result in a SecurityException. |
94 * <P> |
94 * <P> |
95 * Examples: |
95 * Examples: |
96 * <UL> <LI><code>Socket s = new Socket(Proxy.NO_PROXY);</code> will create |
96 * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create |
97 * a plain socket ignoring any other proxy configuration.</LI> |
97 * a plain socket ignoring any other proxy configuration.</LI> |
98 * <LI><code>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));</code> |
98 * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));} |
99 * will create a socket connecting through the specified SOCKS proxy |
99 * will create a socket connecting through the specified SOCKS proxy |
100 * server.</LI> |
100 * server.</LI> |
101 * </UL> |
101 * </UL> |
102 * |
102 * |
103 * @param proxy a {@link java.net.Proxy Proxy} object specifying what kind |
103 * @param proxy a {@link java.net.Proxy Proxy} object specifying what kind |
104 * of proxying should be used. |
104 * of proxying should be used. |
105 * @throws IllegalArgumentException if the proxy is of an invalid type |
105 * @throws IllegalArgumentException if the proxy is of an invalid type |
106 * or <code>null</code>. |
106 * or {@code null}. |
107 * @throws SecurityException if a security manager is present and |
107 * @throws SecurityException if a security manager is present and |
108 * permission to connect to the proxy is |
108 * permission to connect to the proxy is |
109 * denied. |
109 * denied. |
110 * @see java.net.ProxySelector |
110 * @see java.net.ProxySelector |
111 * @see java.net.Proxy |
111 * @see java.net.Proxy |
171 |
171 |
172 /** |
172 /** |
173 * Creates a stream socket and connects it to the specified port |
173 * Creates a stream socket and connects it to the specified port |
174 * number on the named host. |
174 * number on the named host. |
175 * <p> |
175 * <p> |
176 * If the specified host is <tt>null</tt> it is the equivalent of |
176 * If the specified host is {@code null} it is the equivalent of |
177 * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
177 * specifying the address as |
|
178 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
178 * In other words, it is equivalent to specifying an address of the |
179 * In other words, it is equivalent to specifying an address of the |
179 * loopback interface. </p> |
180 * loopback interface. </p> |
180 * <p> |
181 * <p> |
181 * If the application has specified a server socket factory, that |
182 * If the application has specified a server socket factory, that |
182 * factory's <code>createSocketImpl</code> method is called to create |
183 * factory's {@code createSocketImpl} method is called to create |
183 * the actual socket implementation. Otherwise a "plain" socket is created. |
184 * the actual socket implementation. Otherwise a "plain" socket is created. |
184 * <p> |
185 * <p> |
185 * If there is a security manager, its |
186 * If there is a security manager, its |
186 * <code>checkConnect</code> method is called |
187 * {@code checkConnect} method is called |
187 * with the host address and <code>port</code> |
188 * with the host address and {@code port} |
188 * as its arguments. This could result in a SecurityException. |
189 * as its arguments. This could result in a SecurityException. |
189 * |
190 * |
190 * @param host the host name, or <code>null</code> for the loopback address. |
191 * @param host the host name, or {@code null} for the loopback address. |
191 * @param port the port number. |
192 * @param port the port number. |
192 * |
193 * |
193 * @exception UnknownHostException if the IP address of |
194 * @exception UnknownHostException if the IP address of |
194 * the host could not be determined. |
195 * the host could not be determined. |
195 * |
196 * |
196 * @exception IOException if an I/O error occurs when creating the socket. |
197 * @exception IOException if an I/O error occurs when creating the socket. |
197 * @exception SecurityException if a security manager exists and its |
198 * @exception SecurityException if a security manager exists and its |
198 * <code>checkConnect</code> method doesn't allow the operation. |
199 * {@code checkConnect} method doesn't allow the operation. |
199 * @exception IllegalArgumentException if the port parameter is outside |
200 * @exception IllegalArgumentException if the port parameter is outside |
200 * the specified range of valid port values, which is between |
201 * the specified range of valid port values, which is between |
201 * 0 and 65535, inclusive. |
202 * 0 and 65535, inclusive. |
202 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
203 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
203 * @see java.net.SocketImpl |
204 * @see java.net.SocketImpl |
215 /** |
216 /** |
216 * Creates a stream socket and connects it to the specified port |
217 * Creates a stream socket and connects it to the specified port |
217 * number at the specified IP address. |
218 * number at the specified IP address. |
218 * <p> |
219 * <p> |
219 * If the application has specified a socket factory, that factory's |
220 * If the application has specified a socket factory, that factory's |
220 * <code>createSocketImpl</code> method is called to create the |
221 * {@code createSocketImpl} method is called to create the |
221 * actual socket implementation. Otherwise a "plain" socket is created. |
222 * actual socket implementation. Otherwise a "plain" socket is created. |
222 * <p> |
223 * <p> |
223 * If there is a security manager, its |
224 * If there is a security manager, its |
224 * <code>checkConnect</code> method is called |
225 * {@code checkConnect} method is called |
225 * with the host address and <code>port</code> |
226 * with the host address and {@code port} |
226 * as its arguments. This could result in a SecurityException. |
227 * as its arguments. This could result in a SecurityException. |
227 * |
228 * |
228 * @param address the IP address. |
229 * @param address the IP address. |
229 * @param port the port number. |
230 * @param port the port number. |
230 * @exception IOException if an I/O error occurs when creating the socket. |
231 * @exception IOException if an I/O error occurs when creating the socket. |
231 * @exception SecurityException if a security manager exists and its |
232 * @exception SecurityException if a security manager exists and its |
232 * <code>checkConnect</code> method doesn't allow the operation. |
233 * {@code checkConnect} method doesn't allow the operation. |
233 * @exception IllegalArgumentException if the port parameter is outside |
234 * @exception IllegalArgumentException if the port parameter is outside |
234 * the specified range of valid port values, which is between |
235 * the specified range of valid port values, which is between |
235 * 0 and 65535, inclusive. |
236 * 0 and 65535, inclusive. |
236 * @exception NullPointerException if <code>address</code> is null. |
237 * @exception NullPointerException if {@code address} is null. |
237 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
238 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
238 * @see java.net.SocketImpl |
239 * @see java.net.SocketImpl |
239 * @see java.net.SocketImplFactory#createSocketImpl() |
240 * @see java.net.SocketImplFactory#createSocketImpl() |
240 * @see SecurityManager#checkConnect |
241 * @see SecurityManager#checkConnect |
241 */ |
242 */ |
247 /** |
248 /** |
248 * Creates a socket and connects it to the specified remote host on |
249 * Creates a socket and connects it to the specified remote host on |
249 * the specified remote port. The Socket will also bind() to the local |
250 * the specified remote port. The Socket will also bind() to the local |
250 * address and port supplied. |
251 * address and port supplied. |
251 * <p> |
252 * <p> |
252 * If the specified host is <tt>null</tt> it is the equivalent of |
253 * If the specified host is {@code null} it is the equivalent of |
253 * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
254 * specifying the address as |
|
255 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
254 * In other words, it is equivalent to specifying an address of the |
256 * In other words, it is equivalent to specifying an address of the |
255 * loopback interface. </p> |
257 * loopback interface. </p> |
256 * <p> |
258 * <p> |
257 * A local port number of <code>zero</code> will let the system pick up a |
259 * A local port number of {@code zero} will let the system pick up a |
258 * free port in the <code>bind</code> operation.</p> |
260 * free port in the {@code bind} operation.</p> |
259 * <p> |
261 * <p> |
260 * If there is a security manager, its |
262 * If there is a security manager, its |
261 * <code>checkConnect</code> method is called |
263 * {@code checkConnect} method is called |
262 * with the host address and <code>port</code> |
264 * with the host address and {@code port} |
263 * as its arguments. This could result in a SecurityException. |
265 * as its arguments. This could result in a SecurityException. |
264 * |
266 * |
265 * @param host the name of the remote host, or <code>null</code> for the loopback address. |
267 * @param host the name of the remote host, or {@code null} for the loopback address. |
266 * @param port the remote port |
268 * @param port the remote port |
267 * @param localAddr the local address the socket is bound to, or |
269 * @param localAddr the local address the socket is bound to, or |
268 * <code>null</code> for the <code>anyLocal</code> address. |
270 * {@code null} for the {@code anyLocal} address. |
269 * @param localPort the local port the socket is bound to, or |
271 * @param localPort the local port the socket is bound to, or |
270 * <code>zero</code> for a system selected free port. |
272 * {@code zero} for a system selected free port. |
271 * @exception IOException if an I/O error occurs when creating the socket. |
273 * @exception IOException if an I/O error occurs when creating the socket. |
272 * @exception SecurityException if a security manager exists and its |
274 * @exception SecurityException if a security manager exists and its |
273 * <code>checkConnect</code> method doesn't allow the operation. |
275 * {@code checkConnect} method doesn't allow the operation. |
274 * @exception IllegalArgumentException if the port parameter or localPort |
276 * @exception IllegalArgumentException if the port parameter or localPort |
275 * parameter is outside the specified range of valid port values, |
277 * parameter is outside the specified range of valid port values, |
276 * which is between 0 and 65535, inclusive. |
278 * which is between 0 and 65535, inclusive. |
277 * @see SecurityManager#checkConnect |
279 * @see SecurityManager#checkConnect |
278 * @since JDK1.1 |
280 * @since JDK1.1 |
287 /** |
289 /** |
288 * Creates a socket and connects it to the specified remote address on |
290 * Creates a socket and connects it to the specified remote address on |
289 * the specified remote port. The Socket will also bind() to the local |
291 * the specified remote port. The Socket will also bind() to the local |
290 * address and port supplied. |
292 * address and port supplied. |
291 * <p> |
293 * <p> |
292 * If the specified local address is <tt>null</tt> it is the equivalent of |
294 * If the specified local address is {@code null} it is the equivalent of |
293 * specifying the address as the AnyLocal address (see <tt>{@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}()</tt>). |
295 * specifying the address as the AnyLocal address |
294 * <p> |
296 * (see {@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}{@code ()}). |
295 * A local port number of <code>zero</code> will let the system pick up a |
297 * <p> |
296 * free port in the <code>bind</code> operation.</p> |
298 * A local port number of {@code zero} will let the system pick up a |
|
299 * free port in the {@code bind} operation.</p> |
297 * <p> |
300 * <p> |
298 * If there is a security manager, its |
301 * If there is a security manager, its |
299 * <code>checkConnect</code> method is called |
302 * {@code checkConnect} method is called |
300 * with the host address and <code>port</code> |
303 * with the host address and {@code port} |
301 * as its arguments. This could result in a SecurityException. |
304 * as its arguments. This could result in a SecurityException. |
302 * |
305 * |
303 * @param address the remote address |
306 * @param address the remote address |
304 * @param port the remote port |
307 * @param port the remote port |
305 * @param localAddr the local address the socket is bound to, or |
308 * @param localAddr the local address the socket is bound to, or |
306 * <code>null</code> for the <code>anyLocal</code> address. |
309 * {@code null} for the {@code anyLocal} address. |
307 * @param localPort the local port the socket is bound to or |
310 * @param localPort the local port the socket is bound to or |
308 * <code>zero</code> for a system selected free port. |
311 * {@code zero} for a system selected free port. |
309 * @exception IOException if an I/O error occurs when creating the socket. |
312 * @exception IOException if an I/O error occurs when creating the socket. |
310 * @exception SecurityException if a security manager exists and its |
313 * @exception SecurityException if a security manager exists and its |
311 * <code>checkConnect</code> method doesn't allow the operation. |
314 * {@code checkConnect} method doesn't allow the operation. |
312 * @exception IllegalArgumentException if the port parameter or localPort |
315 * @exception IllegalArgumentException if the port parameter or localPort |
313 * parameter is outside the specified range of valid port values, |
316 * parameter is outside the specified range of valid port values, |
314 * which is between 0 and 65535, inclusive. |
317 * which is between 0 and 65535, inclusive. |
315 * @exception NullPointerException if <code>address</code> is null. |
318 * @exception NullPointerException if {@code address} is null. |
316 * @see SecurityManager#checkConnect |
319 * @see SecurityManager#checkConnect |
317 * @since JDK1.1 |
320 * @since JDK1.1 |
318 */ |
321 */ |
319 public Socket(InetAddress address, int port, InetAddress localAddr, |
322 public Socket(InetAddress address, int port, InetAddress localAddr, |
320 int localPort) throws IOException { |
323 int localPort) throws IOException { |
324 |
327 |
325 /** |
328 /** |
326 * Creates a stream socket and connects it to the specified port |
329 * Creates a stream socket and connects it to the specified port |
327 * number on the named host. |
330 * number on the named host. |
328 * <p> |
331 * <p> |
329 * If the specified host is <tt>null</tt> it is the equivalent of |
332 * If the specified host is {@code null} it is the equivalent of |
330 * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. |
333 * specifying the address as |
|
334 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}. |
331 * In other words, it is equivalent to specifying an address of the |
335 * In other words, it is equivalent to specifying an address of the |
332 * loopback interface. </p> |
336 * loopback interface. </p> |
333 * <p> |
337 * <p> |
334 * If the stream argument is <code>true</code>, this creates a |
338 * If the stream argument is {@code true}, this creates a |
335 * stream socket. If the stream argument is <code>false</code>, it |
339 * stream socket. If the stream argument is {@code false}, it |
336 * creates a datagram socket. |
340 * creates a datagram socket. |
337 * <p> |
341 * <p> |
338 * If the application has specified a server socket factory, that |
342 * If the application has specified a server socket factory, that |
339 * factory's <code>createSocketImpl</code> method is called to create |
343 * factory's {@code createSocketImpl} method is called to create |
340 * the actual socket implementation. Otherwise a "plain" socket is created. |
344 * the actual socket implementation. Otherwise a "plain" socket is created. |
341 * <p> |
345 * <p> |
342 * If there is a security manager, its |
346 * If there is a security manager, its |
343 * <code>checkConnect</code> method is called |
347 * {@code checkConnect} method is called |
344 * with the host address and <code>port</code> |
348 * with the host address and {@code port} |
345 * as its arguments. This could result in a SecurityException. |
349 * as its arguments. This could result in a SecurityException. |
346 * <p> |
350 * <p> |
347 * If a UDP socket is used, TCP/IP related socket options will not apply. |
351 * If a UDP socket is used, TCP/IP related socket options will not apply. |
348 * |
352 * |
349 * @param host the host name, or <code>null</code> for the loopback address. |
353 * @param host the host name, or {@code null} for the loopback address. |
350 * @param port the port number. |
354 * @param port the port number. |
351 * @param stream a <code>boolean</code> indicating whether this is |
355 * @param stream a {@code boolean} indicating whether this is |
352 * a stream socket or a datagram socket. |
356 * a stream socket or a datagram socket. |
353 * @exception IOException if an I/O error occurs when creating the socket. |
357 * @exception IOException if an I/O error occurs when creating the socket. |
354 * @exception SecurityException if a security manager exists and its |
358 * @exception SecurityException if a security manager exists and its |
355 * <code>checkConnect</code> method doesn't allow the operation. |
359 * {@code checkConnect} method doesn't allow the operation. |
356 * @exception IllegalArgumentException if the port parameter is outside |
360 * @exception IllegalArgumentException if the port parameter is outside |
357 * the specified range of valid port values, which is between |
361 * the specified range of valid port values, which is between |
358 * 0 and 65535, inclusive. |
362 * 0 and 65535, inclusive. |
359 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
363 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
360 * @see java.net.SocketImpl |
364 * @see java.net.SocketImpl |
371 |
375 |
372 /** |
376 /** |
373 * Creates a socket and connects it to the specified port number at |
377 * Creates a socket and connects it to the specified port number at |
374 * the specified IP address. |
378 * the specified IP address. |
375 * <p> |
379 * <p> |
376 * If the stream argument is <code>true</code>, this creates a |
380 * If the stream argument is {@code true}, this creates a |
377 * stream socket. If the stream argument is <code>false</code>, it |
381 * stream socket. If the stream argument is {@code false}, it |
378 * creates a datagram socket. |
382 * creates a datagram socket. |
379 * <p> |
383 * <p> |
380 * If the application has specified a server socket factory, that |
384 * If the application has specified a server socket factory, that |
381 * factory's <code>createSocketImpl</code> method is called to create |
385 * factory's {@code createSocketImpl} method is called to create |
382 * the actual socket implementation. Otherwise a "plain" socket is created. |
386 * the actual socket implementation. Otherwise a "plain" socket is created. |
383 * |
387 * |
384 * <p>If there is a security manager, its |
388 * <p>If there is a security manager, its |
385 * <code>checkConnect</code> method is called |
389 * {@code checkConnect} method is called |
386 * with <code>host.getHostAddress()</code> and <code>port</code> |
390 * with {@code host.getHostAddress()} and {@code port} |
387 * as its arguments. This could result in a SecurityException. |
391 * as its arguments. This could result in a SecurityException. |
388 * <p> |
392 * <p> |
389 * If UDP socket is used, TCP/IP related socket options will not apply. |
393 * If UDP socket is used, TCP/IP related socket options will not apply. |
390 * |
394 * |
391 * @param host the IP address. |
395 * @param host the IP address. |
392 * @param port the port number. |
396 * @param port the port number. |
393 * @param stream if <code>true</code>, create a stream socket; |
397 * @param stream if {@code true}, create a stream socket; |
394 * otherwise, create a datagram socket. |
398 * otherwise, create a datagram socket. |
395 * @exception IOException if an I/O error occurs when creating the socket. |
399 * @exception IOException if an I/O error occurs when creating the socket. |
396 * @exception SecurityException if a security manager exists and its |
400 * @exception SecurityException if a security manager exists and its |
397 * <code>checkConnect</code> method doesn't allow the operation. |
401 * {@code checkConnect} method doesn't allow the operation. |
398 * @exception IllegalArgumentException if the port parameter is outside |
402 * @exception IllegalArgumentException if the port parameter is outside |
399 * the specified range of valid port values, which is between |
403 * the specified range of valid port values, which is between |
400 * 0 and 65535, inclusive. |
404 * 0 and 65535, inclusive. |
401 * @exception NullPointerException if <code>host</code> is null. |
405 * @exception NullPointerException if {@code host} is null. |
402 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
406 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) |
403 * @see java.net.SocketImpl |
407 * @see java.net.SocketImpl |
404 * @see java.net.SocketImplFactory#createSocketImpl() |
408 * @see java.net.SocketImplFactory#createSocketImpl() |
405 * @see SecurityManager#checkConnect |
409 * @see SecurityManager#checkConnect |
406 * @deprecated Use DatagramSocket instead for UDP transport. |
410 * @deprecated Use DatagramSocket instead for UDP transport. |
514 } |
518 } |
515 |
519 |
516 /** |
520 /** |
517 * Connects this socket to the server. |
521 * Connects this socket to the server. |
518 * |
522 * |
519 * @param endpoint the <code>SocketAddress</code> |
523 * @param endpoint the {@code SocketAddress} |
520 * @throws IOException if an error occurs during the connection |
524 * @throws IOException if an error occurs during the connection |
521 * @throws java.nio.channels.IllegalBlockingModeException |
525 * @throws java.nio.channels.IllegalBlockingModeException |
522 * if this socket has an associated channel, |
526 * if this socket has an associated channel, |
523 * and the channel is in non-blocking mode |
527 * and the channel is in non-blocking mode |
524 * @throws IllegalArgumentException if endpoint is null or is a |
528 * @throws IllegalArgumentException if endpoint is null or is a |
533 /** |
537 /** |
534 * Connects this socket to the server with a specified timeout value. |
538 * Connects this socket to the server with a specified timeout value. |
535 * A timeout of zero is interpreted as an infinite timeout. The connection |
539 * A timeout of zero is interpreted as an infinite timeout. The connection |
536 * will then block until established or an error occurs. |
540 * will then block until established or an error occurs. |
537 * |
541 * |
538 * @param endpoint the <code>SocketAddress</code> |
542 * @param endpoint the {@code SocketAddress} |
539 * @param timeout the timeout value to be used in milliseconds. |
543 * @param timeout the timeout value to be used in milliseconds. |
540 * @throws IOException if an error occurs during the connection |
544 * @throws IOException if an error occurs during the connection |
541 * @throws SocketTimeoutException if timeout expires before connecting |
545 * @throws SocketTimeoutException if timeout expires before connecting |
542 * @throws java.nio.channels.IllegalBlockingModeException |
546 * @throws java.nio.channels.IllegalBlockingModeException |
543 * if this socket has an associated channel, |
547 * if this socket has an associated channel, |
595 } |
599 } |
596 |
600 |
597 /** |
601 /** |
598 * Binds the socket to a local address. |
602 * Binds the socket to a local address. |
599 * <P> |
603 * <P> |
600 * If the address is <code>null</code>, then the system will pick up |
604 * If the address is {@code null}, then the system will pick up |
601 * an ephemeral port and a valid local address to bind the socket. |
605 * an ephemeral port and a valid local address to bind the socket. |
602 * |
606 * |
603 * @param bindpoint the <code>SocketAddress</code> to bind to |
607 * @param bindpoint the {@code SocketAddress} to bind to |
604 * @throws IOException if the bind operation fails, or if the socket |
608 * @throws IOException if the bind operation fails, or if the socket |
605 * is already bound. |
609 * is already bound. |
606 * @throws IllegalArgumentException if bindpoint is a |
610 * @throws IllegalArgumentException if bindpoint is a |
607 * SocketAddress subclass not supported by this socket |
611 * SocketAddress subclass not supported by this socket |
608 * |
612 * |
758 return -1; |
762 return -1; |
759 } |
763 } |
760 |
764 |
761 /** |
765 /** |
762 * Returns the address of the endpoint this socket is connected to, or |
766 * Returns the address of the endpoint this socket is connected to, or |
763 * <code>null</code> if it is unconnected. |
767 * {@code null} if it is unconnected. |
764 * <p> |
768 * <p> |
765 * If the socket was connected prior to being {@link #close closed}, |
769 * If the socket was connected prior to being {@link #close closed}, |
766 * then this method will continue to return the connected address |
770 * then this method will continue to return the connected address |
767 * after the socket is closed. |
771 * after the socket is closed. |
768 * |
772 * |
769 |
773 |
770 * @return a <code>SocketAddress</code> representing the remote endpoint of this |
774 * @return a {@code SocketAddress} representing the remote endpoint of this |
771 * socket, or <code>null</code> if it is not connected yet. |
775 * socket, or {@code null} if it is not connected yet. |
772 * @see #getInetAddress() |
776 * @see #getInetAddress() |
773 * @see #getPort() |
777 * @see #getPort() |
774 * @see #connect(SocketAddress, int) |
778 * @see #connect(SocketAddress, int) |
775 * @see #connect(SocketAddress) |
779 * @see #connect(SocketAddress) |
776 * @since 1.4 |
780 * @since 1.4 |
783 |
787 |
784 /** |
788 /** |
785 * Returns the address of the endpoint this socket is bound to. |
789 * Returns the address of the endpoint this socket is bound to. |
786 * <p> |
790 * <p> |
787 * If a socket bound to an endpoint represented by an |
791 * If a socket bound to an endpoint represented by an |
788 * <code>InetSocketAddress </code> is {@link #close closed}, |
792 * {@code InetSocketAddress } is {@link #close closed}, |
789 * then this method will continue to return an <code>InetSocketAddress</code> |
793 * then this method will continue to return an {@code InetSocketAddress} |
790 * after the socket is closed. In that case the returned |
794 * after the socket is closed. In that case the returned |
791 * <code>InetSocketAddress</code>'s address is the |
795 * {@code InetSocketAddress}'s address is the |
792 * {@link InetAddress#isAnyLocalAddress wildcard} address |
796 * {@link InetAddress#isAnyLocalAddress wildcard} address |
793 * and its port is the local port that it was bound to. |
797 * and its port is the local port that it was bound to. |
794 * <p> |
798 * <p> |
795 * If there is a security manager set, its {@code checkConnect} method is |
799 * If there is a security manager set, its {@code checkConnect} method is |
796 * called with the local address and {@code -1} as its arguments to see |
800 * called with the local address and {@code -1} as its arguments to see |
841 /** |
845 /** |
842 * Returns an input stream for this socket. |
846 * Returns an input stream for this socket. |
843 * |
847 * |
844 * <p> If this socket has an associated channel then the resulting input |
848 * <p> If this socket has an associated channel then the resulting input |
845 * stream delegates all of its operations to the channel. If the channel |
849 * stream delegates all of its operations to the channel. If the channel |
846 * is in non-blocking mode then the input stream's <tt>read</tt> operations |
850 * is in non-blocking mode then the input stream's {@code read} operations |
847 * will throw an {@link java.nio.channels.IllegalBlockingModeException}. |
851 * will throw an {@link java.nio.channels.IllegalBlockingModeException}. |
848 * |
852 * |
849 * <p>Under abnormal conditions the underlying connection may be |
853 * <p>Under abnormal conditions the underlying connection may be |
850 * broken by the remote host or the network software (for example |
854 * broken by the remote host or the network software (for example |
851 * a connection reset in the case of TCP connections). When a |
855 * a connection reset in the case of TCP connections). When a |
865 * {@link java.io.IOException IOException}. |
869 * {@link java.io.IOException IOException}. |
866 * |
870 * |
867 * <li><p>If there are no bytes buffered on the socket, and the |
871 * <li><p>If there are no bytes buffered on the socket, and the |
868 * socket has not been closed using {@link #close close}, then |
872 * socket has not been closed using {@link #close close}, then |
869 * {@link java.io.InputStream#available available} will |
873 * {@link java.io.InputStream#available available} will |
870 * return <code>0</code>. |
874 * return {@code 0}. |
871 * |
875 * |
872 * </ul> |
876 * </ul> |
873 * |
877 * |
874 * <p> Closing the returned {@link java.io.InputStream InputStream} |
878 * <p> Closing the returned {@link java.io.InputStream InputStream} |
875 * will close the associated socket. |
879 * will close the associated socket. |
908 /** |
912 /** |
909 * Returns an output stream for this socket. |
913 * Returns an output stream for this socket. |
910 * |
914 * |
911 * <p> If this socket has an associated channel then the resulting output |
915 * <p> If this socket has an associated channel then the resulting output |
912 * stream delegates all of its operations to the channel. If the channel |
916 * stream delegates all of its operations to the channel. If the channel |
913 * is in non-blocking mode then the output stream's <tt>write</tt> |
917 * is in non-blocking mode then the output stream's {@code write} |
914 * operations will throw an {@link |
918 * operations will throw an {@link |
915 * java.nio.channels.IllegalBlockingModeException}. |
919 * java.nio.channels.IllegalBlockingModeException}. |
916 * |
920 * |
917 * <p> Closing the returned {@link java.io.OutputStream OutputStream} |
921 * <p> Closing the returned {@link java.io.OutputStream OutputStream} |
918 * will close the associated socket. |
922 * will close the associated socket. |
1064 * Note, only limited support is provided for handling incoming urgent |
1068 * Note, only limited support is provided for handling incoming urgent |
1065 * data. In particular, no notification of incoming urgent data is provided |
1069 * data. In particular, no notification of incoming urgent data is provided |
1066 * and there is no capability to distinguish between normal data and urgent |
1070 * and there is no capability to distinguish between normal data and urgent |
1067 * data unless provided by a higher level protocol. |
1071 * data unless provided by a higher level protocol. |
1068 * |
1072 * |
1069 * @param on <code>true</code> to enable |
1073 * @param on {@code true} to enable |
1070 * {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}, |
1074 * {@link SocketOptions#SO_OOBINLINE SO_OOBINLINE}, |
1071 * <code>false</code> to disable. |
1075 * {@code false} to disable. |
1072 * |
1076 * |
1073 * @exception SocketException if there is an error |
1077 * @exception SocketException if there is an error |
1074 * in the underlying protocol, such as a TCP error. |
1078 * in the underlying protocol, such as a TCP error. |
1075 * |
1079 * |
1076 * @since 1.4 |
1080 * @since 1.4 |
1182 getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size)); |
1186 getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size)); |
1183 } |
1187 } |
1184 |
1188 |
1185 /** |
1189 /** |
1186 * Get value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option |
1190 * Get value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option |
1187 * for this <tt>Socket</tt>, that is the buffer size used by the platform |
1191 * for this {@code Socket}, that is the buffer size used by the platform |
1188 * for output on this <tt>Socket</tt>. |
1192 * for output on this {@code Socket}. |
1189 * @return the value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} |
1193 * @return the value of the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} |
1190 * option for this <tt>Socket</tt>. |
1194 * option for this {@code Socket}. |
1191 * |
1195 * |
1192 * @exception SocketException if there is an error |
1196 * @exception SocketException if there is an error |
1193 * in the underlying protocol, such as a TCP error. |
1197 * in the underlying protocol, such as a TCP error. |
1194 * |
1198 * |
1195 * @see #setSendBufferSize(int) |
1199 * @see #setSendBufferSize(int) |
1256 getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size)); |
1260 getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size)); |
1257 } |
1261 } |
1258 |
1262 |
1259 /** |
1263 /** |
1260 * Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option |
1264 * Gets the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option |
1261 * for this <tt>Socket</tt>, that is the buffer size used by the platform |
1265 * for this {@code Socket}, that is the buffer size used by the platform |
1262 * for input on this <tt>Socket</tt>. |
1266 * for input on this {@code Socket}. |
1263 * |
1267 * |
1264 * @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} |
1268 * @return the value of the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} |
1265 * option for this <tt>Socket</tt>. |
1269 * option for this {@code Socket}. |
1266 * @exception SocketException if there is an error |
1270 * @exception SocketException if there is an error |
1267 * in the underlying protocol, such as a TCP error. |
1271 * in the underlying protocol, such as a TCP error. |
1268 * @see #setReceiveBufferSize(int) |
1272 * @see #setReceiveBufferSize(int) |
1269 * @since 1.2 |
1273 * @since 1.2 |
1270 */ |
1274 */ |
1345 * So whether the type-of-service field can be changed after the |
1349 * So whether the type-of-service field can be changed after the |
1346 * TCP connection has been established depends on the implementation |
1350 * TCP connection has been established depends on the implementation |
1347 * in the underlying platform. Applications should not assume that |
1351 * in the underlying platform. Applications should not assume that |
1348 * they can change the TOS field after the connection. |
1352 * they can change the TOS field after the connection. |
1349 * <p> |
1353 * <p> |
1350 * For Internet Protocol v6 <code>tc</code> is the value that |
1354 * For Internet Protocol v6 {@code tc} is the value that |
1351 * would be placed into the sin6_flowinfo field of the IP header. |
1355 * would be placed into the sin6_flowinfo field of the IP header. |
1352 * |
1356 * |
1353 * @param tc an <code>int</code> value for the bitset. |
1357 * @param tc an {@code int} value for the bitset. |
1354 * @throws SocketException if there is an error setting the |
1358 * @throws SocketException if there is an error setting the |
1355 * traffic class or type-of-service |
1359 * traffic class or type-of-service |
1356 * @since 1.4 |
1360 * @since 1.4 |
1357 * @see #getTrafficClass |
1361 * @see #getTrafficClass |
1358 * @see SocketOptions#IP_TOS |
1362 * @see SocketOptions#IP_TOS |
1390 * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
1394 * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
1391 * socket option. |
1395 * socket option. |
1392 * <p> |
1396 * <p> |
1393 * When a TCP connection is closed the connection may remain |
1397 * When a TCP connection is closed the connection may remain |
1394 * in a timeout state for a period of time after the connection |
1398 * in a timeout state for a period of time after the connection |
1395 * is closed (typically known as the <tt>TIME_WAIT</tt> state |
1399 * is closed (typically known as the {@code TIME_WAIT} state |
1396 * or <tt>2MSL</tt> wait state). |
1400 * or {@code 2MSL} wait state). |
1397 * For applications using a well known socket address or port |
1401 * For applications using a well known socket address or port |
1398 * it may not be possible to bind a socket to the required |
1402 * it may not be possible to bind a socket to the required |
1399 * <tt>SocketAddress</tt> if there is a connection in the |
1403 * {@code SocketAddress} if there is a connection in the |
1400 * timeout state involving the socket address or port. |
1404 * timeout state involving the socket address or port. |
1401 * <p> |
1405 * <p> |
1402 * Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
1406 * Enabling {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} |
1403 * prior to binding the socket using {@link #bind(SocketAddress)} allows |
1407 * prior to binding the socket using {@link #bind(SocketAddress)} allows |
1404 * the socket to be bound even though a previous connection is in a timeout |
1408 * the socket to be bound even though a previous connection is in a timeout |
1405 * state. |
1409 * state. |
1406 * <p> |
1410 * <p> |
1407 * When a <tt>Socket</tt> is created the initial setting |
1411 * When a {@code Socket} is created the initial setting |
1408 * of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled. |
1412 * of {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is disabled. |
1409 * <p> |
1413 * <p> |
1410 * The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is |
1414 * The behaviour when {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR} is |
1411 * enabled or disabled after a socket is bound (See {@link #isBound()}) |
1415 * enabled or disabled after a socket is bound (See {@link #isBound()}) |
1412 * is not defined. |
1416 * is not defined. |
1627 /** |
1631 /** |
1628 * Sets the client socket implementation factory for the |
1632 * Sets the client socket implementation factory for the |
1629 * application. The factory can be specified only once. |
1633 * application. The factory can be specified only once. |
1630 * <p> |
1634 * <p> |
1631 * When an application creates a new client socket, the socket |
1635 * When an application creates a new client socket, the socket |
1632 * implementation factory's <code>createSocketImpl</code> method is |
1636 * implementation factory's {@code createSocketImpl} method is |
1633 * called to create the actual socket implementation. |
1637 * called to create the actual socket implementation. |
1634 * <p> |
1638 * <p> |
1635 * Passing <code>null</code> to the method is a no-op unless the factory |
1639 * Passing {@code null} to the method is a no-op unless the factory |
1636 * was already set. |
1640 * was already set. |
1637 * <p>If there is a security manager, this method first calls |
1641 * <p>If there is a security manager, this method first calls |
1638 * the security manager's <code>checkSetFactory</code> method |
1642 * the security manager's {@code checkSetFactory} method |
1639 * to ensure the operation is allowed. |
1643 * to ensure the operation is allowed. |
1640 * This could result in a SecurityException. |
1644 * This could result in a SecurityException. |
1641 * |
1645 * |
1642 * @param fac the desired factory. |
1646 * @param fac the desired factory. |
1643 * @exception IOException if an I/O error occurs when setting the |
1647 * @exception IOException if an I/O error occurs when setting the |
1644 * socket factory. |
1648 * socket factory. |
1645 * @exception SocketException if the factory is already defined. |
1649 * @exception SocketException if the factory is already defined. |
1646 * @exception SecurityException if a security manager exists and its |
1650 * @exception SecurityException if a security manager exists and its |
1647 * <code>checkSetFactory</code> method doesn't allow the operation. |
1651 * {@code checkSetFactory} method doesn't allow the operation. |
1648 * @see java.net.SocketImplFactory#createSocketImpl() |
1652 * @see java.net.SocketImplFactory#createSocketImpl() |
1649 * @see SecurityManager#checkSetFactory |
1653 * @see SecurityManager#checkSetFactory |
1650 */ |
1654 */ |
1651 public static synchronized void setSocketImplFactory(SocketImplFactory fac) |
1655 public static synchronized void setSocketImplFactory(SocketImplFactory fac) |
1652 throws IOException |
1656 throws IOException |
1676 * are irrelevant; in order to choose a protocol the values are simply |
1680 * are irrelevant; in order to choose a protocol the values are simply |
1677 * compared, with larger values indicating stronger preferences. Negative |
1681 * compared, with larger values indicating stronger preferences. Negative |
1678 * values represent a lower priority than positive values. If the |
1682 * values represent a lower priority than positive values. If the |
1679 * application prefers short connection time over both low latency and high |
1683 * application prefers short connection time over both low latency and high |
1680 * bandwidth, for example, then it could invoke this method with the values |
1684 * bandwidth, for example, then it could invoke this method with the values |
1681 * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low |
1685 * {@code (1, 0, 0)}. If the application prefers high bandwidth above low |
1682 * latency, and low latency above short connection time, then it could |
1686 * latency, and low latency above short connection time, then it could |
1683 * invoke this method with the values <tt>(0, 1, 2)</tt>. |
1687 * invoke this method with the values {@code (0, 1, 2)}. |
1684 * |
1688 * |
1685 * <p> Invoking this method after this socket has been connected |
1689 * <p> Invoking this method after this socket has been connected |
1686 * will have no effect. |
1690 * will have no effect. |
1687 * |
1691 * |
1688 * @param connectionTime |
1692 * @param connectionTime |
1689 * An <tt>int</tt> expressing the relative importance of a short |
1693 * An {@code int} expressing the relative importance of a short |
1690 * connection time |
1694 * connection time |
1691 * |
1695 * |
1692 * @param latency |
1696 * @param latency |
1693 * An <tt>int</tt> expressing the relative importance of low |
1697 * An {@code int} expressing the relative importance of low |
1694 * latency |
1698 * latency |
1695 * |
1699 * |
1696 * @param bandwidth |
1700 * @param bandwidth |
1697 * An <tt>int</tt> expressing the relative importance of high |
1701 * An {@code int} expressing the relative importance of high |
1698 * bandwidth |
1702 * bandwidth |
1699 * |
1703 * |
1700 * @since 1.5 |
1704 * @since 1.5 |
1701 */ |
1705 */ |
1702 public void setPerformancePreferences(int connectionTime, |
1706 public void setPerformancePreferences(int connectionTime, |