source: trunk/src/gcc/libjava/java/net/ServerSocket.java@ 608

Last change on this file since 608 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.5 KB
Line 
1/* ServerSocket.java -- Class for implementing server side sockets
2 Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.net;
39
40import java.io.IOException;
41
42/* Written using on-line Java Platform 1.2 API Specification.
43 * Status: I believe all methods are implemented.
44 */
45
46/**
47 * This class models server side sockets. The basic model is that the
48 * server socket is created and bound to some well known port. It then
49 * listens for and accepts connections. At that point the client and
50 * server sockets are ready to communicate with one another utilizing
51 * whatever application layer protocol they desire.
52 * <p>
53 * As with the <code>Socket</code> class, most instance methods of this class
54 * simply redirect their calls to an implementation class.
55 *
56 * @author Aaron M. Renn ([email protected])
57 * @author Per Bothner ([email protected])
58 */
59public class ServerSocket
60{
61
62 // Class Variables
63
64 /**
65 * This is the user defined SocketImplFactory, if one is supplied
66 */
67 private static SocketImplFactory factory;
68
69 // Instance Variables
70
71 /**
72 * This is the SocketImp object to which most instance methods in this
73 * class are redirected
74 */
75 private SocketImpl impl;
76
77 /**
78 * Private constructor that simply sets the implementation.
79 */
80 private ServerSocket()
81 {
82 if (factory != null)
83 impl = factory.createSocketImpl();
84 else
85 impl = new PlainSocketImpl();
86 }
87
88 /**
89 * Creates a server socket and binds it to the specified port. If the
90 * port number is 0, a random free port will be chosen. The pending
91 * connection queue on this socket will be set to 50.
92 *
93 * @param port The port number to bind to
94 *
95 * @exception IOException If an error occurs
96 */
97 public ServerSocket (int port)
98 throws java.io.IOException
99 {
100 this(port, 50);
101 }
102
103 /**
104 * Creates a server socket and binds it to the specified port. If the
105 * port number is 0, a random free port will be chosen. The pending
106 * connection queue on this socket will be set to the value passed as
107 * arg2.
108 *
109 * @param port The port number to bind to
110 * @param backlog The length of the pending connection queue
111 *
112 * @exception IOException If an error occurs
113 */
114 public ServerSocket (int port, int backlog)
115 throws java.io.IOException
116 {
117 this(port, backlog, null);
118 }
119
120 /**
121 * Creates a server socket and binds it to the specified port. If the
122 * port number is 0, a random free port will be chosen. The pending
123 * connection queue on this socket will be set to the value passed as
124 * backlog. The third argument specifies a particular local address to
125 * bind t or null to bind to all local address.
126 *
127 * @param port The port number to bind to
128 * @param backlog The length of the pending connection queue
129 * @param bindAddr The address to bind to, or null to bind to all addresses
130 *
131 * @exception IOException If an error occurs
132 */
133 public ServerSocket (int port, int backlog, InetAddress bindAddr)
134 throws java.io.IOException
135 {
136 this();
137 if (impl == null)
138 throw new IOException("Cannot initialize Socket implementation");
139
140 SecurityManager s = System.getSecurityManager();
141 if (s != null)
142 s.checkListen(port);
143
144 if (bindAddr == null)
145 bindAddr = InetAddress.ANY_IF;
146
147 impl.create(true);
148 impl.bind(bindAddr, port);
149 impl.listen(backlog);
150 }
151
152 /**
153 * This method returns the local address to which this socket is bound
154 *
155 * @return The socket's local address
156 */
157 public InetAddress getInetAddress()
158 {
159 return impl.getInetAddress();
160 }
161
162 /**
163 * This method returns the local port number to which this socket is bound
164 *
165 * @return The socket's port number
166 */
167 public int getLocalPort()
168 {
169 return impl.getLocalPort();
170 }
171
172 /**
173 * Accepts a new connection and returns a connected <code>Socket</code>
174 * instance representing that connection. This method will block until a
175 * connection is available.
176 *
177 * @exception IOException If an error occurs
178 */
179 public Socket accept () throws IOException
180 {
181 Socket s = new Socket();
182 implAccept (s);
183
184 return s;
185 }
186
187 /**
188 * This protected method is used to help subclasses override
189 * <code>ServerSocket.accept()</code>. The passed in socket will be
190 * connected when this method returns.
191 *
192 * @param socket The socket that is used for the accepted connection
193 *
194 * @exception IOException If an error occurs
195 */
196 protected final void implAccept (Socket s) throws IOException
197 {
198 impl.accept(s.impl);
199 }
200
201 /**
202 * Closes this socket and stops listening for connections
203 *
204 * @exception IOException If an error occurs
205 */
206 public void close () throws IOException
207 {
208 impl.close();
209 }
210
211 /**
212 * Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is
213 * disabled (ie, operations never time out). This is the number of
214 * milliseconds a socket operation can block before an
215 * InterruptedIOException is thrown.
216 *
217 * @param timeout The new SO_TIMEOUT value
218 *
219 * @exception IOException If an error occurs
220 */
221 public void setSoTimeout (int timeout) throws SocketException
222 {
223 if (timeout < 0)
224 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
225
226 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
227 }
228
229 /**
230 * Retrieves the current value of the SO_TIMEOUT setting. A value of 0
231 * implies that SO_TIMEOUT is disabled (ie, operations never time out).
232 * This is the number of milliseconds a socket operation can block before
233 * an InterruptedIOException is thrown.
234 *
235 * @return The value of SO_TIMEOUT
236 *
237 * @exception IOException If an error occurs
238 */
239 public int getSoTimeout () throws IOException
240 {
241 Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
242
243 if (!(timeout instanceof Integer))
244 throw new IOException("Internal Error");
245
246 return ((Integer)timeout).intValue();
247 }
248
249 /**
250 * Returns the value of this socket as a <code>String</code>.
251 *
252 * @return This socket represented as a <code>String</code>.
253 */
254 public String toString ()
255 {
256 return "ServerSocket " + impl.toString();
257 }
258
259 // Class methods
260
261 /**
262 * Sets the <code>SocketImplFactory</code> for all
263 * <code>ServerSocket</code>'s. This may only be done
264 * once per virtual machine. Subsequent attempts will generate an
265 * exception. Note that a <code>SecurityManager</code> check is made prior
266 * to setting the factory. If insufficient privileges exist to set the
267 * factory, an exception will be thrown
268 *
269 * @exception SecurityException If this operation is not allowed by the
270 * <code>SecurityManager</code>.
271 * @exception SocketException If the factory object is already defined
272 * @exception IOException If any other error occurs
273 */
274 public static synchronized void setSocketFactory (SocketImplFactory fac)
275 throws IOException
276 {
277 factory = fac;
278 }
279}
Note: See TracBrowser for help on using the repository browser.