FileDocCategorySizeDatePackage
SocketClient.javaAPI DocApache Commons NET 1.4.1 API17287Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net

SocketClient

public abstract class SocketClient extends Object
The SocketClient provides the basic operations that are required of client objects accessing sockets. It is meant to be subclassed to avoid having to rewrite the same code over and over again to open a socket, close a socket, set timeouts, etc. Of special note is the {@link #setSocketFactory setSocketFactory } method, which allows you to control the type of Socket the SocketClient creates for initiating network connections. This is especially useful for adding SSL or proxy support as well as better support for applets. For example, you could create a {@link org.apache.commons.net.SocketFactory} that requests browser security capabilities before creating a socket. All classes derived from SocketClient should use the {@link #_socketFactory_ _socketFactory_ } member variable to create Socket and ServerSocket instances rather than instanting them by directly invoking a constructor. By honoring this contract you guarantee that a user will always be able to provide his own Socket implementations by substituting his own SocketFactory.
author
Daniel F. Savarese
see
SocketFactory

Fields Summary
public static final String
NETASCII_EOL
The end of line character sequence used by most IETF protocols. That is a carriage return followed by a newline: "\r\n"
private static final SocketFactory
__DEFAULT_SOCKET_FACTORY
The default SocketFactory shared by all SocketClient instances.
protected int
_timeout_
The timeout to use after opening a socket.
protected Socket
_socket_
The socket used for the connection.
protected boolean
_isConnected_
A status variable indicating if the client's socket is currently open.
protected int
_defaultPort_
The default port the client should connect to.
protected InputStream
_input_
The socket's InputStream.
protected OutputStream
_output_
The socket's OutputStream.
protected SocketFactory
_socketFactory_
The socket's SocketFactory.
Constructors Summary
public SocketClient()
Default constructor for SocketClient. Initializes _socket_ to null, _timeout_ to 0, _defaultPort to 0, _isConnected_ to false, and _socketFactory_ to a shared instance of {@link org.apache.commons.net.DefaultSocketFactory}.



                                    
     
    
        _socket_ = null;
        _input_ = null;
        _output_ = null;
        _timeout_ = 0;
        _defaultPort_ = 0;
        _isConnected_ = false;
        _socketFactory_ = __DEFAULT_SOCKET_FACTORY;
    
Methods Summary
protected void_connectAction_()
Because there are so many connect() methods, the _connectAction_() method is provided as a means of performing some action immediately after establishing a connection, rather than reimplementing all of the connect() methods. The last action performed by every connect() method after opening a socket is to call this method.

This method sets the timeout on the just opened socket to the default timeout set by {@link #setDefaultTimeout setDefaultTimeout() }, sets _input_ and _output_ to the socket's InputStream and OutputStream respectively, and sets _isConnected_ to true.

Subclasses overriding this method should start by calling super._connectAction_() first to ensure the initialization of the aforementioned protected variables.

        _socket_.setSoTimeout(_timeout_);
        _input_ = _socket_.getInputStream();
        _output_ = _socket_.getOutputStream();
        _isConnected_ = true;
    
public voidconnect(java.net.InetAddress host, int port)
Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
host The remote host.
param
port The port to connect to on the remote host.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

        _socket_ = _socketFactory_.createSocket(host, port);
        _connectAction_();
    
public voidconnect(java.lang.String hostname, int port)
Opens a Socket connected to a remote host at the specified port and originating from the current host at a system assigned port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
hostname The name of the remote host.
param
port The port to connect to on the remote host.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
exception
UnknownHostException If the hostname cannot be resolved.

        _socket_ = _socketFactory_.createSocket(hostname, port);
        _connectAction_();
    
public voidconnect(java.net.InetAddress host, int port, java.net.InetAddress localAddr, int localPort)
Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
host The remote host.
param
port The port to connect to on the remote host.
param
localAddr The local address to use.
param
localPort The local port to use.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

        _socket_ = _socketFactory_.createSocket(host, port, localAddr, localPort);
        _connectAction_();
    
public voidconnect(java.lang.String hostname, int port, java.net.InetAddress localAddr, int localPort)
Opens a Socket connected to a remote host at the specified port and originating from the specified local address and port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
hostname The name of the remote host.
param
port The port to connect to on the remote host.
param
localAddr The local address to use.
param
localPort The local port to use.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
exception
UnknownHostException If the hostname cannot be resolved.

        _socket_ =
            _socketFactory_.createSocket(hostname, port, localAddr, localPort);
        _connectAction_();
    
public voidconnect(java.net.InetAddress host)
Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
host The remote host.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.

        connect(host, _defaultPort_);
    
public voidconnect(java.lang.String hostname)
Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port. Before returning, {@link #_connectAction_ _connectAction_() } is called to perform connection initialization actions.

param
hostname The name of the remote host.
exception
SocketException If the socket timeout could not be set.
exception
IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
exception
UnknownHostException If the hostname cannot be resolved.

        connect(hostname, _defaultPort_);
    
public voiddisconnect()
Disconnects the socket connection. You should call this method after you've finished using the class instance and also before you call {@link #connect connect() } again. _isConnected_ is set to false, _socket_ is set to null, _input_ is set to null, and _output_ is set to null.

exception
IOException If there is an error closing the socket.

        _socket_.close();
        _input_.close();
        _output_.close();
        _socket_ = null;
        _input_ = null;
        _output_ = null;
        _isConnected_ = false;
    
public intgetDefaultPort()
Returns the current value of the default port (stored in {@link #_defaultPort_ _defaultPort_ }).

return
The current value of the default port.

        return _defaultPort_;
    
public intgetDefaultTimeout()
Returns the default timeout in milliseconds that is used when opening a socket.

return
The default timeout in milliseconds that is used when opening a socket.

        return _timeout_;
    
public java.net.InetAddressgetLocalAddress()
Returns the local address to which the client's socket is bound.

return
The local address to which the client's socket is bound.

        return _socket_.getLocalAddress();
    
public intgetLocalPort()
Returns the port number of the open socket on the local host used for the connection.

return
The port number of the open socket on the local host used for the connection.

        return _socket_.getLocalPort();
    
public java.net.InetAddressgetRemoteAddress()

return
The remote address to which the client is connected.

        return _socket_.getInetAddress();
    
public intgetRemotePort()
Returns the port number of the remote host to which the client is connected.

return
The port number of the remote host to which the client is connected.

        return _socket_.getPort();
    
public intgetSoLinger()
Returns the current SO_LINGER timeout of the currently opened socket.

return
The current SO_LINGER timeout. If SO_LINGER is disabled returns -1.
exception
SocketException If the operation fails.

        return _socket_.getSoLinger();
    
public intgetSoTimeout()
Returns the timeout in milliseconds of the currently opened socket.

return
The timeout in milliseconds of the currently opened socket.
exception
SocketException If the operation fails.

        return _socket_.getSoTimeout();
    
public booleangetTcpNoDelay()
Returns true if Nagle's algorithm is enabled on the currently opened socket.

return
True if Nagle's algorithm is enabled on the currently opened socket, false otherwise.
exception
SocketException If the operation fails.

        return _socket_.getTcpNoDelay();
    
public booleanisConnected()
Returns true if the client is currently connected to a server.

return
True if the client is currently connected to a server, false otherwise.

        return _isConnected_;
    
public voidsetDefaultPort(int port)
Sets the default port the SocketClient should connect to when a port is not specified. The {@link #_defaultPort_ _defaultPort_ } variable stores this value. If never set, the default port is equal to zero.

param
port The default port to set.

        _defaultPort_ = port;
    
public voidsetDefaultTimeout(int timeout)
Set the default timeout in milliseconds to use when opening a socket. This value is only used previous to a call to {@link #connect connect()} and should not be confused with {@link #setSoTimeout setSoTimeout()} which operates on an the currently opened socket. _timeout_ contains the new timeout value.

param
timeout The timeout in milliseconds to use for the socket connection.

        _timeout_ = timeout;
    
public voidsetSoLinger(boolean on, int val)
Sets the SO_LINGER timeout on the currently opened socket.

param
on True if linger is to be enabled, false if not.
param
val The linger timeout (in hundredths of a second?)
exception
SocketException If the operation fails.

        _socket_.setSoLinger(on, val);
    
public voidsetSoTimeout(int timeout)
Set the timeout in milliseconds of a currently open connection. Only call this method after a connection has been opened by {@link #connect connect()}.

param
timeout The timeout in milliseconds to use for the currently open socket connection.
exception
SocketException If the operation fails.

        _socket_.setSoTimeout(timeout);
    
public voidsetSocketFactory(SocketFactory factory)
Sets the SocketFactory used by the SocketClient to open socket connections. If the factory value is null, then a default factory is used (only do this to reset the factory after having previously altered it).

param
factory The new SocketFactory the SocketClient should use.

        if (factory == null)
            _socketFactory_ = __DEFAULT_SOCKET_FACTORY;
        else
            _socketFactory_ = factory;
    
public voidsetTcpNoDelay(boolean on)
Enables or disables the Nagle's algorithm (TCP_NODELAY) on the currently opened socket.

param
on True if Nagle's algorithm is to be enabled, false if not.
exception
SocketException If the operation fails.

        _socket_.setTcpNoDelay(on);
    
public booleanverifyRemote(java.net.Socket socket)
Verifies that the remote end of the given socket is connected to the the same host that the SocketClient is currently connected to. This is useful for doing a quick security check when a client needs to accept a connection from a server, such as an FTP data connection or a BSD R command standard error stream.

return
True if the remote hosts are the same, false if not.

        InetAddress host1, host2;

        host1 = socket.getInetAddress();
        host2 = getRemoteAddress();

        return host1.equals(host2);