| java.lang.Object org.apache.commons.net.SocketClient
All known Subclasses: org.apache.commons.net.DaytimeTCPClient, org.apache.commons.net.pop3.POP3, org.apache.commons.net.TimeTCPClient, org.apache.commons.net.smtp.SMTP, org.apache.commons.net.DiscardTCPClient, org.apache.commons.net.bsd.RExecClient, org.apache.commons.net.CharGenTCPClient, org.apache.commons.net.telnet.Telnet, org.apache.commons.net.FingerClient, org.apache.commons.net.nntp.NNTP,
SocketClient | abstract public class SocketClient (Code) | | 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
SocketClient.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
org.apache.commons.net.SocketFactory that
requests browser security capabilities before creating a socket.
All classes derived from SocketClient should use the
SocketClient._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 Also: SocketFactory |
Constructor Summary | |
public | SocketClient() Default constructor for SocketClient. |
Method 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. | public void | connect(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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. Parameters: 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. | public void | connect(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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: hostname - The name of the remote host. Parameters: 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. | public void | connect(InetAddress host, int port, 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. Parameters: port - The port to connect to on the remote host. Parameters: localAddr - The local address to use. Parameters: 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. | public void | connect(String hostname, int port, 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: hostname - The name of the remote host. Parameters: port - The port to connect to on the remote host. Parameters: localAddr - The local address to use. Parameters: 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. | public void | connect(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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. exception: SocketException - If the socket timeout could not be set. exception: IOException - If the socket could not be opened. | public void | connect(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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: 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. | public void | disconnect() Disconnects the socket connection.
You should call this method after you've finished using the class
instance and also before you call
SocketClient.connect connect() again. | public int | getDefaultPort() Returns the current value of the default port (stored in
SocketClient._defaultPort_ _defaultPort_ ). | public int | getDefaultTimeout() Returns the default timeout in milliseconds that is used when
opening a socket. | public InetAddress | getLocalAddress() Returns the local address to which the client's socket is bound. | public int | getLocalPort() Returns the port number of the open socket on the local host used
for the connection. | public InetAddress | getRemoteAddress() | public int | getRemotePort() Returns the port number of the remote host to which the client is
connected. | public int | getSoLinger() Returns the current SO_LINGER timeout of the currently opened socket.
The current SO_LINGER timeout. | public int | getSoTimeout() Returns the timeout in milliseconds of the currently opened socket. | public boolean | getTcpNoDelay() Returns true if Nagle's algorithm is enabled on the currently opened
socket. | public boolean | isConnected() Returns true if the client is currently connected to a server. | public void | setDefaultPort(int port) Sets the default port the SocketClient should connect to when a port
is not specified. | public void | setDefaultTimeout(int timeout) Set the default timeout in milliseconds to use when opening a socket.
This value is only used previous to a call to
SocketClient.connect connect() and should not be confused with
SocketClient.setSoTimeout setSoTimeout() which operates on an the currently opened socket. | public void | setSoLinger(boolean on, int val) Sets the SO_LINGER timeout on the currently opened socket. | public void | setSoTimeout(int timeout) Set the timeout in milliseconds of a currently open connection. | public void | setSocketFactory(SocketFactory factory) Sets the SocketFactory used by the SocketClient to open socket
connections. | public void | setTcpNoDelay(boolean on) Enables or disables the Nagle's algorithm (TCP_NODELAY) on the
currently opened socket. | public boolean | verifyRemote(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. |
NETASCII_EOL | final public static String NETASCII_EOL(Code) | | The end of line character sequence used by most IETF protocols. That
is a carriage return followed by a newline: "\r\n"
|
_defaultPort_ | protected int _defaultPort_(Code) | | The default port the client should connect to.
|
_isConnected_ | protected boolean _isConnected_(Code) | | A status variable indicating if the client's socket is currently open.
|
_socket_ | protected Socket _socket_(Code) | | The socket used for the connection.
|
_timeout_ | protected int _timeout_(Code) | | The timeout to use after opening a socket.
|
SocketClient | public SocketClient()(Code) | | Default constructor for SocketClient. Initializes
_socket_ to null, _timeout_ to 0, _defaultPort to 0,
_isConnected_ to false, and _socketFactory_ to a shared instance of
org.apache.commons.net.DefaultSocketFactory .
|
_connectAction_ | protected void _connectAction_() throws IOException(Code) | | 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
SocketClient.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.
|
connect | public void connect(InetAddress host, int port) throws SocketException, IOException(Code) | | 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. Parameters: 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 mostcases you will only want to catch IOException since SocketException isderived from it. |
connect | public void connect(String hostname, int port) throws SocketException, IOException(Code) | | 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: hostname - The name of the remote host. Parameters: 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 mostcases you will only want to catch IOException since SocketException isderived from it. exception: UnknownHostException - If the hostname cannot be resolved. |
connect | public void connect(InetAddress host, int port, InetAddress localAddr, int localPort) throws SocketException, IOException(Code) | | Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. Parameters: port - The port to connect to on the remote host. Parameters: localAddr - The local address to use. Parameters: 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 mostcases you will only want to catch IOException since SocketException isderived from it. |
connect | public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws SocketException, IOException(Code) | | Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: hostname - The name of the remote host. Parameters: port - The port to connect to on the remote host. Parameters: localAddr - The local address to use. Parameters: 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 mostcases you will only want to catch IOException since SocketException isderived from it. exception: UnknownHostException - If the hostname cannot be resolved. |
connect | public void connect(InetAddress host) throws SocketException, IOException(Code) | | 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: host - The remote host. exception: SocketException - If the socket timeout could not be set. exception: IOException - If the socket could not be opened. In mostcases you will only want to catch IOException since SocketException isderived from it. |
connect | public void connect(String hostname) throws SocketException, IOException(Code) | | 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,
SocketClient._connectAction_ _connectAction_() is called to perform connection initialization actions.
Parameters: 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 mostcases you will only want to catch IOException since SocketException isderived from it. exception: UnknownHostException - If the hostname cannot be resolved. |
disconnect | public void disconnect() throws IOException(Code) | | Disconnects the socket connection.
You should call this method after you've finished using the class
instance and also before you call
SocketClient.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. |
getDefaultPort | public int getDefaultPort()(Code) | | Returns the current value of the default port (stored in
SocketClient._defaultPort_ _defaultPort_ ).
The current value of the default port. |
getDefaultTimeout | public int getDefaultTimeout()(Code) | | Returns the default timeout in milliseconds that is used when
opening a socket.
The default timeout in milliseconds that is used whenopening a socket. |
getLocalAddress | public InetAddress getLocalAddress()(Code) | | Returns the local address to which the client's socket is bound.
The local address to which the client's socket is bound. |
getLocalPort | public int getLocalPort()(Code) | | Returns the port number of the open socket on the local host used
for the connection.
The port number of the open socket on the local host usedfor the connection. |
getRemoteAddress | public InetAddress getRemoteAddress()(Code) | | The remote address to which the client is connected. |
getRemotePort | public int getRemotePort()(Code) | | Returns the port number of the remote host to which the client is
connected.
The port number of the remote host to which the client isconnected. |
getSoLinger | public int getSoLinger() throws SocketException(Code) | | Returns the current SO_LINGER timeout of the currently opened socket.
The current SO_LINGER timeout. If SO_LINGER is disabled returns-1. exception: SocketException - If the operation fails. |
getSoTimeout | public int getSoTimeout() throws SocketException(Code) | | Returns the timeout in milliseconds of the currently opened socket.
The timeout in milliseconds of the currently opened socket. exception: SocketException - If the operation fails. |
getTcpNoDelay | public boolean getTcpNoDelay() throws SocketException(Code) | | Returns true if Nagle's algorithm is enabled on the currently opened
socket.
True if Nagle's algorithm is enabled on the currently openedsocket, false otherwise. exception: SocketException - If the operation fails. |
isConnected | public boolean isConnected()(Code) | | Returns true if the client is currently connected to a server.
True if the client is currently connected to a server,false otherwise. |
setDefaultPort | public void setDefaultPort(int port)(Code) | | Sets the default port the SocketClient should connect to when a port
is not specified. The
SocketClient._defaultPort_ _defaultPort_ variable stores this value. If never set, the default port is equal
to zero.
Parameters: port - The default port to set. |
setDefaultTimeout | public void setDefaultTimeout(int timeout)(Code) | | Set the default timeout in milliseconds to use when opening a socket.
This value is only used previous to a call to
SocketClient.connect connect() and should not be confused with
SocketClient.setSoTimeout setSoTimeout() which operates on an the currently opened socket. _timeout_ contains
the new timeout value.
Parameters: timeout - The timeout in milliseconds to use for the socketconnection. |
setSoLinger | public void setSoLinger(boolean on, int val) throws SocketException(Code) | | Sets the SO_LINGER timeout on the currently opened socket.
Parameters: on - True if linger is to be enabled, false if not. Parameters: val - The linger timeout (in hundredths of a second?) exception: SocketException - If the operation fails. |
setSoTimeout | public void setSoTimeout(int timeout) throws SocketException(Code) | | Set the timeout in milliseconds of a currently open connection.
Only call this method after a connection has been opened
by
SocketClient.connect connect() .
Parameters: timeout - The timeout in milliseconds to use for the currentlyopen socket connection. exception: SocketException - If the operation fails. |
setSocketFactory | public void setSocketFactory(SocketFactory factory)(Code) | | 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).
Parameters: factory - The new SocketFactory the SocketClient should use. |
setTcpNoDelay | public void setTcpNoDelay(boolean on) throws SocketException(Code) | | Enables or disables the Nagle's algorithm (TCP_NODELAY) on the
currently opened socket.
Parameters: on - True if Nagle's algorithm is to be enabled, false if not. exception: SocketException - If the operation fails. |
verifyRemote | public boolean verifyRemote(Socket socket)(Code) | | 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.
True if the remote hosts are the same, false if not. |
|
|