ServerSocket.java revision 508
2362N/A * Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * This class implements server sockets. A server socket waits for 0N/A * requests to come in over the network. It performs some operation 0N/A * based on that request, and then possibly returns a result to the requester. 0N/A * The actual work of the server socket is performed by an instance 0N/A * of the <code>SocketImpl</code> class. An application can 0N/A * change the socket factory that creates the socket 0N/A * implementation to configure itself to create sockets 0N/A * appropriate to the local firewall. 0N/A * @author unascribed 0N/A * @see java.net.SocketImpl 0N/A * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 0N/A * @see java.nio.channels.ServerSocketChannel 0N/A * Various states of this socket. 0N/A * The implementation of this Socket. 0N/A * Are we using an older SocketImpl? 0N/A * Creates an unbound server socket. 0N/A * @exception IOException IO error when opening the socket. 0N/A * Creates a server socket, bound to the specified port. A port number 0N/A * of <code>0</code> means that the port number is automatically 0N/A * allocated, typically from an ephemeral port range. This port 0N/A * number can then be retrieved by calling {@link #getLocalPort getLocalPort}. 0N/A * The maximum queue length for incoming connection indications (a 0N/A * request to connect) is set to <code>50</code>. If a connection 0N/A * indication arrives when the queue is full, the connection is refused. 0N/A * If the application has specified a server socket factory, that 0N/A * factory's <code>createSocketImpl</code> method is called to create 0N/A * the actual socket implementation. Otherwise a "plain" socket is created. 0N/A * If there is a security manager, 0N/A * its <code>checkListen</code> method is called 0N/A * with the <code>port</code> argument 0N/A * as its argument to ensure the operation is allowed. 0N/A * This could result in a SecurityException. 0N/A * @param port the port number, or <code>0</code> to use a port 0N/A * number that is automatically allocated. 0N/A * @exception IOException if an I/O error occurs when opening the socket. 0N/A * @exception SecurityException 0N/A * if a security manager exists and its <code>checkListen</code> 0N/A * method doesn't allow the operation. 0N/A * @exception IllegalArgumentException if the port parameter is outside 0N/A * the specified range of valid port values, which is between 0N/A * 0 and 65535, inclusive. 0N/A * @see java.net.SocketImpl 0N/A * @see java.net.SocketImplFactory#createSocketImpl() 0N/A * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 0N/A * @see SecurityManager#checkListen 0N/A * Creates a server socket and binds it to the specified local port 0N/A * number, with the specified backlog. 0N/A * A port number of <code>0</code> means that the port number is 0N/A * automatically allocated, typically from an ephemeral port range. 0N/A * This port number can then be retrieved by calling 0N/A * {@link #getLocalPort getLocalPort}. 0N/A * The maximum queue length for incoming connection indications (a 0N/A * request to connect) is set to the <code>backlog</code> parameter. If 0N/A * a connection indication arrives when the queue is full, the 0N/A * connection is refused. 0N/A * If the application has specified a server socket factory, that 0N/A * factory's <code>createSocketImpl</code> method is called to create 0N/A * the actual socket implementation. Otherwise a "plain" socket is created. 0N/A * If there is a security manager, 0N/A * its <code>checkListen</code> method is called 0N/A * with the <code>port</code> argument 0N/A * as its argument to ensure the operation is allowed. 0N/A * This could result in a SecurityException. 0N/A * The <code>backlog</code> argument is the requested maximum number of 0N/A * pending connections on the socket. Its exact semantics are implementation 0N/A * specific. In particular, an implementation may impose a maximum length 0N/A * or may choose to ignore the parameter altogther. The value provided 0N/A * should be greater than <code>0</code>. If it is less than or equal to 0N/A * <code>0</code>, then an implementation specific default will be used. 0N/A * @param port the port number, or <code>0</code> to use a port 0N/A * number that is automatically allocated. 0N/A * @param backlog requested maximum length of the queue of incoming 0N/A * @exception IOException if an I/O error occurs when opening the socket. 0N/A * @exception SecurityException 0N/A * if a security manager exists and its <code>checkListen</code> 0N/A * method doesn't allow the operation. 0N/A * @exception IllegalArgumentException if the port parameter is outside 0N/A * the specified range of valid port values, which is between 0N/A * 0 and 65535, inclusive. 0N/A * @see java.net.SocketImpl 0N/A * @see java.net.SocketImplFactory#createSocketImpl() 0N/A * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 0N/A * @see SecurityManager#checkListen 0N/A * Create a server with the specified port, listen backlog, and 0N/A * local IP address to bind to. The <i>bindAddr</i> argument 0N/A * can be used on a multi-homed host for a ServerSocket that 0N/A * will only accept connect requests to one of its addresses. 0N/A * If <i>bindAddr</i> is null, it will default accepting 0N/A * The port must be between 0 and 65535, inclusive. 0N/A * A port number of <code>0</code> means that the port number is 0N/A * automatically allocated, typically from an ephemeral port range. 0N/A * This port number can then be retrieved by calling 0N/A * {@link #getLocalPort getLocalPort}. 0N/A * <P>If there is a security manager, this method 0N/A * calls its <code>checkListen</code> method 0N/A * with the <code>port</code> argument 0N/A * as its argument to ensure the operation is allowed. 0N/A * This could result in a SecurityException. 0N/A * The <code>backlog</code> argument is the requested maximum number of 0N/A * pending connections on the socket. Its exact semantics are implementation 0N/A * specific. In particular, an implementation may impose a maximum length 0N/A * or may choose to ignore the parameter altogther. The value provided 0N/A * should be greater than <code>0</code>. If it is less than or equal to 0N/A * <code>0</code>, then an implementation specific default will be used. 0N/A * @param port the port number, or <code>0</code> to use a port 0N/A * number that is automatically allocated. 0N/A * @param backlog requested maximum length of the queue of incoming 0N/A * @param bindAddr the local InetAddress the server will bind to 0N/A * @throws SecurityException if a security manager exists and 0N/A * its <code>checkListen</code> method doesn't allow the operation. 0N/A * @throws IOException if an I/O error occurs when opening the socket. 0N/A * @exception IllegalArgumentException if the port parameter is outside 0N/A * the specified range of valid port values, which is between 0N/A * 0 and 65535, inclusive. 0N/A * @see SocketOptions 0N/A * @see SecurityManager#checkListen 0N/A "Port value out of range: " +
port);
0N/A * Get the <code>SocketImpl</code> attached to this socket, creating 0N/A * @return the <code>SocketImpl</code> attached to that ServerSocket. 0N/A * @throws SocketException if creation fails. 0N/A // SocketImpl.connect() is a protected method, therefore we need to use 0N/A // getDeclaredMethod, therefore we need permission to access the member 0N/A // No need to do a checkOldImpl() here, we know it's an up to date 0N/A * Creates the socket implementation. 0N/A * @throws IOException if creation fails 0N/A * Binds the <code>ServerSocket</code> to a specific address 0N/A * (IP address and port number). 0N/A * If the address is <code>null</code>, then the system will pick up 0N/A * an ephemeral port and a valid local address to bind the socket. 0N/A * @param endpoint The IP address & port number to bind to. 0N/A * @throws IOException if the bind operation fails, or if the socket 0N/A * @throws SecurityException if a <code>SecurityManager</code> is present and 0N/A * its <code>checkListen</code> method doesn't allow the operation. 0N/A * @throws IllegalArgumentException if endpoint is a 0N/A * SocketAddress subclass not supported by this socket 0N/A * Binds the <code>ServerSocket</code> to a specific address 0N/A * (IP address and port number). * If the address is <code>null</code>, then the system will pick up * an ephemeral port and a valid local address to bind the socket. * The <code>backlog</code> argument is the requested maximum number of * pending connections on the socket. Its exact semantics are implementation * specific. In particular, an implementation may impose a maximum length * or may choose to ignore the parameter altogther. The value provided * should be greater than <code>0</code>. If it is less than or equal to * <code>0</code>, then an implementation specific default will be used. * @param endpoint The IP address & port number to bind to. * @param backlog requested maximum length of the queue of * @throws IOException if the bind operation fails, or if the socket * @throws SecurityException if a <code>SecurityManager</code> is present and * its <code>checkListen</code> method doesn't allow the operation. * @throws IllegalArgumentException if endpoint is a * SocketAddress subclass not supported by this socket * Returns the local address of this server socket. * If the socket was bound prior to being {@link #close closed}, * then this method will continue to return the local address * after the socket is closed. * @return the address to which this socket is bound, * or <code>null</code> if the socket is unbound. // If we're bound, the impl has been created // so we shouldn't get here * Returns the port number on which this socket is listening. * If the socket was bound prior to being {@link #close closed}, * then this method will continue to return the port number * after the socket is closed. * @return the port number to which this socket is listening or * -1 if the socket is not bound yet. // If we're bound, the impl has been created // so we shouldn't get here * Returns the address of the endpoint this socket is bound to, or * <code>null</code> if it is not bound yet. * If the socket was bound prior to being {@link #close closed}, * then this method will continue to return the address of the endpoint * after the socket is closed. * @return a <code>SocketAddress</code> representing the local endpoint of this * socket, or <code>null</code> if it is not bound yet. * @see #bind(SocketAddress) * Listens for a connection to be made to this socket and accepts * it. The method blocks until a connection is made. * <p>A new Socket <code>s</code> is created and, if there * the security manager's <code>checkAccept</code> method is called * with <code>s.getInetAddress().getHostAddress()</code> and * <code>s.getPort()</code> * as its arguments to ensure the operation is allowed. * This could result in a SecurityException. * @exception IOException if an I/O error occurs when waiting for a * @exception SecurityException if a security manager exists and its * <code>checkAccept</code> method doesn't allow the operation. * @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and * the timeout has been reached. * @exception java.nio.channels.IllegalBlockingModeException * if this socket has an associated channel, the channel is in * non-blocking mode, and there is no connection ready to be * @see SecurityManager#checkAccept * Subclasses of ServerSocket use this method to override accept() * to return their own subclass of socket. So a FooServerSocket * will typically hand this method an <i>empty</i> FooSocket. On * return from implAccept the FooSocket will be connected to a client. * @throws java.nio.channels.IllegalBlockingModeException * if this socket has an associated channel, * and the channel is in non-blocking mode * @throws IOException if an I/O error occurs when waiting * Any thread currently blocked in {@link #accept()} will throw * a {@link SocketException}. * <p> If this socket has an associated channel then the channel is closed * @exception IOException if an I/O error occurs when closing the socket. * Returns the unique {@link java.nio.channels.ServerSocketChannel} object * associated with this socket, if any. * <p> A server socket will have a channel if, and only if, the channel * itself was created via the {@link * java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open} * @return the server-socket channel associated with this socket, * or <tt>null</tt> if this socket was not created * Returns the binding state of the ServerSocket. * @return true if the ServerSocket succesfuly bound to an address // Before 1.3 ServerSockets were always bound during creation * Returns the closed state of the ServerSocket. * @return true if the socket has been closed * milliseconds. With this option set to a non-zero timeout, * a call to accept() for this ServerSocket * will block for only this amount of time. If the timeout expires, * a <B>java.net.SocketTimeoutException</B> is raised, though the * ServerSocket is still valid. The option <B>must</B> be enabled * prior to entering the blocking operation to have effect. The * A timeout of zero is interpreted as an infinite timeout. * @param timeout the specified timeout, in milliseconds * @exception SocketException if there is an error in * the underlying protocol, such as a TCP error. * Retrieve setting for SO_TIMEOUT. 0 returns implies that the * option is disabled (i.e., timeout of infinity). * @return the SO_TIMEOUT value * @exception IOException if an I/O error occurs * @see #setSoTimeout(int) * When a TCP connection is closed the connection may remain * in a timeout state for a period of time after the connection * is closed (typically known as the <tt>TIME_WAIT</tt> state * or <tt>2MSL</tt> wait state). * For applications using a well known socket address or port * it may not be possible to bind a socket to the required * <tt>SocketAddress</tt> if there is a connection in the * timeout state involving the socket address or port. * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket * using {@link #bind(SocketAddress)} allows the socket to be * bound even though a previous connection is in a timeout * When a <tt>ServerSocket</tt> is created the initial setting * of <tt>SO_REUSEADDR</tt> is not defined. Applications can * use {@link #getReuseAddress()} to determine the initial * setting of <tt>SO_REUSEADDR</tt>. * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or * disabled after a socket is bound (See {@link #isBound()}) * @param on whether to enable or disable the socket option * @exception SocketException if an error occurs enabling or * disabling the <tt>SO_RESUEADDR</tt> socket option, * or the socket is closed. * @see #getReuseAddress() * @see #bind(SocketAddress) * Tests if SO_REUSEADDR is enabled. * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled. * @exception SocketException if there is an error * in the underlying protocol, such as a TCP error. * @see #setReuseAddress(boolean) * Returns the implementation address and implementation port of * this socket as a <code>String</code>. * @return a string representation of this socket. return "ServerSocket[unbound]";
* The factory for all server sockets. * Sets the server socket implementation factory for the * application. The factory can be specified only once. * When an application creates a new server socket, the socket * implementation factory's <code>createSocketImpl</code> method is * called to create the actual socket implementation. * Passing <code>null</code> to the method is a no-op unless the factory * If there is a security manager, this method first calls * the security manager's <code>checkSetFactory</code> method * to ensure the operation is allowed. * This could result in a SecurityException. * @param fac the desired factory. * @exception IOException if an I/O error occurs when setting the * @exception SocketException if the factory has already been defined. * @exception SecurityException if a security manager exists and its * <code>checkSetFactory</code> method doesn't allow the operation. * @see java.net.SocketImplFactory#createSocketImpl() * @see SecurityManager#checkSetFactory * Sets a default proposed value for the SO_RCVBUF option for sockets * accepted from this <tt>ServerSocket</tt>. The value actually set * in the accepted socket must be determined by calling * {@link Socket#getReceiveBufferSize()} after the socket * is returned by {@link #accept()}. * The value of SO_RCVBUF is used both to set the size of the internal * socket receive buffer, and to set the size of the TCP receive window * that is advertized to the remote peer. * It is possible to change the value subsequently, by calling * {@link Socket#setReceiveBufferSize(int)}. However, if the application * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 * then the proposed value must be set in the ServerSocket <B>before</B> * it is bound to a local address. This implies, that the ServerSocket must be * created with the no-argument constructor, then setReceiveBufferSize() must * be called and lastly the ServerSocket is bound to an address by calling bind(). * Failure to do this will not cause an error, and the buffer size may be set to the * requested value but the TCP receive window in sockets accepted from * this ServerSocket will be no larger than 64K bytes. * @exception SocketException if there is an error * in the underlying protocol, such as a TCP error. * @param size the size to which to set the receive buffer * size. This value must be greater than 0. * @exception IllegalArgumentException if the * value is 0 or is negative. * @see #getReceiveBufferSize * Gets the value of the SO_RCVBUF option for this <tt>ServerSocket</tt>, * that is the proposed buffer size that will be used for Sockets accepted * from this <tt>ServerSocket</tt>. * <p>Note, the value actually set in the accepted socket is determined by * calling {@link Socket#getReceiveBufferSize()}. * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>. * @exception SocketException if there is an error * in the underlying protocol, such as a TCP error. * @see #setReceiveBufferSize(int) * Sets performance preferences for this ServerSocket. * <p> Sockets use the TCP/IP protocol by default. Some implementations * may offer alternative protocols which have different performance * characteristics than TCP/IP. This method allows the application to * express its own preferences as to how these tradeoffs should be made * when the implementation chooses from the available protocols. * <p> Performance preferences are described by three integers * whose values indicate the relative importance of short connection time, * low latency, and high bandwidth. The absolute values of the integers * are irrelevant; in order to choose a protocol the values are simply * compared, with larger values indicating stronger preferences. If the * application prefers short connection time over both low latency and high * bandwidth, for example, then it could invoke this method with the values * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low * latency, and low latency above short connection time, then it could * invoke this method with the values <tt>(0, 1, 2)</tt>. * <p> Invoking this method after this socket has been bound * will have no effect. This implies that in order to use this capability * requires the socket to be created with the no-argument constructor. * An <tt>int</tt> expressing the relative importance of a short * An <tt>int</tt> expressing the relative importance of low * An <tt>int</tt> expressing the relative importance of high /* Not implemented yet */