jhomenet.commons.net.secure
Class ServerSocketMonitor

java.lang.Object
  extended by java.net.ServerSocket
      extended by jhomenet.commons.net.secure.ServerSocketMonitor

public class ServerSocketMonitor
extends java.net.ServerSocket

Wraps an SSLServerSocket instance to intercept accept() invocations and wrap the result in a SocketMonitor object.

Version:
$Revision: 1.1 $ ; $Date: 2001/12/10 20:43:28 $
Author:
Alexander V. Konstantinou (akonstan@acm.org)
See Also:
ServerSocket, SSLServerSocket

Field Summary
protected  javax.net.ssl.SSLServerSocket server
          The wrapped socket
 
Constructor Summary
ServerSocketMonitor(javax.net.ssl.SSLServerSocket server)
          Construct a new SSLServerSocket wrapper over the real SSLServerSocket instance.
 
Method Summary
 java.net.Socket accept()
          Listens for a connection to be made to this socket and accepts it.
 void bind(java.net.SocketAddress endpoint)
          Binds the ServerSocket to a specific address (IP address and port number).
 void bind(java.net.SocketAddress endpoint, int backlog)
          Binds the ServerSocket to a specific address (IP address and port number).
 void close()
          Closes this socket.
 java.nio.channels.ServerSocketChannel getChannel()
          Returns the unique ServerSocketChannel object associated with this socket, if any.
 java.net.InetAddress getInetAddress()
          Returns the local address of this server socket.
 int getLocalPort()
          Returns the port on which this socket is listening.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
 int getReceiveBufferSize()
          Gets the value of the SO_RCVBUF option for this ServerSocket, that is the proposed buffer size that will be used for Sockets accepted from this ServerSocket.
 boolean getReuseAddress()
          Tests if SO_REUSEADDR is enabled.
 int getSoTimeout()
          Retrive setting for SO_TIMEOUT.
 boolean isBound()
          Returns the binding state of the ServerSocket.
 boolean isClosed()
          Returns the closed state of the ServerSocket.
 void setReceiveBufferSize(int size)
          Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.
 void setReuseAddress(boolean on)
          Enable/disable SO_REUSEADDR.
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 java.lang.String toString()
          Returns the implementation address and implementation port of this socket as a String.
 
Methods inherited from class java.net.ServerSocket
implAccept, setPerformancePreferences, setSocketFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

server

protected final javax.net.ssl.SSLServerSocket server
The wrapped socket

Constructor Detail

ServerSocketMonitor

public ServerSocketMonitor(javax.net.ssl.SSLServerSocket server)
                    throws java.io.IOException
Construct a new SSLServerSocket wrapper over the real SSLServerSocket instance.

The ServerSocket object did not provide a public no-arg constructor prior to JDK 1.4. As a result, this constructor will invoke the super(0) to create a server socket and will then close it right away. This may interfere with a security policy restricting listen permission to certain ports.

This class may be recompiled without the first two lines if it is only going to be used in JDK > 1.4

Throws:
java.io.IOException
Method Detail

bind

public void bind(java.net.SocketAddress endpoint)
          throws java.io.IOException
Binds the ServerSocket to a specific address (IP address and port number).

If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

Overrides:
bind in class java.net.ServerSocket
Parameters:
endpoint - The IP address & port number to bind to.
Throws:
java.io.IOException - if the bind operation fails, or if the socket is already bound.
java.lang.SecurityException - if a SecurityManager is present and its checkListen method doesn't allow the operation.
java.lang.IllegalArgumentException - if endpoint is a SocketAddress subclass not supported by this socket
Since:
1.4

bind

public void bind(java.net.SocketAddress endpoint,
                 int backlog)
          throws java.io.IOException
Binds the ServerSocket to a specific address (IP address and port number).

If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.

Overrides:
bind in class java.net.ServerSocket
Parameters:
endpoint - The IP address & port number to bind to.
backlog - The listen backlog length.
Throws:
java.io.IOException - if the bind operation fails, or if the socket is already bound.
java.lang.SecurityException - if a SecurityManager is present and its checkListen method doesn't allow the operation.
java.lang.IllegalArgumentException - if endpoint is a SocketAddress subclass not supported by this socket
Since:
1.4

getInetAddress

public java.net.InetAddress getInetAddress()
Returns the local address of this server socket.

Overrides:
getInetAddress in class java.net.ServerSocket
Returns:
the address to which this socket is connected, or null if the socket is not yet connected.

getLocalPort

public int getLocalPort()
Returns the port on which this socket is listening.

Overrides:
getLocalPort in class java.net.ServerSocket
Returns:
the port number to which this socket is listening.

getLocalSocketAddress

public java.net.SocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.

Overrides:
getLocalSocketAddress in class java.net.ServerSocket
Returns:
a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet.
Since:
1.4
See Also:
getInetAddress(), getLocalPort(), bind(SocketAddress)

accept

public java.net.Socket accept()
                       throws java.io.IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

Overrides:
accept in class java.net.ServerSocket
Returns:
the new Socket
Throws:
java.io.IOException - if an I/O error occurs when waiting for a connection.
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkAccept(java.lang.String, int)

close

public void close()
           throws java.io.IOException
Closes this socket.

Overrides:
close in class java.net.ServerSocket
Throws:
java.io.IOException - if an I/O error occurs when closing the socket.

getChannel

public java.nio.channels.ServerSocketChannel getChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.

A server socket will have a channel if, and only if, the channel itself was created via the ServerSocketChannel.open method.

Overrides:
getChannel in class java.net.ServerSocket
Returns:
the server-socket channel associated with this socket, or null if this socket was not created for a channel
Since:
1.4

isBound

public boolean isBound()
Returns the binding state of the ServerSocket.

Overrides:
isBound in class java.net.ServerSocket
Returns:
true if the ServerSocket succesfuly bound to an address
Since:
1.4

isClosed

public boolean isClosed()
Returns the closed state of the ServerSocket.

Overrides:
isClosed in class java.net.ServerSocket
Returns:
true if the socket has been closed
Since:
1.4

setSoTimeout

public void setSoTimeout(int timeout)
                  throws java.net.SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Overrides:
setSoTimeout in class java.net.ServerSocket
Parameters:
timeout - the specified timeout, in milliseconds
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
getSoTimeout()

getSoTimeout

public int getSoTimeout()
                 throws java.io.IOException
Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Overrides:
getSoTimeout in class java.net.ServerSocket
Returns:
the SO_TIMEOUT value
Throws:
java.io.IOException - if an I/O error occurs
Since:
JDK1.1
See Also:
setSoTimeout(int)

setReuseAddress

public void setReuseAddress(boolean on)
                     throws java.net.SocketException
Enable/disable SO_REUSEADDR.

The SO_REUSEADDR socket option affects the bind() operation and serves 2 main different purposes:

Overrides:
setReuseAddress in class java.net.ServerSocket
Parameters:
on - whether or not to have socket ReuseAddr turned on.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
1.4
See Also:
getReuseAddress()

getReuseAddress

public boolean getReuseAddress()
                        throws java.net.SocketException
Tests if SO_REUSEADDR is enabled.

Overrides:
getReuseAddress in class java.net.ServerSocket
Returns:
a boolean indicating whether or not SO_REUSEADDR is enabled.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
1.4
See Also:
setReuseAddress(boolean)

toString

public java.lang.String toString()
Returns the implementation address and implementation port of this socket as a String.

Overrides:
toString in class java.net.ServerSocket
Returns:
a string representation of this socket.

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws java.net.SocketException
Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket. The value actually set in the accepted socket must be determined by calling Socket.getReceiveBufferSize() after the socket is returned by accept().

The value of SO_RCVBUF is used both to set the size of the internal socket receive buffer, and to set the size of the TCP receive window that is advertized to the remote peer.

It is possible to change the value subsequently, by calling Socket.setReceiveBufferSize(int). However, if the application wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 then the proposed value must be set in the ServerSocket before it is bound to a local address. This implies, that the ServerSocket must be created with the no-argument constructor, then setReceiveBufferSize() must be called and lastly the ServerSocket is bound to an address by calling bind().

Failure to do this will not cause an error, and the buffer size may be set to the requested value but the TCP receive window in sockets accepted from this ServerSocket will be no larger than 64K bytes.

Overrides:
setReceiveBufferSize in class java.net.ServerSocket
Parameters:
size - the size to which to set the receive buffer size. This value must be greater than 0.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
java.lang.IllegalArgumentException - if the value is 0 or is negative.
Since:
1.4
See Also:
getReceiveBufferSize()

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws java.net.SocketException
Gets the value of the SO_RCVBUF option for this ServerSocket, that is the proposed buffer size that will be used for Sockets accepted from this ServerSocket.

Note, the value actually set in the accepted socket is determined by calling Socket.getReceiveBufferSize().

Overrides:
getReceiveBufferSize in class java.net.ServerSocket
Returns:
the value of the SO_RCVBUF option for this Socket.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
1.4
See Also:
setReceiveBufferSize(int)


Copyright © 2004-2007 David Irwin. All Rights Reserved.