31 import java.security.AccessController;
32 import java.security.PrivilegedExceptionAction;
33 import java.util.Set;
34 import java.util.Collections;
35
36 /**
37 * This class implements server sockets. A server socket waits for
38 * requests to come in over the network. It performs some operation
39 * based on that request, and then possibly returns a result to the requester.
40 * <p>
41 * The actual work of the server socket is performed by an instance
42 * of the {@code SocketImpl} class. An application can
43 * change the socket factory that creates the socket
44 * implementation to configure itself to create sockets
45 * appropriate to the local firewall.
46 *
47 * @author unascribed
48 * @see java.net.SocketImpl
49 * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
50 * @see java.nio.channels.ServerSocketChannel
51 * @since JDK1.0
52 */
53 public
54 class ServerSocket implements java.io.Closeable {
55 /**
56 * Various states of this socket.
57 */
58 private boolean created = false;
59 private boolean bound = false;
60 private boolean closed = false;
61 private Object closeLock = new Object();
62
63 /**
64 * The implementation of this Socket.
65 */
66 private SocketImpl impl;
67
68 /**
69 * Are we using an older SocketImpl?
70 */
71 private boolean oldImpl = false;
208 * should be greater than {@code 0}. If it is less than or equal to
209 * {@code 0}, then an implementation specific default will be used.
210 *
211 * @param port the port number, or {@code 0} to use a port
212 * number that is automatically allocated.
213 * @param backlog requested maximum length of the queue of incoming
214 * connections.
215 * @param bindAddr the local InetAddress the server will bind to
216 *
217 * @throws SecurityException if a security manager exists and
218 * its {@code checkListen} method doesn't allow the operation.
219 *
220 * @throws IOException if an I/O error occurs when opening the socket.
221 * @exception IllegalArgumentException if the port parameter is outside
222 * the specified range of valid port values, which is between
223 * 0 and 65535, inclusive.
224 *
225 * @see SocketOptions
226 * @see SocketImpl
227 * @see SecurityManager#checkListen
228 * @since JDK1.1
229 */
230 public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
231 setImpl();
232 if (port < 0 || port > 0xFFFF)
233 throw new IllegalArgumentException(
234 "Port value out of range: " + port);
235 if (backlog < 1)
236 backlog = 50;
237 try {
238 bind(new InetSocketAddress(bindAddr, port), backlog);
239 } catch(SecurityException e) {
240 close();
241 throw e;
242 } catch(IOException e) {
243 close();
244 throw e;
245 }
246 }
247
248 /**
510 throw new SocketException("Socket is closed");
511 if (!isBound())
512 throw new SocketException("Socket is not bound yet");
513 Socket s = new Socket((SocketImpl) null);
514 implAccept(s);
515 return s;
516 }
517
518 /**
519 * Subclasses of ServerSocket use this method to override accept()
520 * to return their own subclass of socket. So a FooServerSocket
521 * will typically hand this method an <i>empty</i> FooSocket. On
522 * return from implAccept the FooSocket will be connected to a client.
523 *
524 * @param s the Socket
525 * @throws java.nio.channels.IllegalBlockingModeException
526 * if this socket has an associated channel,
527 * and the channel is in non-blocking mode
528 * @throws IOException if an I/O error occurs when waiting
529 * for a connection.
530 * @since JDK1.1
531 * @revised 1.4
532 * @spec JSR-51
533 */
534 protected final void implAccept(Socket s) throws IOException {
535 SocketImpl si = null;
536 try {
537 if (s.impl == null)
538 s.setImpl();
539 else {
540 s.impl.reset();
541 }
542 si = s.impl;
543 s.impl = null;
544 si.address = new InetAddress();
545 si.fd = new FileDescriptor();
546 getImpl().accept(si);
547
548 SecurityManager security = System.getSecurityManager();
549 if (security != null) {
550 security.checkAccept(si.getInetAddress().getHostAddress(),
627 */
628 public boolean isClosed() {
629 synchronized(closeLock) {
630 return closed;
631 }
632 }
633
634 /**
635 * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the
636 * specified timeout, in milliseconds. With this option set to a non-zero
637 * timeout, a call to accept() for this ServerSocket
638 * will block for only this amount of time. If the timeout expires,
639 * a <B>java.net.SocketTimeoutException</B> is raised, though the
640 * ServerSocket is still valid. The option <B>must</B> be enabled
641 * prior to entering the blocking operation to have effect. The
642 * timeout must be {@code > 0}.
643 * A timeout of zero is interpreted as an infinite timeout.
644 * @param timeout the specified timeout, in milliseconds
645 * @exception SocketException if there is an error in
646 * the underlying protocol, such as a TCP error.
647 * @since JDK1.1
648 * @see #getSoTimeout()
649 */
650 public synchronized void setSoTimeout(int timeout) throws SocketException {
651 if (isClosed())
652 throw new SocketException("Socket is closed");
653 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
654 }
655
656 /**
657 * Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
658 * 0 returns implies that the option is disabled (i.e., timeout of infinity).
659 * @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value
660 * @exception IOException if an I/O error occurs
661 * @since JDK1.1
662 * @see #setSoTimeout(int)
663 */
664 public synchronized int getSoTimeout() throws IOException {
665 if (isClosed())
666 throw new SocketException("Socket is closed");
667 Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
668 /* extra type safety */
669 if (o instanceof Integer) {
670 return ((Integer) o).intValue();
671 } else {
672 return 0;
673 }
674 }
675
676 /**
677 * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
678 * socket option.
679 * <p>
680 * When a TCP connection is closed the connection may remain
681 * in a timeout state for a period of time after the connection
|
31 import java.security.AccessController;
32 import java.security.PrivilegedExceptionAction;
33 import java.util.Set;
34 import java.util.Collections;
35
36 /**
37 * This class implements server sockets. A server socket waits for
38 * requests to come in over the network. It performs some operation
39 * based on that request, and then possibly returns a result to the requester.
40 * <p>
41 * The actual work of the server socket is performed by an instance
42 * of the {@code SocketImpl} class. An application can
43 * change the socket factory that creates the socket
44 * implementation to configure itself to create sockets
45 * appropriate to the local firewall.
46 *
47 * @author unascribed
48 * @see java.net.SocketImpl
49 * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
50 * @see java.nio.channels.ServerSocketChannel
51 * @since 1.0
52 */
53 public
54 class ServerSocket implements java.io.Closeable {
55 /**
56 * Various states of this socket.
57 */
58 private boolean created = false;
59 private boolean bound = false;
60 private boolean closed = false;
61 private Object closeLock = new Object();
62
63 /**
64 * The implementation of this Socket.
65 */
66 private SocketImpl impl;
67
68 /**
69 * Are we using an older SocketImpl?
70 */
71 private boolean oldImpl = false;
208 * should be greater than {@code 0}. If it is less than or equal to
209 * {@code 0}, then an implementation specific default will be used.
210 *
211 * @param port the port number, or {@code 0} to use a port
212 * number that is automatically allocated.
213 * @param backlog requested maximum length of the queue of incoming
214 * connections.
215 * @param bindAddr the local InetAddress the server will bind to
216 *
217 * @throws SecurityException if a security manager exists and
218 * its {@code checkListen} method doesn't allow the operation.
219 *
220 * @throws IOException if an I/O error occurs when opening the socket.
221 * @exception IllegalArgumentException if the port parameter is outside
222 * the specified range of valid port values, which is between
223 * 0 and 65535, inclusive.
224 *
225 * @see SocketOptions
226 * @see SocketImpl
227 * @see SecurityManager#checkListen
228 * @since 1.1
229 */
230 public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
231 setImpl();
232 if (port < 0 || port > 0xFFFF)
233 throw new IllegalArgumentException(
234 "Port value out of range: " + port);
235 if (backlog < 1)
236 backlog = 50;
237 try {
238 bind(new InetSocketAddress(bindAddr, port), backlog);
239 } catch(SecurityException e) {
240 close();
241 throw e;
242 } catch(IOException e) {
243 close();
244 throw e;
245 }
246 }
247
248 /**
510 throw new SocketException("Socket is closed");
511 if (!isBound())
512 throw new SocketException("Socket is not bound yet");
513 Socket s = new Socket((SocketImpl) null);
514 implAccept(s);
515 return s;
516 }
517
518 /**
519 * Subclasses of ServerSocket use this method to override accept()
520 * to return their own subclass of socket. So a FooServerSocket
521 * will typically hand this method an <i>empty</i> FooSocket. On
522 * return from implAccept the FooSocket will be connected to a client.
523 *
524 * @param s the Socket
525 * @throws java.nio.channels.IllegalBlockingModeException
526 * if this socket has an associated channel,
527 * and the channel is in non-blocking mode
528 * @throws IOException if an I/O error occurs when waiting
529 * for a connection.
530 * @since 1.1
531 * @revised 1.4
532 * @spec JSR-51
533 */
534 protected final void implAccept(Socket s) throws IOException {
535 SocketImpl si = null;
536 try {
537 if (s.impl == null)
538 s.setImpl();
539 else {
540 s.impl.reset();
541 }
542 si = s.impl;
543 s.impl = null;
544 si.address = new InetAddress();
545 si.fd = new FileDescriptor();
546 getImpl().accept(si);
547
548 SecurityManager security = System.getSecurityManager();
549 if (security != null) {
550 security.checkAccept(si.getInetAddress().getHostAddress(),
627 */
628 public boolean isClosed() {
629 synchronized(closeLock) {
630 return closed;
631 }
632 }
633
634 /**
635 * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} with the
636 * specified timeout, in milliseconds. With this option set to a non-zero
637 * timeout, a call to accept() for this ServerSocket
638 * will block for only this amount of time. If the timeout expires,
639 * a <B>java.net.SocketTimeoutException</B> is raised, though the
640 * ServerSocket is still valid. The option <B>must</B> be enabled
641 * prior to entering the blocking operation to have effect. The
642 * timeout must be {@code > 0}.
643 * A timeout of zero is interpreted as an infinite timeout.
644 * @param timeout the specified timeout, in milliseconds
645 * @exception SocketException if there is an error in
646 * the underlying protocol, such as a TCP error.
647 * @since 1.1
648 * @see #getSoTimeout()
649 */
650 public synchronized void setSoTimeout(int timeout) throws SocketException {
651 if (isClosed())
652 throw new SocketException("Socket is closed");
653 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
654 }
655
656 /**
657 * Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
658 * 0 returns implies that the option is disabled (i.e., timeout of infinity).
659 * @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value
660 * @exception IOException if an I/O error occurs
661 * @since 1.1
662 * @see #setSoTimeout(int)
663 */
664 public synchronized int getSoTimeout() throws IOException {
665 if (isClosed())
666 throw new SocketException("Socket is closed");
667 Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
668 /* extra type safety */
669 if (o instanceof Integer) {
670 return ((Integer) o).intValue();
671 } else {
672 return 0;
673 }
674 }
675
676 /**
677 * Enable/disable the {@link SocketOptions#SO_REUSEADDR SO_REUSEADDR}
678 * socket option.
679 * <p>
680 * When a TCP connection is closed the connection may remain
681 * in a timeout state for a period of time after the connection
|