KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > ServerSocket


1 /*
2  * @(#)ServerSocket.java 1.86 04/05/24
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.FileDescriptor JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.nio.channels.ServerSocketChannel JavaDoc;
13 import java.security.AccessController JavaDoc;
14 import java.security.PrivilegedExceptionAction JavaDoc;
15
16 /**
17  * This class implements server sockets. A server socket waits for
18  * requests to come in over the network. It performs some operation
19  * based on that request, and then possibly returns a result to the requester.
20  * <p>
21  * The actual work of the server socket is performed by an instance
22  * of the <code>SocketImpl</code> class. An application can
23  * change the socket factory that creates the socket
24  * implementation to configure itself to create sockets
25  * appropriate to the local firewall.
26  *
27  * @author unascribed
28  * @version 1.86, 05/24/04
29  * @see java.net.SocketImpl
30  * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
31  * @see java.nio.channels.ServerSocketChannel
32  * @since JDK1.0
33  */

34 public
35 class ServerSocket {
36     /**
37      * Various states of this socket.
38      */

39     private boolean created = false;
40     private boolean bound = false;
41     private boolean closed = false;
42     private Object JavaDoc closeLock = new Object JavaDoc();
43
44     /**
45      * The implementation of this Socket.
46      */

47     private SocketImpl JavaDoc impl;
48
49     /**
50      * Are we using an older SocketImpl?
51      */

52     private boolean oldImpl = false;
53
54     /**
55      * Creates an unbound server socket.
56      *
57      * @exception IOException IO error when opening the socket.
58      * @revised 1.4
59      */

60     public ServerSocket() throws IOException JavaDoc {
61     setImpl();
62     }
63
64     /**
65      * Creates a server socket, bound to the specified port. A port of
66      * <code>0</code> creates a socket on any free port.
67      * <p>
68      * The maximum queue length for incoming connection indications (a
69      * request to connect) is set to <code>50</code>. If a connection
70      * indication arrives when the queue is full, the connection is refused.
71      * <p>
72      * If the application has specified a server socket factory, that
73      * factory's <code>createSocketImpl</code> method is called to create
74      * the actual socket implementation. Otherwise a "plain" socket is created.
75      * <p>
76      * If there is a security manager,
77      * its <code>checkListen</code> method is called
78      * with the <code>port</code> argument
79      * as its argument to ensure the operation is allowed.
80      * This could result in a SecurityException.
81      *
82      *
83      * @param port the port number, or <code>0</code> to use any
84      * free port.
85      *
86      * @exception IOException if an I/O error occurs when opening the socket.
87      * @exception SecurityException
88      * if a security manager exists and its <code>checkListen</code>
89      * method doesn't allow the operation.
90      *
91      * @see java.net.SocketImpl
92      * @see java.net.SocketImplFactory#createSocketImpl()
93      * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
94      * @see SecurityManager#checkListen
95      */

96     public ServerSocket(int port) throws IOException JavaDoc {
97     this(port, 50, null);
98     }
99
100     /**
101      * Creates a server socket and binds it to the specified local port
102      * number, with the specified backlog.
103      * A port number of <code>0</code> creates a socket on any
104      * free port.
105      * <p>
106      * The maximum queue length for incoming connection indications (a
107      * request to connect) is set to the <code>backlog</code> parameter. If
108      * a connection indication arrives when the queue is full, the
109      * connection is refused.
110      * <p>
111      * If the application has specified a server socket factory, that
112      * factory's <code>createSocketImpl</code> method is called to create
113      * the actual socket implementation. Otherwise a "plain" socket is created.
114      * <p>
115      * If there is a security manager,
116      * its <code>checkListen</code> method is called
117      * with the <code>port</code> argument
118      * as its argument to ensure the operation is allowed.
119      * This could result in a SecurityException.
120      *
121      * <P>The <code>backlog</code> argument must be a positive
122      * value greater than 0. If the value passed if equal or less
123      * than 0, then the default value will be assumed.
124      * <P>
125      *
126      * @param port the specified port, or <code>0</code> to use
127      * any free port.
128      * @param backlog the maximum length of the queue.
129      *
130      * @exception IOException if an I/O error occurs when opening the socket.
131      * @exception SecurityException
132      * if a security manager exists and its <code>checkListen</code>
133      * method doesn't allow the operation.
134      *
135      * @see java.net.SocketImpl
136      * @see java.net.SocketImplFactory#createSocketImpl()
137      * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
138      * @see SecurityManager#checkListen
139      */

140     public ServerSocket(int port, int backlog) throws IOException JavaDoc {
141     this(port, backlog, null);
142     }
143
144     /**
145      * Create a server with the specified port, listen backlog, and
146      * local IP address to bind to. The <i>bindAddr</i> argument
147      * can be used on a multi-homed host for a ServerSocket that
148      * will only accept connect requests to one of its addresses.
149      * If <i>bindAddr</i> is null, it will default accepting
150      * connections on any/all local addresses.
151      * The port must be between 0 and 65535, inclusive.
152      *
153      * <P>If there is a security manager, this method
154      * calls its <code>checkListen</code> method
155      * with the <code>port</code> argument
156      * as its argument to ensure the operation is allowed.
157      * This could result in a SecurityException.
158      *
159      * <P>The <code>backlog</code> argument must be a positive
160      * value greater than 0. If the value passed if equal or less
161      * than 0, then the default value will be assumed.
162      * <P>
163      * @param port the local TCP port
164      * @param backlog the listen backlog
165      * @param bindAddr the local InetAddress the server will bind to
166      *
167      * @throws SecurityException if a security manager exists and
168      * its <code>checkListen</code> method doesn't allow the operation.
169      *
170      * @throws IOException if an I/O error occurs when opening the socket.
171      *
172      * @see SocketOptions
173      * @see SocketImpl
174      * @see SecurityManager#checkListen
175      * @since JDK1.1
176      */

177     public ServerSocket(int port, int backlog, InetAddress JavaDoc bindAddr) throws IOException JavaDoc {
178     setImpl();
179     if (port < 0 || port > 0xFFFF)
180         throw new IllegalArgumentException JavaDoc(
181                "Port value out of range: " + port);
182     if (backlog < 1)
183       backlog = 50;
184     try {
185         bind(new InetSocketAddress JavaDoc(bindAddr, port), backlog);
186     } catch(SecurityException JavaDoc e) {
187         close();
188         throw e;
189     } catch(IOException JavaDoc e) {
190         close();
191         throw e;
192     }
193     }
194
195     /**
196      * Get the <code>SocketImpl</code> attached to this socket, creating
197      * it if necessary.
198      *
199      * @return the <code>SocketImpl</code> attached to that ServerSocket.
200      * @throws SocketException if creation fails.
201      * @since 1.4
202      */

203     SocketImpl JavaDoc getImpl() throws SocketException JavaDoc {
204     if (!created)
205         createImpl();
206     return impl;
207     }
208
209     private void checkOldImpl() {
210     if (impl == null)
211         return;
212     // SocketImpl.connect() is a protected method, therefore we need to use
213
// getDeclaredMethod, therefore we need permission to access the member
214
try {
215         AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
216             public Object JavaDoc run() throws NoSuchMethodException JavaDoc {
217             Class JavaDoc[] cl = new Class JavaDoc[2];
218             cl[0] = SocketAddress JavaDoc.class;
219             cl[1] = Integer.TYPE;
220             impl.getClass().getDeclaredMethod("connect", cl);
221             return null;
222             }
223         });
224     } catch (java.security.PrivilegedActionException JavaDoc e) {
225         oldImpl = true;
226     }
227     }
228
229     private void setImpl() {
230     if (factory != null) {
231         impl = factory.createSocketImpl();
232         checkOldImpl();
233     } else {
234         // No need to do a checkOldImpl() here, we know it's an up to date
235
// SocketImpl!
236
impl = new SocksSocketImpl JavaDoc();
237     }
238     if (impl != null)
239         impl.setServerSocket(this);
240     }
241
242     /**
243      * Creates the socket implementation.
244      *
245      * @throws IOException if creation fails
246      * @since 1.4
247      */

248     void createImpl() throws SocketException JavaDoc {
249     if (impl == null)
250         setImpl();
251     try {
252         impl.create(true);
253         created = true;
254     } catch (IOException JavaDoc e) {
255         throw new SocketException JavaDoc(e.getMessage());
256     }
257     }
258
259     /**
260      *
261      * Binds the <code>ServerSocket</code> to a specific address
262      * (IP address and port number).
263      * <p>
264      * If the address is <code>null</code>, then the system will pick up
265      * an ephemeral port and a valid local address to bind the socket.
266      * <p>
267      * @param endpoint The IP address & port number to bind to.
268      * @throws IOException if the bind operation fails, or if the socket
269      * is already bound.
270      * @throws SecurityException if a <code>SecurityManager</code> is present and
271      * its <code>checkListen</code> method doesn't allow the operation.
272      * @throws IllegalArgumentException if endpoint is a
273      * SocketAddress subclass not supported by this socket
274      * @since 1.4
275      */

276     public void bind(SocketAddress JavaDoc endpoint) throws IOException JavaDoc {
277     bind(endpoint, 50);
278     }
279
280     /**
281      *
282      * Binds the <code>ServerSocket</code> to a specific address
283      * (IP address and port number).
284      * <p>
285      * If the address is <code>null</code>, then the system will pick up
286      * an ephemeral port and a valid local address to bind the socket.
287      * <P>
288      * The <code>backlog</code> argument must be a positive
289      * value greater than 0. If the value passed if equal or less
290      * than 0, then the default value will be assumed.
291      * @param endpoint The IP address & port number to bind to.
292      * @param backlog The listen backlog length.
293      * @throws IOException if the bind operation fails, or if the socket
294      * is already bound.
295      * @throws SecurityException if a <code>SecurityManager</code> is present and
296      * its <code>checkListen</code> method doesn't allow the operation.
297      * @throws IllegalArgumentException if endpoint is a
298      * SocketAddress subclass not supported by this socket
299      * @since 1.4
300      */

301     public void bind(SocketAddress JavaDoc endpoint, int backlog) throws IOException JavaDoc {
302     if (isClosed())
303         throw new SocketException JavaDoc("Socket is closed");
304     if (!oldImpl && isBound())
305         throw new SocketException JavaDoc("Already bound");
306     if (endpoint == null)
307         endpoint = new InetSocketAddress JavaDoc(0);
308     if (!(endpoint instanceof InetSocketAddress JavaDoc))
309         throw new IllegalArgumentException JavaDoc("Unsupported address type");
310     InetSocketAddress JavaDoc epoint = (InetSocketAddress JavaDoc) endpoint;
311     if (epoint.isUnresolved())
312         throw new SocketException JavaDoc("Unresolved address");
313     if (backlog < 1)
314       backlog = 50;
315     try {
316         SecurityManager JavaDoc security = System.getSecurityManager();
317         if (security != null)
318         security.checkListen(epoint.getPort());
319         getImpl().bind(epoint.getAddress(), epoint.getPort());
320         getImpl().listen(backlog);
321         bound = true;
322     } catch(SecurityException JavaDoc e) {
323         bound = false;
324         throw e;
325     } catch(IOException JavaDoc e) {
326         bound = false;
327         throw e;
328     }
329     }
330
331     /**
332      * Returns the local address of this server socket.
333      *
334      * @return the address to which this socket is bound,
335      * or <code>null</code> if the socket is unbound.
336      */

337     public InetAddress JavaDoc getInetAddress() {
338     if (!isBound())
339         return null;
340     try {
341         return getImpl().getInetAddress();
342     } catch (SocketException JavaDoc e) {
343         // nothing
344
// If we're bound, the the impl has been created
345
// so we shouldn't get here
346
}
347     return null;
348     }
349
350     /**
351      * Returns the port on which this socket is listening.
352      *
353      * @return the port number to which this socket is listening or
354      * -1 if the socket is not bound yet.
355      */

356     public int getLocalPort() {
357     if (!isBound())
358         return -1;
359     try {
360         return getImpl().getLocalPort();
361     } catch (SocketException JavaDoc e) {
362         // nothing
363
// If we're bound, the the impl has been created
364
// so we shouldn't get here
365
}
366     return -1;
367     }
368
369     /**
370      * Returns the address of the endpoint this socket is bound to, or
371      * <code>null</code> if it is not bound yet.
372      *
373      * @return a <code>SocketAddress</code> representing the local endpoint of this
374      * socket, or <code>null</code> if it is not bound yet.
375      * @see #getInetAddress()
376      * @see #getLocalPort()
377      * @see #bind(SocketAddress)
378      * @since 1.4
379      */

380
381     public SocketAddress JavaDoc getLocalSocketAddress() {
382     if (!isBound())
383         return null;
384     return new InetSocketAddress JavaDoc(getInetAddress(), getLocalPort());
385     }
386
387     /**
388      * Listens for a connection to be made to this socket and accepts
389      * it. The method blocks until a connection is made.
390      *
391      * <p>A new Socket <code>s</code> is created and, if there
392      * is a security manager,
393      * the security manager's <code>checkAccept</code> method is called
394      * with <code>s.getInetAddress().getHostAddress()</code> and
395      * <code>s.getPort()</code>
396      * as its arguments to ensure the operation is allowed.
397      * This could result in a SecurityException.
398      *
399      * @exception IOException if an I/O error occurs when waiting for a
400      * connection.
401      * @exception SecurityException if a security manager exists and its
402      * <code>checkListen</code> method doesn't allow the operation.
403      * @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and
404      * the timeout has been reached.
405      * @exception java.nio.channels.IllegalBlockingModeException
406      * if this socket has an associated channel, the channel is in
407      * non-blocking mode, and there is no connection ready to be
408      * accepted
409      *
410      * @return the new Socket
411      * @see SecurityManager#checkAccept
412      * @revised 1.4
413      * @spec JSR-51
414      */

415     public Socket JavaDoc accept() throws IOException JavaDoc {
416     if (isClosed())
417         throw new SocketException JavaDoc("Socket is closed");
418     if (!isBound())
419         throw new SocketException JavaDoc("Socket is not bound yet");
420     Socket JavaDoc s = new Socket JavaDoc((SocketImpl JavaDoc) null);
421     implAccept(s);
422     return s;
423     }
424
425     /**
426      * Subclasses of ServerSocket use this method to override accept()
427      * to return their own subclass of socket. So a FooServerSocket
428      * will typically hand this method an <i>empty</i> FooSocket. On
429      * return from implAccept the FooSocket will be connected to a client.
430      *
431      * @param s the Socket
432      * @throws java.nio.channels.IllegalBlockingModeException
433      * if this socket has an associated channel,
434      * and the channel is in non-blocking mode
435      * @throws IOException if an I/O error occurs when waiting
436      * for a connection.
437      * @since JDK1.1
438      * @revised 1.4
439      * @spec JSR-51
440      */

441     protected final void implAccept(Socket JavaDoc s) throws IOException JavaDoc {
442     SocketImpl JavaDoc si = null;
443     try {
444         if (s.impl == null)
445           s.setImpl();
446         si = s.impl;
447         s.impl = null;
448         si.address = new InetAddress JavaDoc();
449         si.fd = new FileDescriptor JavaDoc();
450         getImpl().accept(si);
451         
452         SecurityManager JavaDoc security = System.getSecurityManager();
453         if (security != null) {
454         security.checkAccept(si.getInetAddress().getHostAddress(),
455                      si.getPort());
456         }
457     } catch (IOException JavaDoc e) {
458         if (si != null)
459         si.reset();
460             s.impl = si;
461         throw e;
462     } catch (SecurityException JavaDoc e) {
463         if (si != null)
464         si.reset();
465         s.impl = si;
466         throw e;
467     }
468     s.impl = si;
469     s.postAccept();
470     }
471
472     /**
473      * Closes this socket.
474      *
475      * Any thread currently blocked in {@link #accept()} will throw
476      * a {@link SocketException}.
477      *
478      * <p> If this socket has an associated channel then the channel is closed
479      * as well.
480      *
481      * @exception IOException if an I/O error occurs when closing the socket.
482      * @revised 1.4
483      * @spec JSR-51
484      */

485     public void close() throws IOException JavaDoc {
486     synchronized(closeLock) {
487         if (isClosed())
488         return;
489         if (created)
490         impl.close();
491         closed = true;
492     }
493     }
494
495     /**
496      * Returns the unique {@link java.nio.channels.ServerSocketChannel} object
497      * associated with this socket, if any.
498      *
499      * <p> A server socket will have a channel if, and only if, the channel
500      * itself was created via the {@link
501      * java.nio.channels.ServerSocketChannel#open ServerSocketChannel.open}
502      * method.
503      *
504      * @return the server-socket channel associated with this socket,
505      * or <tt>null</tt> if this socket was not created
506      * for a channel
507      *
508      * @since 1.4
509      * @spec JSR-51
510      */

511     public ServerSocketChannel JavaDoc getChannel() {
512     return null;
513     }
514
515     /**
516      * Returns the binding state of the ServerSocket.
517      *
518      * @return true if the ServerSocket succesfuly bound to an address
519      * @since 1.4
520      */

521     public boolean isBound() {
522     // Before 1.3 ServerSockets were always bound during creation
523
return bound || oldImpl;
524     }
525
526     /**
527      * Returns the closed state of the ServerSocket.
528      *
529      * @return true if the socket has been closed
530      * @since 1.4
531      */

532     public boolean isClosed() {
533     synchronized(closeLock) {
534         return closed;
535     }
536     }
537
538     /**
539      * Enable/disable SO_TIMEOUT with the specified timeout, in
540      * milliseconds. With this option set to a non-zero timeout,
541      * a call to accept() for this ServerSocket
542      * will block for only this amount of time. If the timeout expires,
543      * a <B>java.net.SocketTimeoutException</B> is raised, though the
544      * ServerSocket is still valid. The option <B>must</B> be enabled
545      * prior to entering the blocking operation to have effect. The
546      * timeout must be > 0.
547      * A timeout of zero is interpreted as an infinite timeout.
548      * @param timeout the specified timeout, in milliseconds
549      * @exception SocketException if there is an error in
550      * the underlying protocol, such as a TCP error.
551      * @since JDK1.1
552      * @see #getSoTimeout()
553      */

554     public synchronized void setSoTimeout(int timeout) throws SocketException JavaDoc {
555     if (isClosed())
556         throw new SocketException JavaDoc("Socket is closed");
557     getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer JavaDoc(timeout));
558     }
559
560     /**
561      * Retrive setting for SO_TIMEOUT. 0 returns implies that the
562      * option is disabled (i.e., timeout of infinity).
563      * @return the SO_TIMEOUT value
564      * @exception IOException if an I/O error occurs
565      * @since JDK1.1
566      * @see #setSoTimeout(int)
567      */

568     public synchronized int getSoTimeout() throws IOException JavaDoc {
569     if (isClosed())
570         throw new SocketException JavaDoc("Socket is closed");
571     Object JavaDoc o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
572     /* extra type safety */
573     if (o instanceof Integer JavaDoc) {
574         return ((Integer JavaDoc) o).intValue();
575     } else {
576         return 0;
577     }
578     }
579
580     /**
581      * Enable/disable the SO_REUSEADDR socket option.
582      * <p>
583      * When a TCP connection is closed the connection may remain
584      * in a timeout state for a period of time after the connection
585      * is closed (typically known as the <tt>TIME_WAIT</tt> state
586      * or <tt>2MSL</tt> wait state).
587      * For applications using a well known socket address or port
588      * it may not be possible to bind a socket to the required
589      * <tt>SocketAddress</tt> if there is a connection in the
590      * timeout state involving the socket address or port.
591      * <p>
592      * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket
593      * using {@link #bind(SocketAddress)} allows the socket to be
594      * bound even though a previous connection is in a timeout
595      * state.
596      * <p>
597      * When a <tt>ServerSocket</tt> is created the initial setting
598      * of <tt>SO_REUSEADDR</tt> is not defined. Applications can
599      * use {@link #getReuseAddress()} to determine the initial
600      * setting of <tt>SO_REUSEADDR</tt>.
601      * <p>
602      * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
603      * disabled after a socket is bound (See {@link #isBound()})
604      * is not defined.
605      *
606      * @param on whether to enable or disable the socket option
607      * @exception SocketException if an error occurs enabling or
608      * disabling the <tt>SO_RESUEADDR</tt> socket option,
609      * or the socket is closed.
610      * @since 1.4
611      * @see #getReuseAddress()
612      * @see #bind(SocketAddress)
613      * @see #isBound()
614      * @see #isClosed()
615      */

616     public void setReuseAddress(boolean on) throws SocketException JavaDoc {
617     if (isClosed())
618         throw new SocketException JavaDoc("Socket is closed");
619         getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
620     }
621
622     /**
623      * Tests if SO_REUSEADDR is enabled.
624      *
625      * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
626      * @exception SocketException if there is an error
627      * in the underlying protocol, such as a TCP error.
628      * @since 1.4
629      * @see #setReuseAddress(boolean)
630      */

631     public boolean getReuseAddress() throws SocketException JavaDoc {
632     if (isClosed())
633         throw new SocketException JavaDoc("Socket is closed");
634     return ((Boolean JavaDoc) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue();
635     }
636
637     /**
638      * Returns the implementation address and implementation port of
639      * this socket as a <code>String</code>.
640      *
641      * @return a string representation of this socket.
642      */

643     public String JavaDoc toString() {
644     if (!isBound())
645         return "ServerSocket[unbound]";
646     return "ServerSocket[addr=" + impl.getInetAddress() +
647         ",port=" + impl.getPort() +
648         ",localport=" + impl.getLocalPort() + "]";
649     }
650
651     void setBound() {
652     bound = true;
653     }
654
655     void setCreated() {
656     created = true;
657     }
658
659     /**
660      * The factory for all server sockets.
661      */

662     private static SocketImplFactory JavaDoc factory = null;
663
664     /**
665      * Sets the server socket implementation factory for the
666      * application. The factory can be specified only once.
667      * <p>
668      * When an application creates a new server socket, the socket
669      * implementation factory's <code>createSocketImpl</code> method is
670      * called to create the actual socket implementation.
671      * <p>
672      * Passing <code>null</code> to the method is a no-op unless the factory
673      * was already set.
674      * <p>
675      * If there is a security manager, this method first calls
676      * the security manager's <code>checkSetFactory</code> method
677      * to ensure the operation is allowed.
678      * This could result in a SecurityException.
679      *
680      * @param fac the desired factory.
681      * @exception IOException if an I/O error occurs when setting the
682      * socket factory.
683      * @exception SocketException if the factory has already been defined.
684      * @exception SecurityException if a security manager exists and its
685      * <code>checkSetFactory</code> method doesn't allow the operation.
686      * @see java.net.SocketImplFactory#createSocketImpl()
687      * @see SecurityManager#checkSetFactory
688      */

689     public static synchronized void setSocketFactory(SocketImplFactory JavaDoc fac) throws IOException JavaDoc {
690     if (factory != null) {
691         throw new SocketException JavaDoc("factory already defined");
692     }
693     SecurityManager JavaDoc security = System.getSecurityManager();
694     if (security != null) {
695         security.checkSetFactory();
696     }
697     factory = fac;
698     }
699
700     /**
701      * Sets a default proposed value for the SO_RCVBUF option for sockets
702      * accepted from this <tt>ServerSocket</tt>. The value actually set
703      * in the accepted socket must be determined by calling
704      * {@link Socket#getReceiveBufferSize()} after the socket
705      * is returned by {@link #accept()}.
706      * <p>
707      * The value of SO_RCVBUF is used both to set the size of the internal
708      * socket receive buffer, and to set the size of the TCP receive window
709      * that is advertized to the remote peer.
710      * <p>
711      * It is possible to change the value subsequently, by calling
712      * {@link Socket#setReceiveBufferSize(int)}. However, if the application
713      * wishes to allow a receive window larger than 64K bytes, as defined by RFC1323
714      * then the proposed value must be set in the ServerSocket <B>before</B>
715      * it is bound to a local address. This implies, that the ServerSocket must be
716      * created with the no-argument constructor, then setReceiveBufferSize() must
717      * be called and lastly the ServerSocket is bound to an address by calling bind().
718      * <p>
719      * Failure to do this will not cause an error, and the buffer size may be set to the
720      * requested value but the TCP receive window in sockets accepted from
721      * this ServerSocket will be no larger than 64K bytes.
722      *
723      * @exception SocketException if there is an error
724      * in the underlying protocol, such as a TCP error.
725      *
726      * @param size the size to which to set the receive buffer
727      * size. This value must be greater than 0.
728      *
729      * @exception IllegalArgumentException if the
730      * value is 0 or is negative.
731      *
732      * @since 1.4
733      * @see #getReceiveBufferSize
734      */

735      public synchronized void setReceiveBufferSize (int size) throws SocketException JavaDoc {
736     if (!(size > 0)) {
737         throw new IllegalArgumentException JavaDoc("negative receive size");
738     }
739     if (isClosed())
740         throw new SocketException JavaDoc("Socket is closed");
741     getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer JavaDoc(size));
742     }
743
744     /**
745      * Gets the value of the SO_RCVBUF option for this <tt>ServerSocket</tt>,
746      * that is the proposed buffer size that will be used for Sockets accepted
747      * from this <tt>ServerSocket</tt>.
748      *
749      * <p>Note, the value actually set in the accepted socket is determined by
750      * calling {@link Socket#getReceiveBufferSize()}.
751      * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.
752      * @exception SocketException if there is an error
753      * in the underlying protocol, such as a TCP error.
754      * @see #setReceiveBufferSize(int)
755      * @since 1.4
756      */

757     public synchronized int getReceiveBufferSize()
758     throws SocketException JavaDoc{
759     if (isClosed())
760         throw new SocketException JavaDoc("Socket is closed");
761     int result = 0;
762     Object JavaDoc o = getImpl().getOption(SocketOptions.SO_RCVBUF);
763     if (o instanceof Integer JavaDoc) {
764         result = ((Integer JavaDoc)o).intValue();
765     }
766     return result;
767     }
768
769     /**
770      * Sets performance preferences for this ServerSocket.
771      *
772      * <p> Sockets use the TCP/IP protocol by default. Some implementations
773      * may offer alternative protocols which have different performance
774      * characteristics than TCP/IP. This method allows the application to
775      * express its own preferences as to how these tradeoffs should be made
776      * when the implementation chooses from the available protocols.
777      *
778      * <p> Performance preferences are described by three integers
779      * whose values indicate the relative importance of short connection time,
780      * low latency, and high bandwidth. The absolute values of the integers
781      * are irrelevant; in order to choose a protocol the values are simply
782      * compared, with larger values indicating stronger preferences. If the
783      * application prefers short connection time over both low latency and high
784      * bandwidth, for example, then it could invoke this method with the values
785      * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low
786      * latency, and low latency above short connection time, then it could
787      * invoke this method with the values <tt>(0, 1, 2)</tt>.
788      *
789      * <p> Invoking this method after this socket has been bound
790      * will have no effect. This implies that in order to use this capability
791      * requires the socket to be created with the no-argument constructor.
792      *
793      * @param connectionTime
794      * An <tt>int</tt> expressing the relative importance of a short
795      * connection time
796      *
797      * @param latency
798      * An <tt>int</tt> expressing the relative importance of low
799      * latency
800      *
801      * @param bandwidth
802      * An <tt>int</tt> expressing the relative importance of high
803      * bandwidth
804      *
805      * @since 1.5
806      */

807     public void setPerformancePreferences(int connectionTime,
808                                           int latency,
809                                           int bandwidth)
810     {
811     /* Not implemented yet */
812     }
813  
814 }
815
Popular Tags