< prev index next >

src/java.base/share/classes/java/net/ServerSocket.java

Print this page

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;

  30 import java.nio.channels.ServerSocketChannel;
  31 import java.util.Objects;
  32 import java.util.Set;
  33 import java.util.Collections;
  34 
  35 import sun.security.util.SecurityConstants;
  36 import sun.net.PlatformSocketImpl;
  37 
  38 /**
  39  * This class implements server sockets. A server socket waits for
  40  * requests to come in over the network. It performs some operation
  41  * based on that request, and then possibly returns a result to the requester.
  42  * <p>
  43  * The actual work of the server socket is performed by an instance
  44  * of the {@code SocketImpl} class.
  45  *
  46  * <p> The {@code ServerSocket} class defines convenience
  47  * methods to set and get several socket options. This class also
  48  * defines the {@link #setOption(SocketOption, Object) setOption}
  49  * and {@link #getOption(SocketOption) getOption} methods to set

 466      * after the socket is closed.
 467      * <p>
 468      * If there is a security manager set, its {@code checkConnect} method is
 469      * called with the local address and {@code -1} as its arguments to see
 470      * if the operation is allowed. If the operation is not allowed,
 471      * a {@code SocketAddress} representing the
 472      * {@link InetAddress#getLoopbackAddress loopback} address and the local
 473      * port to which the socket is bound is returned.
 474      *
 475      * @return a {@code SocketAddress} representing the local endpoint of
 476      *         this socket, or a {@code SocketAddress} representing the
 477      *         loopback address if denied by the security manager,
 478      *         or {@code null} if the socket is not bound yet.
 479      *
 480      * @see #getInetAddress()
 481      * @see #getLocalPort()
 482      * @see #bind(SocketAddress)
 483      * @see SecurityManager#checkConnect
 484      * @since 1.4
 485      */
 486 
 487     public SocketAddress getLocalSocketAddress() {
 488         if (!isBound())
 489             return null;
 490         return new InetSocketAddress(getInetAddress(), getLocalPort());
 491     }
 492 
 493     /**
 494      * Listens for a connection to be made to this socket and accepts
 495      * it. The method blocks until a connection is made.
 496      *
 497      * <p>A new Socket {@code s} is created and, if there





 498      * is a security manager,
 499      * the security manager's {@code checkAccept} method is called
 500      * with {@code s.getInetAddress().getHostAddress()} and
 501      * {@code s.getPort()}
 502      * as its arguments to ensure the operation is allowed.
 503      * This could result in a SecurityException.
 504      *
 505      * @implNote
 506      * An instance of this class using a system-default {@code SocketImpl}
 507      * accepts sockets with a {@code SocketImpl} of the same type, regardless
 508      * of the {@linkplain Socket#setSocketImplFactory(SocketImplFactory)
 509      * client socket implementation factory}, if one has been set.
 510      *
 511      * @throws     IOException  if an I/O error occurs when waiting for a
 512      *               connection.
 513      * @throws     SecurityException  if a security manager exists and its
 514      *             {@code checkAccept} method doesn't allow the operation.
 515      * @throws     SocketTimeoutException if a timeout was previously set with setSoTimeout and
 516      *             the timeout has been reached.
 517      * @throws     java.nio.channels.IllegalBlockingModeException

 655             si.fd = new FileDescriptor();
 656             si.address = new InetAddress();
 657             implAccept(si);
 658         } catch (Exception e) {
 659             si.reset();
 660             throw e;
 661         }
 662     }
 663 
 664     /**
 665      * Accepts a new connection so that the given SocketImpl is connected to
 666      * the peer. The SocketImpl and connection are closed if the connection is
 667      * denied by the security manager.
 668      * @throws IOException if an I/O error occurs
 669      * @throws SecurityException if the security manager's checkAccept method fails
 670      */
 671     private void implAccept(SocketImpl si) throws IOException {
 672         assert !(si instanceof DelegatingSocketImpl);
 673 
 674         // accept a connection
 675         impl.accept(si);











 676 
 677         // check permission, close SocketImpl/connection if denied
 678         @SuppressWarnings("removal")
 679         SecurityManager sm = System.getSecurityManager();
 680         if (sm != null) {
 681             try {
 682                 sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
 683             } catch (SecurityException se) {
 684                 si.close();
 685                 throw se;
 686             }
 687         }
 688     }
 689 
 690     /**
 691      * Throws IOException if the server SocketImpl and the given client
 692      * SocketImpl are not both platform or custom SocketImpls.
 693      */
 694     private void ensureCompatible(SocketImpl si) throws IOException {
 695         if ((impl instanceof PlatformSocketImpl) != (si instanceof PlatformSocketImpl)) {

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;
  30 import java.io.InterruptedIOException;
  31 import java.nio.channels.ServerSocketChannel;
  32 import java.util.Objects;
  33 import java.util.Set;
  34 import java.util.Collections;
  35 
  36 import sun.security.util.SecurityConstants;
  37 import sun.net.PlatformSocketImpl;
  38 
  39 /**
  40  * This class implements server sockets. A server socket waits for
  41  * requests to come in over the network. It performs some operation
  42  * based on that request, and then possibly returns a result to the requester.
  43  * <p>
  44  * The actual work of the server socket is performed by an instance
  45  * of the {@code SocketImpl} class.
  46  *
  47  * <p> The {@code ServerSocket} class defines convenience
  48  * methods to set and get several socket options. This class also
  49  * defines the {@link #setOption(SocketOption, Object) setOption}
  50  * and {@link #getOption(SocketOption) getOption} methods to set

 467      * after the socket is closed.
 468      * <p>
 469      * If there is a security manager set, its {@code checkConnect} method is
 470      * called with the local address and {@code -1} as its arguments to see
 471      * if the operation is allowed. If the operation is not allowed,
 472      * a {@code SocketAddress} representing the
 473      * {@link InetAddress#getLoopbackAddress loopback} address and the local
 474      * port to which the socket is bound is returned.
 475      *
 476      * @return a {@code SocketAddress} representing the local endpoint of
 477      *         this socket, or a {@code SocketAddress} representing the
 478      *         loopback address if denied by the security manager,
 479      *         or {@code null} if the socket is not bound yet.
 480      *
 481      * @see #getInetAddress()
 482      * @see #getLocalPort()
 483      * @see #bind(SocketAddress)
 484      * @see SecurityManager#checkConnect
 485      * @since 1.4
 486      */

 487     public SocketAddress getLocalSocketAddress() {
 488         if (!isBound())
 489             return null;
 490         return new InetSocketAddress(getInetAddress(), getLocalPort());
 491     }
 492 
 493     /**
 494      * Listens for a connection to be made to this socket and accepts
 495      * it. The method blocks until a connection is made.
 496      *
 497      * <p> For the system-default socket implementation at least, if a
 498      * {@linkplain Thread#isVirtual() virtual thread} blocked in {@code accept}
 499      * is {@linkplain Thread#interrupt() interrupted} then the socket is closed
 500      * and {@link SocketException} is thrown with the interrupt status set.
 501      *
 502      * <p> A new Socket {@code s} is created and, if there
 503      * is a security manager,
 504      * the security manager's {@code checkAccept} method is called
 505      * with {@code s.getInetAddress().getHostAddress()} and
 506      * {@code s.getPort()}
 507      * as its arguments to ensure the operation is allowed.
 508      * This could result in a SecurityException.
 509      *
 510      * @implNote
 511      * An instance of this class using a system-default {@code SocketImpl}
 512      * accepts sockets with a {@code SocketImpl} of the same type, regardless
 513      * of the {@linkplain Socket#setSocketImplFactory(SocketImplFactory)
 514      * client socket implementation factory}, if one has been set.
 515      *
 516      * @throws     IOException  if an I/O error occurs when waiting for a
 517      *               connection.
 518      * @throws     SecurityException  if a security manager exists and its
 519      *             {@code checkAccept} method doesn't allow the operation.
 520      * @throws     SocketTimeoutException if a timeout was previously set with setSoTimeout and
 521      *             the timeout has been reached.
 522      * @throws     java.nio.channels.IllegalBlockingModeException

 660             si.fd = new FileDescriptor();
 661             si.address = new InetAddress();
 662             implAccept(si);
 663         } catch (Exception e) {
 664             si.reset();
 665             throw e;
 666         }
 667     }
 668 
 669     /**
 670      * Accepts a new connection so that the given SocketImpl is connected to
 671      * the peer. The SocketImpl and connection are closed if the connection is
 672      * denied by the security manager.
 673      * @throws IOException if an I/O error occurs
 674      * @throws SecurityException if the security manager's checkAccept method fails
 675      */
 676     private void implAccept(SocketImpl si) throws IOException {
 677         assert !(si instanceof DelegatingSocketImpl);
 678 
 679         // accept a connection
 680         try {
 681             impl.accept(si);
 682         } catch (SocketTimeoutException e) {
 683             throw e;
 684         } catch (InterruptedIOException e) {
 685             Thread thread = Thread.currentThread();
 686             if (thread.isVirtual() && thread.isInterrupted()) {
 687                 close();
 688                 throw new SocketException("Closed by interrupt");
 689             }
 690             throw e;
 691         }
 692 
 693         // check permission, close SocketImpl/connection if denied
 694         @SuppressWarnings("removal")
 695         SecurityManager sm = System.getSecurityManager();
 696         if (sm != null) {
 697             try {
 698                 sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
 699             } catch (SecurityException se) {
 700                 si.close();
 701                 throw se;
 702             }
 703         }
 704     }
 705 
 706     /**
 707      * Throws IOException if the server SocketImpl and the given client
 708      * SocketImpl are not both platform or custom SocketImpls.
 709      */
 710     private void ensureCompatible(SocketImpl si) throws IOException {
 711         if ((impl instanceof PlatformSocketImpl) != (si instanceof PlatformSocketImpl)) {
< prev index next >