1 /* ServerSocket.java -- Class for implementing server side sockets
2 Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
29 import java.io.IOException;
31 /* Written using on-line Java Platform 1.2 API Specification.
32 * Status: I believe all methods are implemented.
36 * This class models server side sockets. The basic model is that the
37 * server socket is created and bound to some well known port. It then
38 * listens for and accepts connections. At that point the client and
39 * server sockets are ready to communicate with one another utilizing
40 * whatever application layer protocol they desire.
42 * As with the <code>Socket</code> class, most instance methods of this class
43 * simply redirect their calls to an implementation class.
45 * @author Aaron M. Renn (arenn@urbanophile.com)
46 * @author Per Bothner (bothner@cygnus.com)
48 public class ServerSocket
54 * This is the user defined SocketImplFactory, if one is supplied
56 private static SocketImplFactory factory;
61 * This is the SocketImp object to which most instance methods in this
62 * class are redirected
64 private SocketImpl impl;
67 * Private constructor that simply sets the implementation.
69 private ServerSocket()
72 impl = factory.createSocketImpl();
74 impl = new PlainSocketImpl();
78 * Creates a server socket and binds it to the specified port. If the
79 * port number is 0, a random free port will be chosen. The pending
80 * connection queue on this socket will be set to 50.
82 * @param port The port number to bind to
84 * @exception IOException If an error occurs
86 public ServerSocket (int port)
87 throws java.io.IOException
93 * Creates a server socket and binds it to the specified port. If the
94 * port number is 0, a random free port will be chosen. The pending
95 * connection queue on this socket will be set to the value passed as
98 * @param port The port number to bind to
99 * @param backlog The length of the pending connection queue
101 * @exception IOException If an error occurs
103 public ServerSocket (int port, int backlog)
104 throws java.io.IOException
106 this(port, backlog, null);
110 * Creates a server socket and binds it to the specified port. If the
111 * port number is 0, a random free port will be chosen. The pending
112 * connection queue on this socket will be set to the value passed as
113 * backlog. The third argument specifies a particular local address to
114 * bind t or null to bind to all local address.
116 * @param port The port number to bind to
117 * @param backlog The length of the pending connection queue
118 * @param bindAddr The address to bind to, or null to bind to all addresses
120 * @exception IOException If an error occurs
122 public ServerSocket (int port, int backlog, InetAddress bindAddr)
123 throws java.io.IOException
127 throw new IOException("Cannot initialize Socket implementation");
129 SecurityManager s = System.getSecurityManager();
133 if (bindAddr == null)
134 bindAddr = InetAddress.ANY_IF;
137 impl.bind(bindAddr, port);
138 impl.listen(backlog);
142 * This method returns the local address to which this socket is bound
144 * @return The socket's local address
146 public InetAddress getInetAddress()
148 return impl.getInetAddress();
152 * This method returns the local port number to which this socket is bound
154 * @return The socket's port number
156 public int getLocalPort()
158 return impl.getLocalPort();
162 * Accepts a new connection and returns a connected <code>Socket</code>
163 * instance representing that connection. This method will block until a
164 * connection is available.
166 * @exception IOException If an error occurs
168 public Socket accept () throws IOException
170 Socket s = new Socket();
177 * This protected method is used to help subclasses override
178 * <code>ServerSocket.accept()</code>. The passed in socket will be
179 * connected when this method returns.
181 * @param socket The socket that is used for the accepted connection
183 * @exception IOException If an error occurs
185 protected final void implAccept (Socket s) throws IOException
191 * Closes this socket and stops listening for connections
193 * @exception IOException If an error occurs
195 public void close () throws IOException
201 * Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is
202 * disabled (ie, operations never time out). This is the number of
203 * milliseconds a socket operation can block before an
204 * InterruptedIOException is thrown.
206 * @param timeout The new SO_TIMEOUT value
208 * @exception IOException If an error occurs
210 public void setSoTimeout (int timeout) throws SocketException
213 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
215 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
219 * Retrieves the current value of the SO_TIMEOUT setting. A value of 0
220 * implies that SO_TIMEOUT is disabled (ie, operations never time out).
221 * This is the number of milliseconds a socket operation can block before
222 * an InterruptedIOException is thrown.
224 * @return The value of SO_TIMEOUT
226 * @exception IOException If an error occurs
228 public int getSoTimeout () throws IOException
230 Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
232 if (!(timeout instanceof Integer))
233 throw new IOException("Internal Error");
235 return ((Integer)timeout).intValue();
239 * Returns the value of this socket as a <code>String</code>.
241 * @return This socket represented as a <code>String</code>.
243 public String toString ()
245 return "ServerSocket " + impl.toString();
251 * Sets the <code>SocketImplFactory</code> for all
252 * <code>ServerSocket</code>'s. This may only be done
253 * once per virtual machine. Subsequent attempts will generate an
254 * exception. Note that a <code>SecurityManager</code> check is made prior
255 * to setting the factory. If insufficient privileges exist to set the
256 * factory, an exception will be thrown
258 * @exception SecurityException If this operation is not allowed by the
259 * <code>SecurityManager</code>.
260 * @exception SocketException If the factory object is already defined
261 * @exception IOException If any other error occurs
263 public static synchronized void setSocketFactory (SocketImplFactory fac)