< prev index next >

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

Print this page




  89     /**
  90      * Creates an unbound server socket.
  91      *
  92      * @exception IOException IO error when opening the socket.
  93      * @revised 1.4
  94      */
  95     public ServerSocket() throws IOException {
  96         setImpl();
  97     }
  98 
  99     /**
 100      * Creates a server socket, bound to the specified port. A port number
 101      * of {@code 0} means that the port number is automatically
 102      * allocated, typically from an ephemeral port range. This port
 103      * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
 104      * <p>
 105      * The maximum queue length for incoming connection indications (a
 106      * request to connect) is set to {@code 50}. If a connection
 107      * indication arrives when the queue is full, the connection is refused.
 108      * <p>
 109      * If the application has specified a server socket implementation
 110      * factory, that factory's {@code createSocketImpl} method is called to
 111      * create the actual socket implementation. Otherwise a system-default
 112      * socket implementation is created.
 113      * <p>
 114      * If there is a security manager,
 115      * its {@code checkListen} method is called
 116      * with the {@code port} argument
 117      * as its argument to ensure the operation is allowed.
 118      * This could result in a SecurityException.
 119      *
 120      *
 121      * @param      port  the port number, or {@code 0} to use a port
 122      *                   number that is automatically allocated.
 123      *
 124      * @exception  IOException  if an I/O error occurs when opening the socket.
 125      * @exception  SecurityException
 126      * if a security manager exists and its {@code checkListen}
 127      * method doesn't allow the operation.
 128      * @exception  IllegalArgumentException if the port parameter is outside
 129      *             the specified range of valid port values, which is between
 130      *             0 and 65535, inclusive.
 131      *
 132      * @see        java.net.SocketImpl


 134      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
 135      * @see        SecurityManager#checkListen
 136      */
 137     public ServerSocket(int port) throws IOException {
 138         this(port, 50, null);
 139     }
 140 
 141     /**
 142      * Creates a server socket and binds it to the specified local port
 143      * number, with the specified backlog.
 144      * A port number of {@code 0} means that the port number is
 145      * automatically allocated, typically from an ephemeral port range.
 146      * This port number can then be retrieved by calling
 147      * {@link #getLocalPort getLocalPort}.
 148      * <p>
 149      * The maximum queue length for incoming connection indications (a
 150      * request to connect) is set to the {@code backlog} parameter. If
 151      * a connection indication arrives when the queue is full, the
 152      * connection is refused.
 153      * <p>
 154      * If the application has specified a server socket implementation
 155      * factory, that factory's {@code createSocketImpl} method is called to
 156      * create the actual socket implementation. Otherwise a system-default
 157      * socket implementation is created.
 158      * <p>
 159      * If there is a security manager,
 160      * its {@code checkListen} method is called
 161      * with the {@code port} argument
 162      * as its argument to ensure the operation is allowed.
 163      * This could result in a SecurityException.
 164      *
 165      * The {@code backlog} argument is the requested maximum number of
 166      * pending connections on the socket. Its exact semantics are implementation
 167      * specific. In particular, an implementation may impose a maximum length
 168      * or may choose to ignore the parameter altogether. The value provided
 169      * should be greater than {@code 0}. If it is less than or equal to
 170      * {@code 0}, then an implementation specific default will be used.
 171      *
 172      * @param      port     the port number, or {@code 0} to use a port
 173      *                      number that is automatically allocated.
 174      * @param      backlog  requested maximum length of the queue of incoming
 175      *                      connections.
 176      *
 177      * @exception  IOException  if an I/O error occurs when opening the socket.


 457      */
 458 
 459     public SocketAddress getLocalSocketAddress() {
 460         if (!isBound())
 461             return null;
 462         return new InetSocketAddress(getInetAddress(), getLocalPort());
 463     }
 464 
 465     /**
 466      * Listens for a connection to be made to this socket and accepts
 467      * it. The method blocks until a connection is made.
 468      *
 469      * <p>A new Socket {@code s} is created and, if there
 470      * is a security manager,
 471      * the security manager's {@code checkAccept} method is called
 472      * with {@code s.getInetAddress().getHostAddress()} and
 473      * {@code s.getPort()}
 474      * as its arguments to ensure the operation is allowed.
 475      * This could result in a SecurityException.
 476      *
 477      * @implNote
 478      * An instance of this class using a system-default {@code SocketImpl}
 479      * accepts sockets with a {@code SocketImpl} of the same type, regardless
 480      * of the {@linkplain Socket#setSocketImplFactory(SocketImplFactory)
 481      * client socket implementation factory}, if one has been set.
 482      *
 483      * @exception  IOException  if an I/O error occurs when waiting for a
 484      *               connection.
 485      * @exception  SecurityException  if a security manager exists and its
 486      *             {@code checkAccept} method doesn't allow the operation.
 487      * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
 488      *             the timeout has been reached.
 489      * @exception  java.nio.channels.IllegalBlockingModeException
 490      *             if this socket has an associated channel, the channel is in
 491      *             non-blocking mode, and there is no connection ready to be
 492      *             accepted
 493      *
 494      * @return the new Socket
 495      * @see SecurityManager#checkAccept
 496      * @revised 1.4
 497      * @spec JSR-51
 498      */
 499     public Socket accept() throws IOException {
 500         if (isClosed())
 501             throw new SocketException("Socket is closed");
 502         if (!isBound())
 503             throw new SocketException("Socket is not bound yet");
 504         Socket s = new Socket((SocketImpl) null);
 505         implAccept(s);
 506         return s;
 507     }
 508 
 509     /**
 510      * Subclasses of ServerSocket use this method to override accept()
 511      * to return their own subclass of socket.  So a FooServerSocket
 512      * will typically hand this method a newly created, unbound, FooSocket.
 513      * On return from implAccept the FooSocket will be connected to a client.
 514      *
 515      * <p> The behavior of this method is unspecified when invoked with a
 516      * socket that is not newly created and unbound. Any socket options set
 517      * on the given socket prior to invoking this method may or may not be
 518      * preserved when the connection is accepted. It may not be possible to
 519      * accept a connection when this socket has a {@code SocketImpl} of one
 520      * type and the given socket has a {@code SocketImpl} of a completely
 521      * different type.
 522      *
 523      * @implNote
 524      * An instance of this class using a system-default {@code SocketImpl}
 525      * can accept a connection with a Socket using a {@code SocketImpl} of
 526      * the same type: {@code IOException} is thrown if the Socket is using
 527      * a custom {@code SocketImpl}. An instance of this class using a
 528      * custom {@code SocketImpl} cannot accept a connection with a Socket
 529      * using a system-default {@code SocketImpl}.
 530      *
 531      * @param s the Socket
 532      * @throws java.nio.channels.IllegalBlockingModeException
 533      *         if this socket has an associated channel,
 534      *         and the channel is in non-blocking mode
 535      * @throws IOException if an I/O error occurs when waiting
 536      *         for a connection, or if it is not possible for this socket
 537      *         to accept a connection with the given socket
 538      *
 539      * @since   1.1
 540      * @revised 1.4
 541      * @spec JSR-51
 542      */
 543     protected final void implAccept(Socket s) throws IOException {
 544         SocketImpl si = s.impl;
 545 
 546         // Socket has no SocketImpl
 547         if (si == null) {
 548             si = implAccept();
 549             s.setImpl(si);
 550             s.postAccept();
 551             return;
 552         }
 553 
 554         // Socket has a SOCKS or HTTP SocketImpl, need delegate
 555         if (si instanceof DelegatingSocketImpl) {
 556             si = ((DelegatingSocketImpl) si).delegate();
 557             assert si instanceof PlatformSocketImpl;
 558         }




  89     /**
  90      * Creates an unbound server socket.
  91      *
  92      * @exception IOException IO error when opening the socket.
  93      * @revised 1.4
  94      */
  95     public ServerSocket() throws IOException {
  96         setImpl();
  97     }
  98 
  99     /**
 100      * Creates a server socket, bound to the specified port. A port number
 101      * of {@code 0} means that the port number is automatically
 102      * allocated, typically from an ephemeral port range. This port
 103      * number can then be retrieved by calling {@link #getLocalPort getLocalPort}.
 104      * <p>
 105      * The maximum queue length for incoming connection indications (a
 106      * request to connect) is set to {@code 50}. If a connection
 107      * indication arrives when the queue is full, the connection is refused.
 108      * <p>
 109      * If the application has specified a server socket factory, that
 110      * factory's {@code createSocketImpl} method is called to create
 111      * the actual socket implementation. Otherwise a "plain" socket is created.

 112      * <p>
 113      * If there is a security manager,
 114      * its {@code checkListen} method is called
 115      * with the {@code port} argument
 116      * as its argument to ensure the operation is allowed.
 117      * This could result in a SecurityException.
 118      *
 119      *
 120      * @param      port  the port number, or {@code 0} to use a port
 121      *                   number that is automatically allocated.
 122      *
 123      * @exception  IOException  if an I/O error occurs when opening the socket.
 124      * @exception  SecurityException
 125      * if a security manager exists and its {@code checkListen}
 126      * method doesn't allow the operation.
 127      * @exception  IllegalArgumentException if the port parameter is outside
 128      *             the specified range of valid port values, which is between
 129      *             0 and 65535, inclusive.
 130      *
 131      * @see        java.net.SocketImpl


 133      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
 134      * @see        SecurityManager#checkListen
 135      */
 136     public ServerSocket(int port) throws IOException {
 137         this(port, 50, null);
 138     }
 139 
 140     /**
 141      * Creates a server socket and binds it to the specified local port
 142      * number, with the specified backlog.
 143      * A port number of {@code 0} means that the port number is
 144      * automatically allocated, typically from an ephemeral port range.
 145      * This port number can then be retrieved by calling
 146      * {@link #getLocalPort getLocalPort}.
 147      * <p>
 148      * The maximum queue length for incoming connection indications (a
 149      * request to connect) is set to the {@code backlog} parameter. If
 150      * a connection indication arrives when the queue is full, the
 151      * connection is refused.
 152      * <p>
 153      * If the application has specified a server socket factory, that
 154      * factory's {@code createSocketImpl} 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,
 158      * its {@code checkListen} method is called
 159      * with the {@code port} argument
 160      * as its argument to ensure the operation is allowed.
 161      * This could result in a SecurityException.
 162      *
 163      * The {@code backlog} argument is the requested maximum number of
 164      * pending connections on the socket. Its exact semantics are implementation
 165      * specific. In particular, an implementation may impose a maximum length
 166      * or may choose to ignore the parameter altogether. The value provided
 167      * should be greater than {@code 0}. If it is less than or equal to
 168      * {@code 0}, then an implementation specific default will be used.
 169      *
 170      * @param      port     the port number, or {@code 0} to use a port
 171      *                      number that is automatically allocated.
 172      * @param      backlog  requested maximum length of the queue of incoming
 173      *                      connections.
 174      *
 175      * @exception  IOException  if an I/O error occurs when opening the socket.


 455      */
 456 
 457     public SocketAddress getLocalSocketAddress() {
 458         if (!isBound())
 459             return null;
 460         return new InetSocketAddress(getInetAddress(), getLocalPort());
 461     }
 462 
 463     /**
 464      * Listens for a connection to be made to this socket and accepts
 465      * it. The method blocks until a connection is made.
 466      *
 467      * <p>A new Socket {@code s} is created and, if there
 468      * is a security manager,
 469      * the security manager's {@code checkAccept} method is called
 470      * with {@code s.getInetAddress().getHostAddress()} and
 471      * {@code s.getPort()}
 472      * as its arguments to ensure the operation is allowed.
 473      * This could result in a SecurityException.
 474      *






 475      * @exception  IOException  if an I/O error occurs when waiting for a
 476      *               connection.
 477      * @exception  SecurityException  if a security manager exists and its
 478      *             {@code checkAccept} method doesn't allow the operation.
 479      * @exception  SocketTimeoutException if a timeout was previously set with setSoTimeout and
 480      *             the timeout has been reached.
 481      * @exception  java.nio.channels.IllegalBlockingModeException
 482      *             if this socket has an associated channel, the channel is in
 483      *             non-blocking mode, and there is no connection ready to be
 484      *             accepted
 485      *
 486      * @return the new Socket
 487      * @see SecurityManager#checkAccept
 488      * @revised 1.4
 489      * @spec JSR-51
 490      */
 491     public Socket accept() throws IOException {
 492         if (isClosed())
 493             throw new SocketException("Socket is closed");
 494         if (!isBound())
 495             throw new SocketException("Socket is not bound yet");
 496         Socket s = new Socket((SocketImpl) null);
 497         implAccept(s);
 498         return s;
 499     }
 500 
 501     /**
 502      * Subclasses of ServerSocket use this method to override accept()
 503      * to return their own subclass of socket.  So a FooServerSocket
 504      * will typically hand this method an <i>empty</i> FooSocket.  On
 505      * return from implAccept the FooSocket will be connected to a client.
















 506      *
 507      * @param s the Socket
 508      * @throws java.nio.channels.IllegalBlockingModeException
 509      *         if this socket has an associated channel,
 510      *         and the channel is in non-blocking mode
 511      * @throws IOException if an I/O error occurs when waiting
 512      * for a connection.


 513      * @since   1.1
 514      * @revised 1.4
 515      * @spec JSR-51
 516      */
 517     protected final void implAccept(Socket s) throws IOException {
 518         SocketImpl si = s.impl;
 519 
 520         // Socket has no SocketImpl
 521         if (si == null) {
 522             si = implAccept();
 523             s.setImpl(si);
 524             s.postAccept();
 525             return;
 526         }
 527 
 528         // Socket has a SOCKS or HTTP SocketImpl, need delegate
 529         if (si instanceof DelegatingSocketImpl) {
 530             si = ((DelegatingSocketImpl) si).delegate();
 531             assert si instanceof PlatformSocketImpl;
 532         }


< prev index next >