77 |
private SocketImpl impl; |
private SocketImpl impl; |
78 |
|
|
79 |
/** |
/** |
80 |
|
* ServerSocketChannel of this ServerSocket. This channel only exists |
81 |
|
* when the socket is created by ServerSocketChannel.open(). |
82 |
|
*/ |
83 |
|
private ServerSocketChannel ch; |
84 |
|
|
85 |
|
/** |
86 |
* Constructor that simply sets the implementation. |
* Constructor that simply sets the implementation. |
87 |
* |
* |
88 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
124 |
* @param backlog The length of the pending connection queue |
* @param backlog The length of the pending connection queue |
125 |
* |
* |
126 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
127 |
|
* @exception SecurityException If a security manager exists and its |
128 |
|
* checkListen method doesn't allow the operation |
129 |
*/ |
*/ |
130 |
public ServerSocket (int port, int backlog) |
public ServerSocket (int port, int backlog) |
131 |
throws java.io.IOException |
throws IOException |
132 |
{ |
{ |
133 |
this(port, backlog, null); |
this(port, backlog, null); |
134 |
} |
} |
145 |
* @param bindAddr The address to bind to, or null to bind to all addresses |
* @param bindAddr The address to bind to, or null to bind to all addresses |
146 |
* |
* |
147 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
148 |
|
* @exception SecurityException If a security manager exists and its |
149 |
|
* checkListen method doesn't allow the operation |
150 |
* |
* |
151 |
* @since 1.1 |
* @since 1.1 |
152 |
*/ |
*/ |
153 |
public ServerSocket (int port, int backlog, InetAddress bindAddr) |
public ServerSocket (int port, int backlog, InetAddress bindAddr) |
154 |
throws java.io.IOException |
throws IOException |
155 |
{ |
{ |
156 |
this(); |
this(); |
157 |
if (impl == null) |
if (impl == null) |
176 |
*/ |
*/ |
177 |
public InetAddress getInetAddress() |
public InetAddress getInetAddress() |
178 |
{ |
{ |
179 |
return impl.getInetAddress(); |
try |
180 |
|
{ |
181 |
|
return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR); |
182 |
|
} |
183 |
|
catch (SocketException e) |
184 |
|
{ |
185 |
|
return null; |
186 |
|
} |
187 |
} |
} |
188 |
|
|
189 |
/** |
/** |
197 |
} |
} |
198 |
|
|
199 |
/** |
/** |
200 |
|
* Returns the local socket address |
201 |
|
* |
202 |
|
* @since 1.4 |
203 |
|
*/ |
204 |
|
public SocketAddress getLocalSocketAddress() |
205 |
|
{ |
206 |
|
InetAddress addr = getInetAddress(); |
207 |
|
|
208 |
|
if (addr != null) |
209 |
|
return new InetSocketAddress (getInetAddress(), getLocalPort()); |
210 |
|
|
211 |
|
return null; |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
* Accepts a new connection and returns a connected <code>Socket</code> |
* Accepts a new connection and returns a connected <code>Socket</code> |
216 |
* instance representing that connection. This method will block until a |
* instance representing that connection. This method will block until a |
217 |
* connection is available. |
* connection is available. |
218 |
* |
* |
219 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
220 |
|
* @exception SecurityException If a security manager exists and its |
221 |
|
* checkListen method doesn't allow the operation |
222 |
|
* @exception IllegalBlockingModeException If this socket has an associated |
223 |
|
* channel, and the channel is in non-blocking mode |
224 |
|
* @exception SocketTimeoutException If a timeout was previously set with |
225 |
|
* setSoTimeout and the timeout has been reached |
226 |
*/ |
*/ |
227 |
public Socket accept () throws IOException |
public Socket accept () throws IOException |
228 |
{ |
{ |
229 |
Socket s = new Socket(); |
Socket s = new Socket(); |
230 |
implAccept (s); |
implAccept (s); |
240 |
* @param socket The socket that is used for the accepted connection |
* @param socket The socket that is used for the accepted connection |
241 |
* |
* |
242 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
243 |
|
* @exception IllegalBlockingModeException If this socket has an associated |
244 |
|
* channel, and the channel is in non-blocking mode |
245 |
* |
* |
246 |
* @since 1.1 |
* @since 1.1 |
247 |
*/ |
*/ |
248 |
protected final void implAccept (Socket s) throws IOException |
protected final void implAccept (Socket s) |
249 |
|
throws IOException |
250 |
{ |
{ |
251 |
|
if (ch != null && !ch.isBlocking()) |
252 |
|
throw new IllegalBlockingModeException(); |
253 |
|
|
254 |
impl.accept(s.impl); |
impl.accept(s.impl); |
255 |
} |
} |
256 |
|
|
265 |
} |
} |
266 |
|
|
267 |
/** |
/** |
268 |
|
* Returns the unique ServerSocketChannel object |
269 |
|
* associated with this socket, if any. |
270 |
|
* |
271 |
|
* The socket only has a ServerSocketChannel if its created |
272 |
|
* by ServerSocketChannel.open. |
273 |
|
* |
274 |
|
* @since 1.4 |
275 |
|
*/ |
276 |
|
public ServerSocketChannel getChannel() |
277 |
|
{ |
278 |
|
return ch; |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
|
* Returns true then the socket is bound, otherwise false |
283 |
|
* |
284 |
|
* @since 1.4 |
285 |
|
*/ |
286 |
|
public boolean isBound() |
287 |
|
{ |
288 |
|
try |
289 |
|
{ |
290 |
|
Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR); |
291 |
|
} |
292 |
|
catch (SocketException e) |
293 |
|
{ |
294 |
|
return false; |
295 |
|
} |
296 |
|
|
297 |
|
return true; |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
* Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is |
* Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is |
302 |
* disabled (ie, operations never time out). This is the number of |
* disabled (ie, operations never time out). This is the number of |
303 |
* milliseconds a socket operation can block before an |
* milliseconds a socket operation can block before an |
305 |
* |
* |
306 |
* @param timeout The new SO_TIMEOUT value |
* @param timeout The new SO_TIMEOUT value |
307 |
* |
* |
308 |
* @exception IOException If an error occurs |
* @exception SocketException If an error occurs |
309 |
* |
* |
310 |
* @since 1.1 |
* @since 1.1 |
311 |
*/ |
*/ |
340 |
} |
} |
341 |
|
|
342 |
/** |
/** |
343 |
|
* Enables/Disables the SO_REUSEADDR option |
344 |
|
* |
345 |
|
* @exception SocketException If an error occurs |
346 |
|
* |
347 |
|
* @since 1.4 |
348 |
|
*/ |
349 |
|
public void setReuseAddress (boolean on) |
350 |
|
throws SocketException |
351 |
|
{ |
352 |
|
if (impl == null) |
353 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
354 |
|
|
355 |
|
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
|
* Checks if the SO_REUSEADDR option is enabled |
360 |
|
* |
361 |
|
* @exception SocketException If an error occurs |
362 |
|
* |
363 |
|
* @since 1.4 |
364 |
|
*/ |
365 |
|
public boolean getReuseAddress() |
366 |
|
throws SocketException |
367 |
|
{ |
368 |
|
if (impl == null) |
369 |
|
throw new SocketException ("Cannot initialize Socket implementation"); |
370 |
|
|
371 |
|
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); |
372 |
|
|
373 |
|
if (!(reuseaddr instanceof Boolean)) |
374 |
|
throw new SocketException ("Internal Error"); |
375 |
|
|
376 |
|
return ((Boolean) reuseaddr).booleanValue (); |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
|
* This method sets the value for the system level socket option |
381 |
|
* SO_RCVBUF to the specified value. Note that valid values for this |
382 |
|
* option are specific to a given operating system. |
383 |
|
* |
384 |
|
* @param size The new receive buffer size. |
385 |
|
* |
386 |
|
* @exception SocketException If an error occurs or Socket is not connected |
387 |
|
* @exception IllegalArgumentException If size is 0 or negative |
388 |
|
* |
389 |
|
* @since 1.4 |
390 |
|
*/ |
391 |
|
public void setReceiveBufferSize (int size) |
392 |
|
throws SocketException |
393 |
|
{ |
394 |
|
if (impl == null) |
395 |
|
throw new SocketException ("Not connected"); |
396 |
|
|
397 |
|
if (size <= 0) |
398 |
|
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0"); |
399 |
|
|
400 |
|
impl.setOption (SocketOptions.SO_RCVBUF, new Integer (size)); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* This method returns the value of the system level socket option |
405 |
|
* SO_RCVBUF, which is used by the operating system to tune buffer |
406 |
|
* sizes for data transfers. |
407 |
|
* |
408 |
|
* @return The receive buffer size. |
409 |
|
* |
410 |
|
* @exception SocketException If an error occurs or Socket is not connected |
411 |
|
* |
412 |
|
* @since 1.4 |
413 |
|
*/ |
414 |
|
public int getReceiveBufferSize () |
415 |
|
throws SocketException |
416 |
|
{ |
417 |
|
if (impl == null) |
418 |
|
throw new SocketException ("Not connected"); |
419 |
|
|
420 |
|
Object buf = impl.getOption (SocketOptions.SO_RCVBUF); |
421 |
|
|
422 |
|
if (!(buf instanceof Integer)) |
423 |
|
throw new SocketException ("Internal Error: Unexpected type"); |
424 |
|
|
425 |
|
return ((Integer) buf).intValue (); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
* Returns the value of this socket as a <code>String</code>. |
* Returns the value of this socket as a <code>String</code>. |
430 |
* |
* |
431 |
* @return This socket represented as a <code>String</code>. |
* @return This socket represented as a <code>String</code>. |
432 |
*/ |
*/ |
433 |
public String toString () |
public String toString () |
434 |
{ |
{ |
435 |
return "ServerSocket " + impl.toString(); |
return "ServerSocket" + impl.toString(); |
436 |
} |
} |
437 |
|
|
438 |
// Class methods |
// Class methods |