PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jre-1.6.0/src/java/net/Socket.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1374 lines | 483 code | 72 blank | 819 comment | 121 complexity | 22a8cbe42e854518c9c723752da6f5ff MD5 | raw file
  1. /*
  2. * %W% %E%
  3. *
  4. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  5. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.IOException;
  11. import java.io.InterruptedIOException;
  12. import java.nio.channels.SocketChannel;
  13. import java.security.AccessController;
  14. import java.security.PrivilegedExceptionAction;
  15. import java.security.PrivilegedAction;
  16. /**
  17. * This class implements client sockets (also called just
  18. * "sockets"). A socket is an endpoint for communication
  19. * between two machines.
  20. * <p>
  21. * The actual work of the socket is performed by an instance of the
  22. * <code>SocketImpl</code> class. An application, by changing
  23. * the socket factory that creates the socket implementation,
  24. * can configure itself to create sockets appropriate to the local
  25. * firewall.
  26. *
  27. * @author unascribed
  28. * @version 1.115, 09/05/07
  29. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  30. * @see java.net.SocketImpl
  31. * @see java.nio.channels.SocketChannel
  32. * @since JDK1.0
  33. */
  34. public
  35. class Socket {
  36. /**
  37. * Various states of this socket.
  38. */
  39. private boolean created = false;
  40. private boolean bound = false;
  41. private boolean connected = false;
  42. private boolean closed = false;
  43. private Object closeLock = new Object();
  44. private boolean shutIn = false;
  45. private boolean shutOut = false;
  46. /**
  47. * The implementation of this Socket.
  48. */
  49. SocketImpl impl;
  50. /**
  51. * Are we using an older SocketImpl?
  52. */
  53. private boolean oldImpl = false;
  54. /**
  55. * Creates an unconnected socket, with the
  56. * system-default type of SocketImpl.
  57. *
  58. * @since JDK1.1
  59. * @revised 1.4
  60. */
  61. public Socket() {
  62. setImpl();
  63. }
  64. /**
  65. * Creates an unconnected socket, specifying the type of proxy, if any,
  66. * that should be used regardless of any other settings.
  67. * <P>
  68. * If there is a security manager, its <code>checkConnect</code> method
  69. * is called with the proxy host address and port number
  70. * as its arguments. This could result in a SecurityException.
  71. * <P>
  72. * Examples:
  73. * <UL> <LI><code>Socket s = new Socket(Proxy.NO_PROXY);</code> will create
  74. * a plain socket ignoring any other proxy configuration.</LI>
  75. * <LI><code>Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));</code>
  76. * will create a socket connecting through the specified SOCKS proxy
  77. * server.</LI>
  78. * </UL>
  79. *
  80. * @param proxy a {@link java.net.Proxy Proxy} object specifying what kind
  81. * of proxying should be used.
  82. * @throws IllegalArgumentException if the proxy is of an invalid type
  83. * or <code>null</code>.
  84. * @throws SecurityException if a security manager is present and
  85. * permission to connect to the proxy is
  86. * denied.
  87. * @see java.net.ProxySelector
  88. * @see java.net.Proxy
  89. *
  90. * @since 1.5
  91. */
  92. public Socket(Proxy proxy) {
  93. // Create a copy of Proxy as a security measure
  94. if (proxy == null) {
  95. throw new IllegalArgumentException("Invalid Proxy");
  96. }
  97. Proxy p = proxy == Proxy.NO_PROXY ? proxy : sun.net.ApplicationProxy.create(proxy);
  98. if (p.type() == Proxy.Type.SOCKS) {
  99. SecurityManager security = System.getSecurityManager();
  100. InetSocketAddress epoint = (InetSocketAddress) p.address();
  101. if (epoint.getAddress() != null) {
  102. checkAddress(epoint.getAddress(), "Socket");
  103. }
  104. if (security != null) {
  105. if (epoint.isUnresolved())
  106. epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort());
  107. if (epoint.isUnresolved())
  108. security.checkConnect(epoint.getHostName(), epoint.getPort());
  109. else
  110. security.checkConnect(epoint.getAddress().getHostAddress(),
  111. epoint.getPort());
  112. }
  113. impl = new SocksSocketImpl(p);
  114. impl.setSocket(this);
  115. } else {
  116. if (p == Proxy.NO_PROXY) {
  117. if (factory == null) {
  118. impl = new PlainSocketImpl();
  119. impl.setSocket(this);
  120. } else
  121. setImpl();
  122. } else
  123. throw new IllegalArgumentException("Invalid Proxy");
  124. }
  125. }
  126. /**
  127. * Creates an unconnected Socket with a user-specified
  128. * SocketImpl.
  129. * <P>
  130. * @param impl an instance of a <B>SocketImpl</B>
  131. * the subclass wishes to use on the Socket.
  132. *
  133. * @exception SocketException if there is an error in the underlying protocol,
  134. * such as a TCP error.
  135. * @since JDK1.1
  136. */
  137. protected Socket(SocketImpl impl) throws SocketException {
  138. this.impl = impl;
  139. if (impl != null) {
  140. checkOldImpl();
  141. this.impl.setSocket(this);
  142. }
  143. }
  144. /**
  145. * Creates a stream socket and connects it to the specified port
  146. * number on the named host.
  147. * <p>
  148. * If the specified host is <tt>null</tt> it is the equivalent of
  149. * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
  150. * In other words, it is equivalent to specifying an address of the
  151. * loopback interface. </p>
  152. * <p>
  153. * If the application has specified a server socket factory, that
  154. * factory's <code>createSocketImpl</code> method is called to create
  155. * the actual socket implementation. Otherwise a "plain" socket is created.
  156. * <p>
  157. * If there is a security manager, its
  158. * <code>checkConnect</code> method is called
  159. * with the host address and <code>port</code>
  160. * as its arguments. This could result in a SecurityException.
  161. *
  162. * @param host the host name, or <code>null</code> for the loopback address.
  163. * @param port the port number.
  164. *
  165. * @exception UnknownHostException if the IP address of
  166. * the host could not be determined.
  167. *
  168. * @exception IOException if an I/O error occurs when creating the socket.
  169. * @exception SecurityException if a security manager exists and its
  170. * <code>checkConnect</code> method doesn't allow the operation.
  171. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  172. * @see java.net.SocketImpl
  173. * @see java.net.SocketImplFactory#createSocketImpl()
  174. * @see SecurityManager#checkConnect
  175. */
  176. public Socket(String host, int port)
  177. throws UnknownHostException, IOException
  178. {
  179. this(host != null ? new InetSocketAddress(host, port) :
  180. new InetSocketAddress(InetAddress.getByName(null), port),
  181. (SocketAddress) null, true);
  182. }
  183. /**
  184. * Creates a stream socket and connects it to the specified port
  185. * number at the specified IP address.
  186. * <p>
  187. * If the application has specified a socket factory, that factory's
  188. * <code>createSocketImpl</code> method is called to create the
  189. * actual socket implementation. Otherwise a "plain" socket is created.
  190. * <p>
  191. * If there is a security manager, its
  192. * <code>checkConnect</code> method is called
  193. * with the host address and <code>port</code>
  194. * as its arguments. This could result in a SecurityException.
  195. *
  196. * @param address the IP address.
  197. * @param port the port number.
  198. * @exception IOException if an I/O error occurs when creating the socket.
  199. * @exception SecurityException if a security manager exists and its
  200. * <code>checkConnect</code> method doesn't allow the operation.
  201. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  202. * @see java.net.SocketImpl
  203. * @see java.net.SocketImplFactory#createSocketImpl()
  204. * @see SecurityManager#checkConnect
  205. */
  206. public Socket(InetAddress address, int port) throws IOException {
  207. this(address != null ? new InetSocketAddress(address, port) : null,
  208. (SocketAddress) null, true);
  209. }
  210. /**
  211. * Creates a socket and connects it to the specified remote host on
  212. * the specified remote port. The Socket will also bind() to the local
  213. * address and port supplied.
  214. * <p>
  215. * If the specified host is <tt>null</tt> it is the equivalent of
  216. * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
  217. * In other words, it is equivalent to specifying an address of the
  218. * loopback interface. </p>
  219. * <p>
  220. * If there is a security manager, its
  221. * <code>checkConnect</code> method is called
  222. * with the host address and <code>port</code>
  223. * as its arguments. This could result in a SecurityException.
  224. *
  225. * @param host the name of the remote host, or <code>null</code> for the loopback address.
  226. * @param port the remote port
  227. * @param localAddr the local address the socket is bound to
  228. * @param localPort the local port the socket is bound to
  229. * @exception IOException if an I/O error occurs when creating the socket.
  230. * @exception SecurityException if a security manager exists and its
  231. * <code>checkConnect</code> method doesn't allow the operation.
  232. * @see SecurityManager#checkConnect
  233. * @since JDK1.1
  234. */
  235. public Socket(String host, int port, InetAddress localAddr,
  236. int localPort) throws IOException {
  237. this(host != null ? new InetSocketAddress(host, port) :
  238. new InetSocketAddress(InetAddress.getByName(null), port),
  239. new InetSocketAddress(localAddr, localPort), true);
  240. }
  241. /**
  242. * Creates a socket and connects it to the specified remote address on
  243. * the specified remote port. The Socket will also bind() to the local
  244. * address and port supplied.
  245. * <p>
  246. * If there is a security manager, its
  247. * <code>checkConnect</code> method is called
  248. * with the host address and <code>port</code>
  249. * as its arguments. This could result in a SecurityException.
  250. *
  251. * @param address the remote address
  252. * @param port the remote port
  253. * @param localAddr the local address the socket is bound to
  254. * @param localPort the local port the socket is bound to
  255. * @exception IOException if an I/O error occurs when creating the socket.
  256. * @exception SecurityException if a security manager exists and its
  257. * <code>checkConnect</code> method doesn't allow the operation.
  258. * @see SecurityManager#checkConnect
  259. * @since JDK1.1
  260. */
  261. public Socket(InetAddress address, int port, InetAddress localAddr,
  262. int localPort) throws IOException {
  263. this(address != null ? new InetSocketAddress(address, port) : null,
  264. new InetSocketAddress(localAddr, localPort), true);
  265. }
  266. /**
  267. * Creates a stream socket and connects it to the specified port
  268. * number on the named host.
  269. * <p>
  270. * If the specified host is <tt>null</tt> it is the equivalent of
  271. * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>.
  272. * In other words, it is equivalent to specifying an address of the
  273. * loopback interface. </p>
  274. * <p>
  275. * If the stream argument is <code>true</code>, this creates a
  276. * stream socket. If the stream argument is <code>false</code>, it
  277. * creates a datagram socket.
  278. * <p>
  279. * If the application has specified a server socket factory, that
  280. * factory's <code>createSocketImpl</code> method is called to create
  281. * the actual socket implementation. Otherwise a "plain" socket is created.
  282. * <p>
  283. * If there is a security manager, its
  284. * <code>checkConnect</code> method is called
  285. * with the host address and <code>port</code>
  286. * as its arguments. This could result in a SecurityException.
  287. * <p>
  288. * If a UDP socket is used, TCP/IP related socket options will not apply.
  289. *
  290. * @param host the host name, or <code>null</code> for the loopback address.
  291. * @param port the port number.
  292. * @param stream a <code>boolean</code> indicating whether this is
  293. * a stream socket or a datagram socket.
  294. * @exception IOException if an I/O error occurs when creating the socket.
  295. * @exception SecurityException if a security manager exists and its
  296. * <code>checkConnect</code> method doesn't allow the operation.
  297. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  298. * @see java.net.SocketImpl
  299. * @see java.net.SocketImplFactory#createSocketImpl()
  300. * @see SecurityManager#checkConnect
  301. * @deprecated Use DatagramSocket instead for UDP transport.
  302. */
  303. @Deprecated
  304. public Socket(String host, int port, boolean stream) throws IOException {
  305. this(host != null ? new InetSocketAddress(host, port) :
  306. new InetSocketAddress(InetAddress.getByName(null), port),
  307. (SocketAddress) null, stream);
  308. }
  309. /**
  310. * Creates a socket and connects it to the specified port number at
  311. * the specified IP address.
  312. * <p>
  313. * If the stream argument is <code>true</code>, this creates a
  314. * stream socket. If the stream argument is <code>false</code>, it
  315. * creates a datagram socket.
  316. * <p>
  317. * If the application has specified a server socket factory, that
  318. * factory's <code>createSocketImpl</code> method is called to create
  319. * the actual socket implementation. Otherwise a "plain" socket is created.
  320. *
  321. * <p>If there is a security manager, its
  322. * <code>checkConnect</code> method is called
  323. * with <code>host.getHostAddress()</code> and <code>port</code>
  324. * as its arguments. This could result in a SecurityException.
  325. * <p>
  326. * If UDP socket is used, TCP/IP related socket options will not apply.
  327. *
  328. * @param host the IP address.
  329. * @param port the port number.
  330. * @param stream if <code>true</code>, create a stream socket;
  331. * otherwise, create a datagram socket.
  332. * @exception IOException if an I/O error occurs when creating the socket.
  333. * @exception SecurityException if a security manager exists and its
  334. * <code>checkConnect</code> method doesn't allow the operation.
  335. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  336. * @see java.net.SocketImpl
  337. * @see java.net.SocketImplFactory#createSocketImpl()
  338. * @see SecurityManager#checkConnect
  339. * @deprecated Use DatagramSocket instead for UDP transport.
  340. */
  341. @Deprecated
  342. public Socket(InetAddress host, int port, boolean stream) throws IOException {
  343. this(host != null ? new InetSocketAddress(host, port) : null,
  344. new InetSocketAddress(0), stream);
  345. }
  346. private Socket(SocketAddress address, SocketAddress localAddr,
  347. boolean stream) throws IOException {
  348. setImpl();
  349. // backward compatibility
  350. if (address == null)
  351. throw new NullPointerException();
  352. try {
  353. createImpl(stream);
  354. if (localAddr != null)
  355. bind(localAddr);
  356. if (address != null)
  357. connect(address);
  358. } catch (IOException e) {
  359. close();
  360. throw e;
  361. }
  362. }
  363. /**
  364. * Creates the socket implementation.
  365. *
  366. * @param stream a <code>boolean</code> value : <code>true</code> for a TCP socket,
  367. * <code>false</code> for UDP.
  368. * @throws IOException if creation fails
  369. * @since 1.4
  370. */
  371. void createImpl(boolean stream) throws SocketException {
  372. if (impl == null)
  373. setImpl();
  374. try {
  375. impl.create(stream);
  376. created = true;
  377. } catch (IOException e) {
  378. throw new SocketException(e.getMessage());
  379. }
  380. }
  381. private void checkOldImpl() {
  382. if (impl == null)
  383. return;
  384. // SocketImpl.connect() is a protected method, therefore we need to use
  385. // getDeclaredMethod, therefore we need permission to access the member
  386. oldImpl = AccessController.doPrivileged
  387. (new PrivilegedAction<Boolean>() {
  388. public Boolean run() {
  389. Class[] cl = new Class[2];
  390. cl[0] = SocketAddress.class;
  391. cl[1] = Integer.TYPE;
  392. Class clazz = impl.getClass();
  393. while (true) {
  394. try {
  395. clazz.getDeclaredMethod("connect", cl);
  396. return Boolean.FALSE;
  397. } catch (NoSuchMethodException e) {
  398. clazz = clazz.getSuperclass();
  399. // java.net.SocketImpl class will always have this abstract method.
  400. // If we have not found it by now in the hierarchy then it does not
  401. // exist, we are an old style impl.
  402. if (clazz.equals(java.net.SocketImpl.class)) {
  403. return Boolean.TRUE;
  404. }
  405. }
  406. }
  407. }
  408. });
  409. }
  410. /**
  411. * Sets impl to the system-default type of SocketImpl.
  412. * @since 1.4
  413. */
  414. void setImpl() {
  415. if (factory != null) {
  416. impl = factory.createSocketImpl();
  417. checkOldImpl();
  418. } else {
  419. // No need to do a checkOldImpl() here, we know it's an up to date
  420. // SocketImpl!
  421. impl = new SocksSocketImpl();
  422. }
  423. if (impl != null)
  424. impl.setSocket(this);
  425. }
  426. /**
  427. * Get the <code>SocketImpl</code> attached to this socket, creating
  428. * it if necessary.
  429. *
  430. * @return the <code>SocketImpl</code> attached to that ServerSocket.
  431. * @throws SocketException if creation fails
  432. * @since 1.4
  433. */
  434. SocketImpl getImpl() throws SocketException {
  435. if (!created)
  436. createImpl(true);
  437. return impl;
  438. }
  439. /**
  440. * Connects this socket to the server.
  441. *
  442. * @param endpoint the <code>SocketAddress</code>
  443. * @throws IOException if an error occurs during the connection
  444. * @throws java.nio.channels.IllegalBlockingModeException
  445. * if this socket has an associated channel,
  446. * and the channel is in non-blocking mode
  447. * @throws IllegalArgumentException if endpoint is null or is a
  448. * SocketAddress subclass not supported by this socket
  449. * @since 1.4
  450. * @spec JSR-51
  451. */
  452. public void connect(SocketAddress endpoint) throws IOException {
  453. connect(endpoint, 0);
  454. }
  455. /**
  456. * Connects this socket to the server with a specified timeout value.
  457. * A timeout of zero is interpreted as an infinite timeout. The connection
  458. * will then block until established or an error occurs.
  459. *
  460. * @param endpoint the <code>SocketAddress</code>
  461. * @param timeout the timeout value to be used in milliseconds.
  462. * @throws IOException if an error occurs during the connection
  463. * @throws SocketTimeoutException if timeout expires before connecting
  464. * @throws java.nio.channels.IllegalBlockingModeException
  465. * if this socket has an associated channel,
  466. * and the channel is in non-blocking mode
  467. * @throws IllegalArgumentException if endpoint is null or is a
  468. * SocketAddress subclass not supported by this socket
  469. * @since 1.4
  470. * @spec JSR-51
  471. */
  472. public void connect(SocketAddress endpoint, int timeout) throws IOException {
  473. if (endpoint == null)
  474. throw new IllegalArgumentException("connect: The address can't be null");
  475. if (timeout < 0)
  476. throw new IllegalArgumentException("connect: timeout can't be negative");
  477. if (isClosed())
  478. throw new SocketException("Socket is closed");
  479. if (!oldImpl && isConnected())
  480. throw new SocketException("already connected");
  481. if (!(endpoint instanceof InetSocketAddress))
  482. throw new IllegalArgumentException("Unsupported address type");
  483. InetSocketAddress epoint = (InetSocketAddress) endpoint;
  484. InetAddress addr = epoint.getAddress ();
  485. int port = epoint.getPort();
  486. checkAddress(addr, "connect");
  487. SecurityManager security = System.getSecurityManager();
  488. if (security != null) {
  489. if (epoint.isUnresolved())
  490. security.checkConnect(epoint.getHostName(), port);
  491. else
  492. security.checkConnect(addr.getHostAddress(),port);
  493. }
  494. if (!created)
  495. createImpl(true);
  496. if (!oldImpl)
  497. impl.connect(epoint, timeout);
  498. else if (timeout == 0) {
  499. if (epoint.isUnresolved())
  500. impl.connect(addr.getHostName(),port);
  501. else
  502. impl.connect(addr, port);
  503. } else
  504. throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)");
  505. connected = true;
  506. /*
  507. * If the socket was not bound before the connect, it is now because
  508. * the kernel will have picked an ephemeral port & a local address
  509. */
  510. bound = true;
  511. }
  512. /**
  513. * Binds the socket to a local address.
  514. * <P>
  515. * If the address is <code>null</code>, then the system will pick up
  516. * an ephemeral port and a valid local address to bind the socket.
  517. *
  518. * @param bindpoint the <code>SocketAddress</code> to bind to
  519. * @throws IOException if the bind operation fails, or if the socket
  520. * is already bound.
  521. * @throws IllegalArgumentException if bindpoint is a
  522. * SocketAddress subclass not supported by this socket
  523. *
  524. * @since 1.4
  525. * @see #isBound
  526. */
  527. public void bind(SocketAddress bindpoint) throws IOException {
  528. if (isClosed())
  529. throw new SocketException("Socket is closed");
  530. if (!oldImpl && isBound())
  531. throw new SocketException("Already bound");
  532. if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress)))
  533. throw new IllegalArgumentException("Unsupported address type");
  534. InetSocketAddress epoint = (InetSocketAddress) bindpoint;
  535. if (epoint != null && epoint.isUnresolved())
  536. throw new SocketException("Unresolved address");
  537. if (epoint == null) {
  538. epoint = new InetSocketAddress(0);
  539. }
  540. InetAddress addr = epoint.getAddress();
  541. int port = epoint.getPort();
  542. checkAddress (addr, "bind");
  543. getImpl().bind (addr, port);
  544. bound = true;
  545. }
  546. private void checkAddress (InetAddress addr, String op) {
  547. if (addr == null) {
  548. return;
  549. }
  550. if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
  551. throw new IllegalArgumentException(op + ": invalid address type");
  552. }
  553. }
  554. /**
  555. * set the flags after an accept() call.
  556. */
  557. final void postAccept() {
  558. connected = true;
  559. created = true;
  560. bound = true;
  561. }
  562. void setCreated() {
  563. created = true;
  564. }
  565. void setBound() {
  566. bound = true;
  567. }
  568. void setConnected() {
  569. connected = true;
  570. }
  571. /**
  572. * Returns the address to which the socket is connected.
  573. *
  574. * @return the remote IP address to which this socket is connected,
  575. * or <code>null</code> if the socket is not connected.
  576. */
  577. public InetAddress getInetAddress() {
  578. if (!isConnected())
  579. return null;
  580. try {
  581. return getImpl().getInetAddress();
  582. } catch (SocketException e) {
  583. }
  584. return null;
  585. }
  586. /**
  587. * Gets the local address to which the socket is bound.
  588. *
  589. * @return the local address to which the socket is bound or
  590. * <code>InetAddress.anyLocalAddress()</code>
  591. * if the socket is not bound yet.
  592. * @since JDK1.1
  593. */
  594. public InetAddress getLocalAddress() {
  595. // This is for backward compatibility
  596. if (!isBound())
  597. return InetAddress.anyLocalAddress();
  598. InetAddress in = null;
  599. try {
  600. in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
  601. if (in.isAnyLocalAddress()) {
  602. in = InetAddress.anyLocalAddress();
  603. }
  604. } catch (Exception e) {
  605. in = InetAddress.anyLocalAddress(); // "0.0.0.0"
  606. }
  607. return in;
  608. }
  609. /**
  610. * Returns the remote port to which this socket is connected.
  611. *
  612. * @return the remote port number to which this socket is connected, or
  613. * 0 if the socket is not connected yet.
  614. */
  615. public int getPort() {
  616. if (!isConnected())
  617. return 0;
  618. try {
  619. return getImpl().getPort();
  620. } catch (SocketException e) {
  621. // Shouldn't happen as we're connected
  622. }
  623. return -1;
  624. }
  625. /**
  626. * Returns the local port to which this socket is bound.
  627. *
  628. * @return the local port number to which this socket is bound or -1
  629. * if the socket is not bound yet.
  630. */
  631. public int getLocalPort() {
  632. if (!isBound())
  633. return -1;
  634. try {
  635. return getImpl().getLocalPort();
  636. } catch(SocketException e) {
  637. // shouldn't happen as we're bound
  638. }
  639. return -1;
  640. }
  641. /**
  642. * Returns the address of the endpoint this socket is connected to, or
  643. * <code>null</code> if it is unconnected.
  644. * @return a <code>SocketAddress</code> reprensenting the remote endpoint of this
  645. * socket, or <code>null</code> if it is not connected yet.
  646. * @see #getInetAddress()
  647. * @see #getPort()
  648. * @see #connect(SocketAddress, int)
  649. * @see #connect(SocketAddress)
  650. * @since 1.4
  651. */
  652. public SocketAddress getRemoteSocketAddress() {
  653. if (!isConnected())
  654. return null;
  655. return new InetSocketAddress(getInetAddress(), getPort());
  656. }
  657. /**
  658. * Returns the address of the endpoint this socket is bound to, or
  659. * <code>null</code> if it is not bound yet.
  660. *
  661. * @return a <code>SocketAddress</code> representing the local endpoint of this
  662. * socket, or <code>null</code> if it is not bound yet.
  663. * @see #getLocalAddress()
  664. * @see #getLocalPort()
  665. * @see #bind(SocketAddress)
  666. * @since 1.4
  667. */
  668. public SocketAddress getLocalSocketAddress() {
  669. if (!isBound())
  670. return null;
  671. return new InetSocketAddress(getLocalAddress(), getLocalPort());
  672. }
  673. /**
  674. * Returns the unique {@link java.nio.channels.SocketChannel SocketChannel}
  675. * object associated with this socket, if any.
  676. *
  677. * <p> A socket will have a channel if, and only if, the channel itself was
  678. * created via the {@link java.nio.channels.SocketChannel#open
  679. * SocketChannel.open} or {@link
  680. * java.nio.channels.ServerSocketChannel#accept ServerSocketChannel.accept}
  681. * methods.
  682. *
  683. * @return the socket channel associated with this socket,
  684. * or <tt>null</tt> if this socket was not created
  685. * for a channel
  686. *
  687. * @since 1.4
  688. * @spec JSR-51
  689. */
  690. public SocketChannel getChannel() {
  691. return null;
  692. }
  693. /**
  694. * Returns an input stream for this socket.
  695. *
  696. * <p> If this socket has an associated channel then the resulting input
  697. * stream delegates all of its operations to the channel. If the channel
  698. * is in non-blocking mode then the input stream's <tt>read</tt> operations
  699. * will throw an {@link java.nio.channels.IllegalBlockingModeException}.
  700. *
  701. * <p>Under abnormal conditions the underlying connection may be
  702. * broken by the remote host or the network software (for example
  703. * a connection reset in the case of TCP connections). When a
  704. * broken connection is detected by the network software the
  705. * following applies to the returned input stream :-
  706. *
  707. * <ul>
  708. *
  709. * <li><p>The network software may discard bytes that are buffered
  710. * by the socket. Bytes that aren't discarded by the network
  711. * software can be read using {@link java.io.InputStream#read read}.
  712. *
  713. * <li><p>If there are no bytes buffered on the socket, or all
  714. * buffered bytes have been consumed by
  715. * {@link java.io.InputStream#read read}, then all subsequent
  716. * calls to {@link java.io.InputStream#read read} will throw an
  717. * {@link java.io.IOException IOException}.
  718. *
  719. * <li><p>If there are no bytes buffered on the socket, and the
  720. * socket has not been closed using {@link #close close}, then
  721. * {@link java.io.InputStream#available available} will
  722. * return <code>0</code>.
  723. *
  724. * </ul>
  725. *
  726. * <p> Closing the returned {@link java.io.InputStream InputStream}
  727. * will close the associated socket.
  728. *
  729. * @return an input stream for reading bytes from this socket.
  730. * @exception IOException if an I/O error occurs when creating the
  731. * input stream, the socket is closed, the socket is
  732. * not connected, or the socket input has been shutdown
  733. * using {@link #shutdownInput()}
  734. *
  735. * @revised 1.4
  736. * @spec JSR-51
  737. */
  738. public InputStream getInputStream() throws IOException {
  739. if (isClosed())
  740. throw new SocketException("Socket is closed");
  741. if (!isConnected())
  742. throw new SocketException("Socket is not connected");
  743. if (isInputShutdown())
  744. throw new SocketException("Socket input is shutdown");
  745. final Socket s = this;
  746. InputStream is = null;
  747. try {
  748. is = (InputStream)
  749. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  750. public Object run() throws IOException {
  751. return impl.getInputStream();
  752. }
  753. });
  754. } catch (java.security.PrivilegedActionException e) {
  755. throw (IOException) e.getException();
  756. }
  757. return is;
  758. }
  759. /**
  760. * Returns an output stream for this socket.
  761. *
  762. * <p> If this socket has an associated channel then the resulting output
  763. * stream delegates all of its operations to the channel. If the channel
  764. * is in non-blocking mode then the output stream's <tt>write</tt>
  765. * operations will throw an {@link
  766. * java.nio.channels.IllegalBlockingModeException}.
  767. *
  768. * <p> Closing the returned {@link java.io.OutputStream OutputStream}
  769. * will close the associated socket.
  770. *
  771. * @return an output stream for writing bytes to this socket.
  772. * @exception IOException if an I/O error occurs when creating the
  773. * output stream or if the socket is not connected.
  774. * @revised 1.4
  775. * @spec JSR-51
  776. */
  777. public OutputStream getOutputStream() throws IOException {
  778. if (isClosed())
  779. throw new SocketException("Socket is closed");
  780. if (!isConnected())
  781. throw new SocketException("Socket is not connected");
  782. if (isOutputShutdown())
  783. throw new SocketException("Socket output is shutdown");
  784. final Socket s = this;
  785. OutputStream os = null;
  786. try {
  787. os = (OutputStream)
  788. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  789. public Object run() throws IOException {
  790. return impl.getOutputStream();
  791. }
  792. });
  793. } catch (java.security.PrivilegedActionException e) {
  794. throw (IOException) e.getException();
  795. }
  796. return os;
  797. }
  798. /**
  799. * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
  800. *
  801. * @param on <code>true</code> to enable TCP_NODELAY,
  802. * <code>false</code> to disable.
  803. *
  804. * @exception SocketException if there is an error
  805. * in the underlying protocol, such as a TCP error.
  806. *
  807. * @since JDK1.1
  808. *
  809. * @see #getTcpNoDelay()
  810. */
  811. public void setTcpNoDelay(boolean on) throws SocketException {
  812. if (isClosed())
  813. throw new SocketException("Socket is closed");
  814. getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
  815. }
  816. /**
  817. * Tests if TCP_NODELAY is enabled.
  818. *
  819. * @return a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled.
  820. * @exception SocketException if there is an error
  821. * in the underlying protocol, such as a TCP error.
  822. * @since JDK1.1
  823. * @see #setTcpNoDelay(boolean)
  824. */
  825. public boolean getTcpNoDelay() throws SocketException {
  826. if (isClosed())
  827. throw new SocketException("Socket is closed");
  828. return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();
  829. }
  830. /**
  831. * Enable/disable SO_LINGER with the specified linger time in seconds.
  832. * The maximum timeout value is platform specific.
  833. *
  834. * The setting only affects socket close.
  835. *
  836. * @param on whether or not to linger on.
  837. * @param linger how long to linger for, if on is true.
  838. * @exception SocketException if there is an error
  839. * in the underlying protocol, such as a TCP error.
  840. * @exception IllegalArgumentException if the linger value is negative.
  841. * @since JDK1.1
  842. * @see #getSoLinger()
  843. */
  844. public void setSoLinger(boolean on, int linger) throws SocketException {
  845. if (isClosed())
  846. throw new SocketException("Socket is closed");
  847. if (!on) {
  848. getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));
  849. } else {
  850. if (linger < 0) {
  851. throw new IllegalArgumentException("invalid value for SO_LINGER");
  852. }
  853. if (linger > 65535)
  854. linger = 65535;
  855. getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
  856. }
  857. }
  858. /**
  859. * Returns setting for SO_LINGER. -1 returns implies that the
  860. * option is disabled.
  861. *
  862. * The setting only affects socket close.
  863. *
  864. * @return the setting for SO_LINGER.
  865. * @exception SocketException if there is an error
  866. * in the underlying protocol, such as a TCP error.
  867. * @since JDK1.1
  868. * @see #setSoLinger(boolean, int)
  869. */
  870. public int getSoLinger() throws SocketException {
  871. if (isClosed())
  872. throw new SocketException("Socket is closed");
  873. Object o = getImpl().getOption(SocketOptions.SO_LINGER);
  874. if (o instanceof Integer) {
  875. return ((Integer) o).intValue();
  876. } else {
  877. return -1;
  878. }
  879. }
  880. /**
  881. * Send one byte of urgent data on the socket. The byte to be sent is the lowest eight
  882. * bits of the data parameter. The urgent byte is
  883. * sent after any preceding writes to the socket OutputStream
  884. * and before any future writes to the OutputStream.
  885. * @param data The byte of data to send
  886. * @exception IOException if there is an error
  887. * sending the data.
  888. * @since 1.4
  889. */
  890. public void sendUrgentData (int data) throws IOException {
  891. if (!getImpl().supportsUrgentData ()) {
  892. throw new SocketException ("Urgent data not supported");
  893. }
  894. getImpl().sendUrgentData (data);
  895. }
  896. /**
  897. * Enable/disable OOBINLINE (receipt of TCP urgent data)
  898. *
  899. * By default, this option is disabled and TCP urgent data received on a
  900. * socket is silently discarded. If the user wishes to receive urgent data, then
  901. * this option must be enabled. When enabled, urgent data is received
  902. * inline with normal data.
  903. * <p>
  904. * Note, only limited support is provided for handling incoming urgent
  905. * data. In particular, no notification of incoming urgent data is provided
  906. * and there is no capability to distinguish between normal data and urgent
  907. * data unless provided by a higher level protocol.
  908. *
  909. * @param on <code>true</code> to enable OOBINLINE,
  910. * <code>false</code> to disable.
  911. *
  912. * @exception SocketException if there is an error
  913. * in the underlying protocol, such as a TCP error.
  914. *
  915. * @since 1.4
  916. *
  917. * @see #getOOBInline()
  918. */
  919. public void setOOBInline(boolean on) throws SocketException {
  920. if (isClosed())
  921. throw new SocketException("Socket is closed");
  922. getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on));
  923. }
  924. /**
  925. * Tests if OOBINLINE is enabled.
  926. *
  927. * @return a <code>boolean</code> indicating whether or not OOBINLINE is enabled.
  928. * @exception SocketException if there is an error
  929. * in the underlying protocol, such as a TCP error.
  930. * @since 1.4
  931. * @see #setOOBInline(boolean)
  932. */
  933. public boolean getOOBInline() throws SocketException {
  934. if (isClosed())
  935. throw new SocketException("Socket is closed");
  936. return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();
  937. }
  938. /**
  939. * Enable/disable SO_TIMEOUT with the specified timeout, in
  940. * milliseconds. With this option set to a non-zero timeout,
  941. * a read() call on the InputStream associated with this Socket
  942. * will block for only this amount of time. If the timeout expires,
  943. * a <B>java.net.SocketTimeoutException</B> is raised, though the
  944. * Socket is still valid. The option <B>must</B> be enabled
  945. * prior to entering the blocking operation to have effect. The
  946. * timeout must be > 0.
  947. * A timeout of zero is interpreted as an infinite timeout.
  948. * @param timeout the specified timeout, in milliseconds.
  949. * @exception SocketException if there is an error
  950. * in the underlying protocol, such as a TCP error.
  951. * @since JDK 1.1
  952. * @see #getSoTimeout()
  953. */
  954. public synchronized void setSoTimeout(int timeout) throws SocketException {
  955. if (isClosed())
  956. throw new SocketException("Socket is closed");
  957. if (timeout < 0)
  958. throw new IllegalArgumentException("timeout can't be negative");
  959. getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  960. }
  961. /**
  962. * Returns setting for SO_TIMEOUT. 0 returns implies that the
  963. * option is disabled (i.e., timeout of infinity).
  964. * @return the setting for SO_TIMEOUT
  965. * @exception SocketException if there is an error
  966. * in the underlying protocol, such as a TCP error.
  967. * @since JDK1.1
  968. * @see #setSoTimeout(int)
  969. */
  970. public synchronized int getSoTimeout() throws SocketException {
  971. if (isClosed())
  972. throw new SocketException("Socket is closed");
  973. Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
  974. /* extra type safety */
  975. if (o instanceof Integer) {
  976. return ((Integer) o).intValue();
  977. } else {
  978. return 0;
  979. }
  980. }
  981. /**
  982. * Sets the SO_SNDBUF option to the specified value for this
  983. * <tt>Socket</tt>. The SO_SNDBUF option is used by the platform's
  984. * networking code as a hint for the size to set
  985. * the underlying network I/O buffers.
  986. *
  987. * <p>Because SO_SNDBUF is a hint, applications that want to
  988. * verify what size the buffers were set to should call
  989. * {@link #getSendBufferSize()}.
  990. *
  991. * @exception SocketException if there is an error
  992. * in the underlying protocol, such as a TCP error.
  993. *
  994. * @param size the size to which to set the send buffer
  995. * size. This value must be greater than 0.
  996. *
  997. * @exception IllegalArgumentException if the
  998. * value is 0 or is negative.
  999. *
  1000. * @see #getSendBufferSize()
  1001. * @since 1.2
  1002. */
  1003. public synchronized void setSendBufferSize(int size)
  1004. throws SocketException{
  1005. if (!(size > 0)) {
  1006. throw new IllegalArgumentException("negative send size");
  1007. }
  1008. if (isClosed())
  1009. throw new SocketException("Socket is closed");
  1010. getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
  1011. }
  1012. /**
  1013. * Get value of the SO_SNDBUF option for this <tt>Socket</tt>,
  1014. * that is the buffer size used by the platform
  1015. * for output on this <tt>Socket</tt>.
  1016. * @return the value of the SO_SNDBUF option for this <tt>Socket</tt>.
  1017. *
  1018. * @exception SocketException if there is an error
  1019. * in the underlying protocol, such as a TCP error.
  1020. *
  1021. * @see #setSendBufferSize(int)
  1022. * @since 1.2
  1023. */
  1024. public synchronized int getSendBufferSize() throws SocketException {
  1025. if (isClosed())
  1026. throw new SocketException("Socket is closed");
  1027. int result = 0;
  1028. Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
  1029. if (o instanceof Integer) {
  1030. result = ((Integer)o).intValue();
  1031. }
  1032. return result;
  1033. }
  1034. /**
  1035. * Sets the SO_RCVBUF option to the specified value for this
  1036. * <tt>Socket</tt>. The SO_RCVBUF option is used by the platform's
  1037. * networking code as a hint for the size to set
  1038. * the underlying network I/O buffers.
  1039. *
  1040. * <p>Increasing the receive buffer size can increase the performance of
  1041. * network I/O for high-volume connection, while decreasing it can
  1042. * help reduce the backlog of incoming data.
  1043. *
  1044. * <p>Because SO_RCVBUF is a hint, applications that want to
  1045. * verify what size the buffers were set to should call
  1046. * {@link #getReceiveBufferSize()}.
  1047. *
  1048. * <p>The value of SO_RCVBUF is also used to set the TCP receive window
  1049. * that is advertized to the remote peer. Generally, the window size
  1050. * can be modified at any time when a socket is connected. However, if
  1051. * a receive window larger than 64K is required then this must be requested
  1052. * <B>before</B> the socket is connected to the remote peer. There are two
  1053. * cases to be aware of:<p>
  1054. * <ol>
  1055. * <li>For sockets accepted from a ServerSocket, this must be done by calling
  1056. * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
  1057. * is bound to a local address.<p></li>
  1058. * <li>For client sockets, setReceiveBufferSize() must be called before
  1059. * connecting the socket to its remote peer.<p></li></ol>
  1060. * @param size the size to which to set the receive buffer
  1061. * size. This value must be greater than 0.
  1062. *
  1063. * @exception IllegalArgumentException if the value is 0 or is
  1064. * negative.
  1065. *
  1066. * @exception SocketException if there is an error
  1067. * in the underlying protocol, such as a TCP error.
  1068. *
  1069. * @see #getReceiveBufferSize()
  1070. * @see ServerSocket#setReceiveBufferSize(int)
  1071. * @since 1.2
  1072. */
  1073. public synchronized void setReceiveBufferSize(int size)
  1074. throws SocketException{
  1075. if (size <= 0) {
  1076. throw new IllegalArgumentException("invalid receive size");
  1077. }
  1078. if (isClosed())
  1079. throw new SocketException("Socket is closed");
  1080. getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
  1081. }
  1082. /**
  1083. * Gets the value of the SO_RCVBUF option for this <tt>Socket</tt>,
  1084. * that is the buffer size used by the platform for
  1085. * input on this <tt>Socket</tt>.
  1086. *
  1087. * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.
  1088. * @exception SocketException if there is an error
  1089. * in the underlying protocol, such as a TCP error.
  1090. * @see #setReceiveBufferSize(int)
  1091. * @since 1.2
  1092. */
  1093. public synchronized int getReceiveBufferSize()
  1094. throws SocketException{
  1095. if (isClosed())
  1096. throw new SocketException("Socket is closed");
  1097. int result = 0;
  1098. Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
  1099. if (o instanceof Integer) {
  1100. result = ((Integer)o).intValue();
  1101. }
  1102. return result;
  1103. }
  1104. /**
  1105. * Enable/disable SO_KEEPALIVE.
  1106. *
  1107. * @param on whether or not to have socket keep alive turned on.
  1108. * @exception SocketException if there is an error
  1109. * in the underlying protocol, such as a TCP error.
  1110. * @since 1.3
  1111. * @see #getKeepAlive()
  1112. */
  1113. public void setKeepAlive(boolean on) throws SocketException {
  1114. if (isClosed())
  1115. throw new SocketException("Socket is closed");
  1116. getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));
  1117. }
  1118. /**
  1119. * Tests if SO_KEEPALIVE is enabled.
  1120. *
  1121. * @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is enabled.
  1122. * @exception SocketException if there is an error
  1123. * in the underlying protocol, such as a TCP error.
  1124. * @since 1.3
  1125. * @see #setKeepAlive(boolean)
  1126. */
  1127. public boolean getKeepAlive() throws SocketException {
  1128. if (isClosed())
  1129. throw new SocketException("Socket is closed");
  1130. return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue();
  1131. }
  1132. /**
  1133. * Sets traffic class or type-of-service octet in the IP
  1134. * header for packets sent from this Socket.
  1135. * As the underlying network implementation may ignore this
  1136. * value applications should consider it a hint.
  1137. *
  1138. * <P> The tc <B>must</B> be in the range <code> 0 <= tc <=
  1139. * 255</code> or an IllegalArgumentException will be thrown.
  1140. * <p>Notes:
  1141. * <p> For Internet Protocol v4 the value consists of an octet
  1142. * with precedence and TOS fields as detailed in RFC 1349. The
  1143. * TOS field is bitset created by bitwise-or'ing values such
  1144. * the following :-
  1145. * <p>
  1146. * <UL>
  1147. * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI>
  1148. * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI>
  1149. * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI>
  1150. * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI>
  1151. * </UL>
  1152. * The last low order bit is always ignored as this
  1153. * corresponds to the MBZ (must be zero) bit.
  1154. * <p>
  1155. * Setting bits in the precedence field may result in a
  1156. * SocketException indicating that the operation is not
  1157. * permitted.
  1158. * <p>
  1159. * As RFC 1122 section 4.2.4.2 indicates, a compliant TCP
  1160. * implementation should, but is not required to, let application
  1161. * change the TOS field during the lifetime of a connection.
  1162. * So whether the type-of-service field can be changed after the
  1163. * TCP connection has been established depends on the implementation
  1164. * in the underlying platform. Applications should not assume that
  1165. * they can change the TOS field after the connection.
  1166. * <p>
  1167. * For Internet Protocol v6 <code>tc</code> is the value that
  1168. * would be placed into the sin6_flowinfo field of the IP header.
  1169. *
  1170. * @param tc an <code>int</code> value for the bitset.
  1171. * @throws SocketException if there is an error setting the
  1172. * traffic class or type-of-service
  1173. * @since 1.4
  1174. * @see #getTrafficClass
  1175. */
  1176. public void setTrafficClass(int tc) throws SocketException {
  1177. if (tc < 0 || tc > 255)
  1178. throw new IllegalArgumentException("tc is not in range 0 -- 255");
  1179. if (isClosed())
  1180. throw new SocketException("Socket is closed");
  1181. getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
  1182. }
  1183. /**
  1184. * Gets traffic class or type-of-service in the IP header
  1185. * for packets sent from this Socket
  1186. * <p>
  1187. * As the underlying network implementation may ignore the
  1188. * traffic class or type-of-service set using {@link #setTrafficClass(int)}
  1189. * this method may return a different value than was previously
  1190. * set using the {@link #setTrafficClass(int)} method on this Socket.
  1191. *
  1192. * @return the traffic class or type-of-service already set
  1193. * @throws SocketException if there is an error obtaining the
  1194. * traffic class or type-of-service value.
  1195. * @since 1.4
  1196. * @see #setTrafficClass(int)
  1197. */
  1198. public int getTrafficClass() throws SocketException {
  1199. return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue();
  1200. }
  1201. /**
  1202. * Enable/disable the SO_REUSEADDR socket option.
  1203. * <p>
  1204. * When a TCP connection is closed the connection may remain
  1205. * in a timeout state for a period of time after the connection
  1206. * is closed (typically known as the <tt>TIME_WAIT</tt> state
  1207. * or <tt>2MSL</tt> wait state).
  1208. * For applications using a well known socket address or port
  1209. * it may not be possible to bind a socket to the required
  1210. * <tt>SocketAddress</tt> if there is a connection in the
  1211. * timeout state involving the socket address or port.
  1212. * <p>
  1213. * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket
  1214. * using {@link #bind(SocketAddress)} allows the socket to be
  1215. * bound even though a previous connection is in a timeout
  1216. * state.
  1217. * <p>
  1218. * When a <tt>Socket</tt> is created the initial setting
  1219. * of <tt>SO_REUSEADDR</tt> is disabled.
  1220. * <p>
  1221. * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
  1222. * disabled after a socket is bound (See {@link #isBound()})
  1223. * is not defined.
  1224. *
  1225. * @param on whether to enable or disable the socket option
  1226. * @exception SocketException if an error occurs enabling or
  1227. * disabling the <tt>SO_RESUEADDR</tt> socket option,
  1228. * or the socket is closed.
  1229. * @since 1.4
  1230. * @see #getReuseAddress()
  1231. * @see #bind(SocketAddress)
  1232. * @see #isClosed()
  1233. * @see #isBound()
  1234. */
  1235. public void setReuseAddress(boolean on) throws SocketException {
  1236. if (isClosed())
  1237. throw new SocketException("Socket is closed");
  1238. getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
  1239. }
  1240. /**
  1241. * Tests if SO_REUSEADDR is enabled.
  1242. *
  1243. * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
  1244. * @exception SocketException if there is an error
  1245. * in the underlying protocol, such as a TCP error.
  1246. * @since 1.4
  1247. * @see #setReuseAddress(boolean)
  1248. */
  1249. public boolean getReuseAddress() throws SocketException {
  1250. if (isClosed())
  1251. throw new SocketException("Socket is closed");
  1252. return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
  1253. }
  1254. /**
  1255. * Closes this socket.
  1256. * <p>
  1257. * Any thread currently blocked in an I/O operation upon this socket
  1258. * will throw a {@link SocketException}.
  1259. * <p>
  1260. * Once a socket has been closed, it is not available for further networking
  1261. * use (i.e. can't be reconnected or rebound). A new socket needs to be
  1262. * created.
  1263. *
  1264. * <p> Closing this socket will also close the socket's
  1265. * {@link java.io.InputStream InputStream} and
  1266. * {@link java.io.OutputStream OutputStream}.
  1267. *
  1268. * <p> If this socket has an associated channel then the channel is closed
  1269. * as well.
  1270. *
  1271. * @exception IOException if an I/O error occurs when closing this socket.
  1272. * @revised 1.4
  1273. * @spec JSR-51
  1274. * @see #isClosed
  1275. */
  1276. public synchronized void close() throws IOException {
  1277. synchronized(closeLock) {
  1278. if (isClosed())
  1279. return;
  1280. if (created)
  1281. impl.close();
  1282. closed = true;
  1283. }
  1284. }
  1285. /**
  1286. * Places the input stream for this socket at "end of stream".
  1287. * Any data sent to the input stream side of the socket is acknowledged
  1288. * and then silently discarded.
  1289. * <p>
  1290. * If you read from a socket input stream after invoking
  1291. * shutdownInput() on the socket, the stream will return EOF.
  1292. *
  1293. * @exception IOException if an I/O error occurs when shutting down this
  1294. * socket.
  1295. *
  1296. * @since 1.3
  1297. * @see java.net.Socket#shutdownOutput()
  1298. * @see java.net.Socket#close()
  1299. * @see java.net.Socket#setSoLinger(boolean, int)
  1300. * @see #isInputShutdown
  1301. */
  1302. public void shutdownInput() th