diff options
Diffstat (limited to 'libjava/java/net/ServerSocket.java')
-rw-r--r-- | libjava/java/net/ServerSocket.java | 217 |
1 files changed, 114 insertions, 103 deletions
diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java index be3f8008d86..037421d682a 100644 --- a/libjava/java/net/ServerSocket.java +++ b/libjava/java/net/ServerSocket.java @@ -8,7 +8,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -36,7 +36,6 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ - package java.net; import gnu.java.net.PlainSocketImpl; @@ -44,6 +43,7 @@ import java.io.IOException; import java.nio.channels.IllegalBlockingModeException; import java.nio.channels.ServerSocketChannel; + /* Written using on-line Java Platform 1.2 API Specification. * Status: I believe all methods are implemented. */ @@ -55,7 +55,7 @@ import java.nio.channels.ServerSocketChannel; * server sockets are ready to communicate with one another utilizing * whatever application layer protocol they desire. * - * As with the <code>Socket</code> class, most instance methods of this class + * As with the <code>Socket</code> class, most instance methods of this class * simply redirect their calls to an implementation class. * * @author Aaron M. Renn (arenn@urbanophile.com) @@ -78,13 +78,14 @@ public class ServerSocket * True if socket is bound. */ private boolean bound; - + /* * This constructor is only used by java.nio. */ + // FIXME: Workaround a bug in gcj. //ServerSocket (PlainSocketImpl impl) throws IOException - ServerSocket (SocketImpl impl) throws IOException + ServerSocket(SocketImpl impl) throws IOException { if (impl == null) throw new NullPointerException("impl may not be null"); @@ -96,16 +97,17 @@ public class ServerSocket /* * This method is only used by java.nio. */ + // FIXME: Workaround a bug in gcj. //PlainSocketImpl getImpl() SocketImpl getImpl() { return impl; } - + /** * Constructor that simply sets the implementation. - * + * * @exception IOException If an error occurs * * @specnote This constructor is public since JDK 1.4 @@ -126,13 +128,12 @@ public class ServerSocket * connection queue on this socket will be set to 50. * * @param port The port number to bind to - * + * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkListen method doesn't allow the operation */ - public ServerSocket (int port) - throws IOException + public ServerSocket(int port) throws IOException { this(port, 50); } @@ -150,8 +151,7 @@ public class ServerSocket * @exception SecurityException If a security manager exists and its * checkListen method doesn't allow the operation */ - public ServerSocket (int port, int backlog) - throws IOException + public ServerSocket(int port, int backlog) throws IOException { this(port, backlog, null); } @@ -173,13 +173,13 @@ public class ServerSocket * * @since 1.1 */ - public ServerSocket (int port, int backlog, InetAddress bindAddr) + public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { this(); // bind/listen socket - bind (new InetSocketAddress (bindAddr, port), backlog); + bind(new InetSocketAddress(bindAddr, port), backlog); } /** @@ -191,21 +191,20 @@ public class ServerSocket * @exception IllegalArgumentException If address type is not supported * @exception SecurityException If a security manager exists and its * checkListen method doesn't allow the operation - * + * * @since 1.4 */ - public void bind (SocketAddress endpoint) - throws IOException + public void bind(SocketAddress endpoint) throws IOException { - bind (endpoint, 50); + bind(endpoint, 50); } - + /** * Binds the server socket to a specified socket address * * @param endpoint The socket address to bind to * @param backlog The length of the pending connection queue - * + * * @exception IOException If an error occurs * @exception IllegalArgumentException If address type is not supported * @exception SecurityException If a security manager exists and its @@ -213,26 +212,27 @@ public class ServerSocket * * @since 1.4 */ - public void bind (SocketAddress endpoint, int backlog) throws IOException + public void bind(SocketAddress endpoint, int backlog) + throws IOException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - + if (! (endpoint instanceof InetSocketAddress)) - throw new IllegalArgumentException ("Address type not supported"); + throw new IllegalArgumentException("Address type not supported"); InetSocketAddress tmp = (InetSocketAddress) endpoint; - SecurityManager s = System.getSecurityManager (); + SecurityManager s = System.getSecurityManager(); if (s != null) - s.checkListen (tmp.getPort ()); + s.checkListen(tmp.getPort()); InetAddress addr = tmp.getAddress(); - + // Initialize addr with 0.0.0.0. if (addr == null) addr = InetAddress.ANY_IF; - + try { impl.bind(addr, tmp.getPort()); @@ -241,21 +241,21 @@ public class ServerSocket } catch (IOException exception) { - close(); - throw exception; + close(); + throw exception; } catch (RuntimeException exception) { - close(); - throw exception; + close(); + throw exception; } catch (Error error) { - close(); - throw error; + close(); + throw error; } } - + /** * This method returns the local address to which this socket is bound * @@ -263,17 +263,17 @@ public class ServerSocket */ public InetAddress getInetAddress() { - if (!isBound()) + if (! isBound()) return null; - + try { - return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR); + return (InetAddress) impl.getOption(SocketOptions.SO_BINDADDR); } catch (SocketException e) { - // This never happens as we are bound. - return null; + // This never happens as we are bound. + return null; } } @@ -284,30 +284,34 @@ public class ServerSocket */ public int getLocalPort() { - if (!isBound()) + if (! isBound()) return -1; - + return impl.getLocalPort(); } /** * Returns the local socket address * + * @return the local socket address, null if not bound + * * @since 1.4 */ public SocketAddress getLocalSocketAddress() { - if (!isBound()) + if (! isBound()) return null; - + return new InetSocketAddress(getInetAddress(), getLocalPort()); } /** - * Accepts a new connection and returns a connected <code>Socket</code> - * instance representing that connection. This method will block until a + * Accepts a new connection and returns a connected <code>Socket</code> + * instance representing that connection. This method will block until a * connection is available. * + * @return socket object for the just accepted connection + * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkListen method doesn't allow the operation @@ -316,17 +320,17 @@ public class ServerSocket * @exception SocketTimeoutException If a timeout was previously set with * setSoTimeout and the timeout has been reached */ - public Socket accept () throws IOException + public Socket accept() throws IOException { - SecurityManager sm = System.getSecurityManager (); + SecurityManager sm = System.getSecurityManager(); if (sm != null) - sm.checkListen (impl.getLocalPort ()); + sm.checkListen(impl.getLocalPort()); Socket socket = new Socket(); - + try { - implAccept(socket); + implAccept(socket); } catch (IOException e) { @@ -337,15 +341,15 @@ public class ServerSocket catch (IOException e2) { } - + throw e; } - + return socket; } /** - * This protected method is used to help subclasses override + * This protected method is used to help subclasses override * <code>ServerSocket.accept()</code>. The passed in socket will be * connected when this method returns. * @@ -357,12 +361,11 @@ public class ServerSocket * * @since 1.1 */ - protected final void implAccept (Socket socket) - throws IOException + protected final void implAccept(Socket socket) throws IOException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - + // The Sun spec says that if we have an associated channel and // it is in non-blocking mode, we throw an IllegalBlockingModeException. // However, in our implementation if the channel itself initiated this @@ -380,11 +383,11 @@ public class ServerSocket * * @exception IOException If an error occurs */ - public void close () throws IOException + public void close() throws IOException { if (isClosed()) return; - + impl.close(); impl = null; bound = false; @@ -399,6 +402,8 @@ public class ServerSocket * * The socket only has a ServerSocketChannel if its created * by ServerSocketChannel.open. + * + * @return the associated socket channel, null if none exists * * @since 1.4 */ @@ -409,6 +414,8 @@ public class ServerSocket /** * Returns true when the socket is bound, otherwise false + * + * @return true if socket is bound, false otherwise * * @since 1.4 */ @@ -419,6 +426,8 @@ public class ServerSocket /** * Returns true if the socket is closed, otherwise false + * + * @return true if socket is closed, false otherwise * * @since 1.4 */ @@ -429,7 +438,7 @@ public class ServerSocket /** * Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is - * disabled (ie, operations never time out). This is the number of + * disabled (ie, operations never time out). This is the number of * milliseconds a socket operation can block before an * InterruptedIOException is thrown. * @@ -439,11 +448,11 @@ public class ServerSocket * * @since 1.1 */ - public void setSoTimeout (int timeout) throws SocketException + public void setSoTimeout(int timeout) throws SocketException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - + if (timeout < 0) throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); @@ -462,113 +471,113 @@ public class ServerSocket * * @since 1.1 */ - public int getSoTimeout () throws IOException + public int getSoTimeout() throws IOException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - + Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); - if (!(timeout instanceof Integer)) + if (! (timeout instanceof Integer)) throw new IOException("Internal Error"); - return ((Integer)timeout).intValue(); + return ((Integer) timeout).intValue(); } /** * Enables/Disables the SO_REUSEADDR option + * + * @param on true if SO_REUSEADDR should be enabled, false otherwise * * @exception SocketException If an error occurs - * + * * @since 1.4 */ - public void setReuseAddress (boolean on) - throws SocketException + public void setReuseAddress(boolean on) throws SocketException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - - impl.setOption (SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); + + impl.setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on)); } /** * Checks if the SO_REUSEADDR option is enabled - * + * + * @return true if SO_REUSEADDR is set, false otherwise + * * @exception SocketException If an error occurs - * + * * @since 1.4 */ - public boolean getReuseAddress() - throws SocketException + public boolean getReuseAddress() throws SocketException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - - Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); - if (!(reuseaddr instanceof Boolean)) - throw new SocketException ("Internal Error"); - - return ((Boolean) reuseaddr).booleanValue (); + Object reuseaddr = impl.getOption(SocketOptions.SO_REUSEADDR); + + if (! (reuseaddr instanceof Boolean)) + throw new SocketException("Internal Error"); + + return ((Boolean) reuseaddr).booleanValue(); } /** * This method sets the value for the system level socket option * SO_RCVBUF to the specified value. Note that valid values for this * option are specific to a given operating system. - * + * * @param size The new receive buffer size. - * + * * @exception SocketException If an error occurs or Socket is not connected * @exception IllegalArgumentException If size is 0 or negative * * @since 1.4 */ - public void setReceiveBufferSize (int size) - throws SocketException + public void setReceiveBufferSize(int size) throws SocketException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - + if (size <= 0) - throw new IllegalArgumentException ("SO_RCVBUF value must be > 0"); + throw new IllegalArgumentException("SO_RCVBUF value must be > 0"); - impl.setOption (SocketOptions.SO_RCVBUF, new Integer (size)); + impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size)); } /** * This method returns the value of the system level socket option * SO_RCVBUF, which is used by the operating system to tune buffer * sizes for data transfers. - * + * * @return The receive buffer size. - * + * * @exception SocketException If an error occurs or Socket is not connected - * + * * @since 1.4 */ - public int getReceiveBufferSize () - throws SocketException + public int getReceiveBufferSize() throws SocketException { if (isClosed()) throw new SocketException("ServerSocket is closed"); - - Object buf = impl.getOption (SocketOptions.SO_RCVBUF); - if (!(buf instanceof Integer)) - throw new SocketException ("Internal Error: Unexpected type"); - - return ((Integer) buf).intValue (); + Object buf = impl.getOption(SocketOptions.SO_RCVBUF); + + if (! (buf instanceof Integer)) + throw new SocketException("Internal Error: Unexpected type"); + + return ((Integer) buf).intValue(); } /** - * Returns the value of this socket as a <code>String</code>. + * Returns the value of this socket as a <code>String</code>. * * @return This socket represented as a <code>String</code>. */ - public String toString () + public String toString() { - if (!isBound()) + if (! isBound()) return "ServerSocket[unbound]"; return ("ServerSocket[addr=" + getInetAddress() @@ -578,19 +587,21 @@ public class ServerSocket } /** - * Sets the <code>SocketImplFactory</code> for all + * Sets the <code>SocketImplFactory</code> for all * <code>ServerSocket</code>'s. This may only be done * once per virtual machine. Subsequent attempts will generate an * exception. Note that a <code>SecurityManager</code> check is made prior * to setting the factory. If insufficient privileges exist to set the * factory, an exception will be thrown * + * @param fac the factory to set + * * @exception SecurityException If this operation is not allowed by the * <code>SecurityManager</code>. * @exception SocketException If the factory object is already defined * @exception IOException If any other error occurs */ - public static synchronized void setSocketFactory (SocketImplFactory fac) + public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException { factory = fac; |