org.apache.commons.net
public abstract class SocketClient extends Object
See Also: SocketFactory
Field Summary | |
---|---|
static String | NETASCII_EOL
The end of line character sequence used by most IETF protocols. |
protected int | _defaultPort_ The default port the client should connect to. |
protected InputStream | _input_ The socket's InputStream. |
protected boolean | _isConnected_
A status variable indicating if the client's socket is currently open. |
protected OutputStream | _output_ The socket's OutputStream. |
protected SocketFactory | _socketFactory_ The socket's SocketFactory. |
protected Socket | _socket_ The socket used for the connection. |
protected int | _timeout_ The timeout to use after opening a socket. |
Constructor Summary | |
---|---|
SocketClient()
Default constructor for SocketClient. |
Method Summary | |
---|---|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
void | disconnect()
Disconnects the socket connection.
|
int | getDefaultPort()
Returns the current value of the default port (stored in
{@link #_defaultPort_ _defaultPort_ }).
|
int | getDefaultTimeout()
Returns the default timeout in milliseconds that is used when
opening a socket.
|
InetAddress | getLocalAddress()
Returns the local address to which the client's socket is bound.
|
int | getLocalPort()
Returns the port number of the open socket on the local host used
for the connection.
|
InetAddress | getRemoteAddress() |
int | getRemotePort()
Returns the port number of the remote host to which the client is
connected.
|
int | getSoLinger()
Returns the current SO_LINGER timeout of the currently opened socket.
|
int | getSoTimeout()
Returns the timeout in milliseconds of the currently opened socket.
|
boolean | getTcpNoDelay()
Returns true if Nagle's algorithm is enabled on the currently opened
socket.
|
boolean | isConnected()
Returns true if the client is currently connected to a server.
|
void | setDefaultPort(int port)
Sets the default port the SocketClient should connect to when a port
is not specified. |
void | setDefaultTimeout(int timeout)
Set the default timeout in milliseconds to use when opening a socket.
|
void | setSocketFactory(SocketFactory factory)
Sets the SocketFactory used by the SocketClient to open socket
connections. |
void | setSoLinger(boolean on, int val)
Sets the SO_LINGER timeout on the currently opened socket.
|
void | setSoTimeout(int timeout)
Set the timeout in milliseconds of a currently open connection.
|
void | setTcpNoDelay(boolean on)
Enables or disables the Nagle's algorithm (TCP_NODELAY) on the
currently opened socket.
|
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. |
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. |
Parameters: host The remote host. port The port to connect to on the remote host.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
Parameters: hostname The name of the remote host. port The port to connect to on the remote host.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it. UnknownHostException If the hostname cannot be resolved.
Parameters: host The remote host. port The port to connect to on the remote host. localAddr The local address to use. localPort The local port to use.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
Parameters: hostname The name of the remote host. port The port to connect to on the remote host. localAddr The local address to use. localPort The local port to use.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it. UnknownHostException If the hostname cannot be resolved.
Parameters: host The remote host.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it.
Parameters: hostname The name of the remote host.
Throws: SocketException If the socket timeout could not be set. IOException If the socket could not be opened. In most cases you will only want to catch IOException since SocketException is derived from it. UnknownHostException If the hostname cannot be resolved.
Throws: IOException If there is an error closing the socket.
Returns: The current value of the default port.
Returns: The default timeout in milliseconds that is used when opening a socket.
Returns: The local address to which the client's socket is bound.
Returns: The port number of the open socket on the local host used for the connection.
Returns: The remote address to which the client is connected.
Returns: The port number of the remote host to which the client is connected.
Returns: The current SO_LINGER timeout. If SO_LINGER is disabled returns -1.
Throws: SocketException If the operation fails.
Returns: The timeout in milliseconds of the currently opened socket.
Throws: SocketException If the operation fails.
Returns: True if Nagle's algorithm is enabled on the currently opened socket, false otherwise.
Throws: SocketException If the operation fails.
Returns: True if the client is currently connected to a server, false otherwise.
Parameters: port The default port to set.
Parameters: timeout The timeout in milliseconds to use for the socket connection.
Parameters: factory The new SocketFactory the SocketClient should use.
Parameters: on True if linger is to be enabled, false if not. val The linger timeout (in hundredths of a second?)
Throws: SocketException If the operation fails.
Parameters: timeout The timeout in milliseconds to use for the currently open socket connection.
Throws: SocketException If the operation fails.
Parameters: on True if Nagle's algorithm is to be enabled, false if not.
Throws: SocketException If the operation fails.
Returns: True if the remote hosts are the same, false if not.
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.