1. /*
  2. * @(#)ServerSocket.java 1.42 00/08/16
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.net;
  11. import java.io.IOException;
  12. import java.io.FileDescriptor;
  13. /**
  14. * This class implements server sockets. A server socket waits for
  15. * requests to come in over the network. It performs some operation
  16. * based on that request, and then possibly returns a result to the requester.
  17. * <p>
  18. * The actual work of the server socket is performed by an instance
  19. * of the <code>SocketImpl</code> class. An application can
  20. * change the socket factory that creates the socket
  21. * implementation to configure itself to create sockets
  22. * appropriate to the local firewall.
  23. *
  24. * @author unascribed
  25. * @version 1.42, 08/16/00
  26. * @see java.net.SocketImpl
  27. * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  28. * @since JDK1.0
  29. */
  30. public
  31. class ServerSocket {
  32. /**
  33. * The implementation of this Socket.
  34. */
  35. private SocketImpl impl;
  36. /**
  37. * Creates an unconnected server socket. Note: this method
  38. * should not be public.
  39. * @exception IOException IO error when opening the socket.
  40. */
  41. private ServerSocket() throws IOException {
  42. impl = (factory != null) ? factory.createSocketImpl() :
  43. new PlainSocketImpl();
  44. }
  45. /**
  46. * Creates a server socket on a specified port. A port of
  47. * <code>0</code> creates a socket on any free port.
  48. * <p>
  49. * The maximum queue length for incoming connection indications (a
  50. * request to connect) is set to <code>50</code>. If a connection
  51. * indication arrives when the queue is full, the connection is refused.
  52. * <p>
  53. * If the application has specified a server socket factory, that
  54. * factory's <code>createSocketImpl</code> method is called to create
  55. * the actual socket implementation. Otherwise a "plain" socket is created.
  56. * <p>
  57. * If there is a security manager,
  58. * its <code>checkListen</code> method is called
  59. * with the <code>port</code> argument
  60. * as its argument to ensure the operation is allowed.
  61. * This could result in a SecurityException.
  62. *
  63. * @param port the port number, or <code>0</code> to use any
  64. * free port.
  65. *
  66. * @exception IOException if an I/O error occurs when opening the socket.
  67. * @exception SecurityException
  68. * if a security manager exists and its <code>checkListen</code>
  69. * method doesn't allow the operation.
  70. *
  71. * @see java.net.SocketImpl
  72. * @see java.net.SocketImplFactory#createSocketImpl()
  73. * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  74. * @see SecurityManager#checkListen
  75. */
  76. public ServerSocket(int port) throws IOException {
  77. this(port, 50, null);
  78. }
  79. /**
  80. * Creates a server socket and binds it to the specified local port
  81. * number, with the specified backlog.
  82. * A port number of <code>0</code> creates a socket on any
  83. * free port.
  84. * <p>
  85. * The maximum queue length for incoming connection indications (a
  86. * request to connect) is set to the <code>backlog</code> parameter. If
  87. * a connection indication arrives when the queue is full, the
  88. * connection is refused.
  89. * <p>
  90. * If the application has specified a server socket factory, that
  91. * factory's <code>createSocketImpl</code> method is called to create
  92. * the actual socket implementation. Otherwise a "plain" socket is created.
  93. * <p>
  94. * If there is a security manager,
  95. * its <code>checkListen</code> method is called
  96. * with the <code>port</code> argument
  97. * as its argument to ensure the operation is allowed.
  98. * This could result in a SecurityException.
  99. *
  100. * @param port the specified port, or <code>0</code> to use
  101. * any free port.
  102. * @param backlog the maximum length of the queue.
  103. *
  104. * @exception IOException if an I/O error occurs when opening the socket.
  105. * @exception SecurityException
  106. * if a security manager exists and its <code>checkListen</code>
  107. * method doesn't allow the operation.
  108. *
  109. * @see java.net.SocketImpl
  110. * @see java.net.SocketImplFactory#createSocketImpl()
  111. * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  112. * @see SecurityManager#checkListen
  113. */
  114. public ServerSocket(int port, int backlog) throws IOException {
  115. this(port, backlog, null);
  116. }
  117. /**
  118. * Create a server with the specified port, listen backlog, and
  119. * local IP address to bind to. The <i>bindAddr</i> argument
  120. * can be used on a multi-homed host for a ServerSocket that
  121. * will only accept connect requests to one of its addresses.
  122. * If <i>bindAddr</i> is null, it will default accepting
  123. * connections on any/all local addresses.
  124. * The port must be between 0 and 65535, inclusive.
  125. *
  126. * <P>If there is a security manager, this method
  127. * calls its <code>checkListen</code> method
  128. * with the <code>port</code> argument
  129. * as its argument to ensure the operation is allowed.
  130. * This could result in a SecurityException.
  131. *
  132. * <P>
  133. * @param port the local TCP port
  134. * @param backlog the listen backlog
  135. * @param bindAddr the local InetAddress the server will bind to
  136. *
  137. * @throws SecurityException if a security manager exists and
  138. * its <code>checkListen</code> method doesn't allow the operation.
  139. *
  140. * @throws IOException if an I/O error occurs when opening the socket.
  141. *
  142. * @see SocketOptions
  143. * @see SocketImpl
  144. * @see SecurityManager#checkListen
  145. * @since JDK1.1
  146. */
  147. public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
  148. this();
  149. if (port < 0 || port > 0xFFFF)
  150. throw new IllegalArgumentException(
  151. "Port value out of range: " + port);
  152. try {
  153. SecurityManager security = System.getSecurityManager();
  154. if (security != null) {
  155. security.checkListen(port);
  156. }
  157. impl.create(true); // a stream socket
  158. if (bindAddr == null)
  159. bindAddr = InetAddress.anyLocalAddress;
  160. impl.bind(bindAddr, port);
  161. impl.listen(backlog);
  162. } catch(SecurityException e) {
  163. impl.close();
  164. throw e;
  165. } catch(IOException e) {
  166. impl.close();
  167. throw e;
  168. }
  169. }
  170. /**
  171. * Returns the local address of this server socket.
  172. *
  173. * @return the address to which this socket is connected,
  174. * or <code>null</code> if the socket is not yet connected.
  175. */
  176. public InetAddress getInetAddress() {
  177. return impl.getInetAddress();
  178. }
  179. /**
  180. * Returns the port on which this socket is listening.
  181. *
  182. * @return the port number to which this socket is listening.
  183. */
  184. public int getLocalPort() {
  185. return impl.getLocalPort();
  186. }
  187. /**
  188. * Listens for a connection to be made to this socket and accepts
  189. * it. The method blocks until a connection is made.
  190. *
  191. * <p>A new Socket <code>s</code> is created and, if there
  192. * is a security manager,
  193. * the security manager's <code>checkAccept</code> method is called
  194. * with <code>s.getInetAddress().getHostAddress()</code> and
  195. * <code>s.getPort()</code>
  196. * as its arguments to ensure the operation is allowed.
  197. * This could result in a SecurityException.
  198. *
  199. * @exception IOException if an I/O error occurs when waiting for a
  200. * connection.
  201. * @exception SecurityException if a security manager exists and its
  202. * <code>checkListen</code> method doesn't allow the operation.
  203. * @return the new Socket
  204. * @see SecurityManager#checkAccept
  205. */
  206. public Socket accept() throws IOException {
  207. Socket s = new Socket();
  208. implAccept(s);
  209. return s;
  210. }
  211. /**
  212. * Subclasses of ServerSocket use this method to override accept()
  213. * to return their own subclass of socket. So a FooServerSocket
  214. * will typically hand this method an <i>empty</i> FooSocket. On
  215. * return from implAccept the FooSocket will be connected to a client.
  216. *
  217. * @param s the Socket
  218. * @throws IOException if an I/O error occurs when waiting
  219. * for a connection.
  220. * @since JDK1.1
  221. */
  222. protected final void implAccept(Socket s) throws IOException {
  223. SocketImpl si = s.impl;
  224. try {
  225. s.impl = null;
  226. si.address = new InetAddress();
  227. si.fd = new FileDescriptor();
  228. impl.accept(si);
  229. SecurityManager security = System.getSecurityManager();
  230. if (security != null) {
  231. security.checkAccept(si.getInetAddress().getHostAddress(),
  232. si.getPort());
  233. }
  234. } catch (IOException e) {
  235. si.reset();
  236. s.impl = si;
  237. throw e;
  238. } catch (SecurityException e) {
  239. si.reset();
  240. s.impl = si;
  241. throw e;
  242. }
  243. s.impl = si;
  244. }
  245. /**
  246. * Closes this socket.
  247. *
  248. * @exception IOException if an I/O error occurs when closing the socket.
  249. */
  250. public void close() throws IOException {
  251. impl.close();
  252. }
  253. /**
  254. * Enable/disable SO_TIMEOUT with the specified timeout, in
  255. * milliseconds. With this option set to a non-zero timeout,
  256. * a call to accept() for this ServerSocket
  257. * will block for only this amount of time. If the timeout expires,
  258. * a <B>java.io.InterruptedIOException</B> is raised, though the
  259. * ServerSocket is still valid. The option <B>must</B> be enabled
  260. * prior to entering the blocking operation to have effect. The
  261. * timeout must be > 0.
  262. * A timeout of zero is interpreted as an infinite timeout.
  263. * @param timeout the specified timeout, in milliseconds
  264. * @exception SocketException if there is an error in
  265. * the underlying protocol, such as a TCP error.
  266. * @since JDK1.1
  267. * @see #getSoTimeout()
  268. */
  269. public synchronized void setSoTimeout(int timeout) throws SocketException {
  270. impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  271. }
  272. /**
  273. * Retrive setting for SO_TIMEOUT. 0 returns implies that the
  274. * option is disabled (i.e., timeout of infinity).
  275. * @return the SO_TIMEOUT value
  276. * @exception IOException if an I/O error occurs
  277. * @since JDK1.1
  278. * @see #setSoTimeout(int)
  279. */
  280. public synchronized int getSoTimeout() throws IOException {
  281. Object o = impl.getOption(SocketOptions.SO_TIMEOUT);
  282. /* extra type safety */
  283. if (o instanceof Integer) {
  284. return ((Integer) o).intValue();
  285. } else {
  286. return 0;
  287. }
  288. }
  289. /**
  290. * Returns the implementation address and implementation port of
  291. * this socket as a <code>String</code>.
  292. *
  293. * @return a string representation of this socket.
  294. */
  295. public String toString() {
  296. return "ServerSocket[addr=" + impl.getInetAddress() +
  297. ",port=" + impl.getPort() +
  298. ",localport=" + impl.getLocalPort() + "]";
  299. }
  300. /**
  301. * The factory for all server sockets.
  302. */
  303. private static SocketImplFactory factory;
  304. /**
  305. * Sets the server socket implementation factory for the
  306. * application. The factory can be specified only once.
  307. * <p>
  308. * When an application creates a new server socket, the socket
  309. * implementation factory's <code>createSocketImpl</code> method is
  310. * called to create the actual socket implementation.
  311. * <p>
  312. * If there is a security manager, this method first calls
  313. * the security manager's <code>checkSetFactory</code> method
  314. * to ensure the operation is allowed.
  315. * This could result in a SecurityException.
  316. *
  317. * @param fac the desired factory.
  318. * @exception IOException if an I/O error occurs when setting the
  319. * socket factory.
  320. * @exception SocketException if the factory has already been defined.
  321. * @exception SecurityException if a security manager exists and its
  322. * <code>checkSetFactory</code> method doesn't allow the operation.
  323. * @see java.net.SocketImplFactory#createSocketImpl()
  324. * @see SecurityManager#checkSetFactory
  325. */
  326. public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  327. if (factory != null) {
  328. throw new SocketException("factory already defined");
  329. }
  330. SecurityManager security = System.getSecurityManager();
  331. if (security != null) {
  332. security.checkSetFactory();
  333. }
  334. factory = fac;
  335. }
  336. }