1. /*
  2. * @(#)ServerSocketChannel.java 1.24 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels;
  8. import java.io.IOException;
  9. import java.net.ServerSocket;
  10. import java.net.SocketAddress;
  11. import java.nio.channels.spi.*;
  12. /**
  13. * A selectable channel for stream-oriented listening sockets.
  14. *
  15. * <p> Server-socket channels are not a complete abstraction of listening
  16. * network sockets. Binding and the manipulation of socket options must be
  17. * done through an associated {@link java.net.ServerSocket} object obtained by
  18. * invoking the {@link #socket() socket} method. It is not possible to create
  19. * a channel for an arbitrary, pre-existing server socket, nor is it possible
  20. * to specify the {@link java.net.SocketImpl} object to be used by a server
  21. * socket associated with a server-socket channel.
  22. *
  23. * <p> A server-socket channel is created by invoking the {@link #open() open}
  24. * method of this class. A newly-created server-socket channel is open but not
  25. * yet bound. An attempt to invoke the {@link #accept() accept} method of an
  26. * unbound server-socket channel will cause a {@link NotYetBoundException} to
  27. * be thrown. A server-socket channel can be bound by invoking one of the
  28. * {@link java.net.ServerSocket#bind(java.net.SocketAddress,int) bind} methods
  29. * of an associated server socket.
  30. *
  31. * <p> Server-socket channels are safe for use by multiple concurrent threads.
  32. * </p>
  33. *
  34. *
  35. * @author Mark Reinhold
  36. * @author JSR-51 Expert Group
  37. * @version 1.24, 03/12/19
  38. * @since 1.4
  39. */
  40. public abstract class ServerSocketChannel
  41. extends AbstractSelectableChannel
  42. {
  43. /**
  44. * Initializes a new instance of this class.
  45. */
  46. protected ServerSocketChannel(SelectorProvider provider) {
  47. super(provider);
  48. }
  49. /**
  50. * Opens a server-socket channel.
  51. *
  52. * <p> The new channel is created by invoking the {@link
  53. * java.nio.channels.spi.SelectorProvider#openServerSocketChannel
  54. * openServerSocketChannel} method of the system-wide default {@link
  55. * java.nio.channels.spi.SelectorProvider} object.
  56. *
  57. * <p> The new channel's socket is initially unbound; it must be bound to a
  58. * specific address via one of its socket's {@link
  59. * java.net.ServerSocket#bind(SocketAddress) bind} methods before
  60. * connections can be accepted. </p>
  61. *
  62. * @return A new socket channel
  63. *
  64. * @throws IOException
  65. * If an I/O error occurs
  66. */
  67. public static ServerSocketChannel open() throws IOException {
  68. return SelectorProvider.provider().openServerSocketChannel();
  69. }
  70. /**
  71. * Returns an operation set identifying this channel's supported
  72. * operations.
  73. *
  74. * <p> Server-socket channels only support the accepting of new
  75. * connections, so this method returns {@link SelectionKey#OP_ACCEPT}.
  76. * </p>
  77. *
  78. * @return The valid-operation set
  79. */
  80. public final int validOps() {
  81. return SelectionKey.OP_ACCEPT;
  82. }
  83. // -- ServerSocket-specific operations --
  84. /**
  85. * Retrieves a server socket associated with this channel.
  86. *
  87. * <p> The returned object will not declare any public methods that are not
  88. * declared in the {@link java.net.ServerSocket} class. </p>
  89. *
  90. * @return A server socket associated with this channel
  91. */
  92. public abstract ServerSocket socket();
  93. /**
  94. * Accepts a connection made to this channel's socket.
  95. *
  96. * <p> If this channel is in non-blocking mode then this method will
  97. * immediately return <tt>null</tt> if there are no pending connections.
  98. * Otherwise it will block indefinitely until a new connection is available
  99. * or an I/O error occurs.
  100. *
  101. * <p> The socket channel returned by this method, if any, will be in
  102. * blocking mode regardless of the blocking mode of this channel.
  103. *
  104. * <p> This method performs exactly the same security checks as the {@link
  105. * java.net.ServerSocket#accept accept} method of the {@link
  106. * java.net.ServerSocket} class. That is, if a security manager has been
  107. * installed then for each new connection this method verifies that the
  108. * address and port number of the connection's remote endpoint are
  109. * permitted by the security manager's {@link
  110. * java.lang.SecurityManager#checkAccept checkAccept} method. </p>
  111. *
  112. * @return The socket channel for the new connection,
  113. * or <tt>null</tt> if this channel is in non-blocking mode
  114. * and no connection is available to be accepted
  115. *
  116. * @throws ClosedChannelException
  117. * If this channel is closed
  118. *
  119. * @throws AsynchronousCloseException
  120. * If another thread closes this channel
  121. * while the accept operation is in progress
  122. *
  123. * @throws ClosedByInterruptException
  124. * If another thread interrupts the current thread
  125. * while the accept operation is in progress, thereby
  126. * closing the channel and setting the current thread's
  127. * interrupt status
  128. *
  129. * @throws NotYetBoundException
  130. * If this channel's socket has not yet been bound
  131. *
  132. * @throws SecurityException
  133. * If a security manager has been installed
  134. * and it does not permit access to the remote endpoint
  135. * of the new connection
  136. *
  137. * @throws IOException
  138. * If some other I/O error occurs
  139. */
  140. public abstract SocketChannel accept() throws IOException;
  141. }