public abstract class SocketClient
extends java.lang.Object
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
SocketFactory
that
requests browser security capabilities before creating a socket.
All classes derived from SocketClient should use the
_socketFactory_
member variable to
create Socket and ServerSocket instances rather than instantiating
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.SocketFactory
限定符和类型 | 字段和说明 |
---|---|
protected int |
_defaultPort_
The default port the client should connect to.
|
protected java.lang.String |
_hostname_
The hostname used for the connection (null = no hostname supplied).
|
protected java.io.InputStream |
_input_
The socket's InputStream.
|
protected java.io.OutputStream |
_output_
The socket's OutputStream.
|
protected javax.net.ServerSocketFactory |
_serverSocketFactory_
The socket's ServerSocket Factory.
|
protected java.net.Socket |
_socket_
The socket used for the connection.
|
protected javax.net.SocketFactory |
_socketFactory_
The socket's SocketFactory.
|
protected int |
_timeout_
The timeout to use after opening a socket.
|
protected int |
connectTimeout |
static java.lang.String |
NETASCII_EOL
The end of line character sequence used by most IETF protocols.
|
构造器和说明 |
---|
SocketClient()
Default constructor for SocketClient.
|
限定符和类型 | 方法和说明 |
---|---|
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.
|
void |
addProtocolCommandListener(ProtocolCommandListener listener)
Adds a ProtocolCommandListener.
|
void |
connect(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.
|
void |
connect(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.
|
void |
connect(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.
|
void |
connect(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.
|
void |
connect(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.
|
void |
connect(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.
|
protected void |
createCommandSupport()
Create the CommandSupport instance if required
|
void |
disconnect()
Disconnects the socket connection.
|
protected void |
fireCommandSent(java.lang.String command,
java.lang.String message)
If there are any listeners, send them the command details.
|
protected void |
fireReplyReceived(int replyCode,
java.lang.String reply)
If there are any listeners, send them the reply details.
|
java.nio.charset.Charset |
getCharset()
Gets the charset.
|
protected ProtocolCommandSupport |
getCommandSupport()
Subclasses can override this if they need to provide their own
instance field for backwards compatibilty.
|
int |
getConnectTimeout()
Get the underlying socket connection timeout.
|
int |
getDefaultPort()
Returns the current value of the default port (stored in
_defaultPort_ ). |
int |
getDefaultTimeout()
Returns the default timeout in milliseconds that is used when
opening a socket.
|
boolean |
getKeepAlive()
Returns the current value of the SO_KEEPALIVE flag on the currently opened socket.
|
java.net.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.
|
java.net.Proxy |
getProxy()
Gets the proxy for use with all the connections.
|
protected int |
getReceiveBufferSize()
Get the current receivedBuffer size
|
java.net.InetAddress |
getRemoteAddress() |
int |
getRemotePort()
Returns the port number of the remote host to which the client is
connected.
|
protected int |
getSendBufferSize()
Get the current sendBuffer size
|
javax.net.ServerSocketFactory |
getServerSocketFactory()
Get the underlying
ServerSocketFactory |
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 |
isAvailable()
Make various checks on the socket to test if it is available for use.
|
boolean |
isConnected()
Returns true if the client is currently connected to a server.
|
void |
removeProtocolCommandListener(ProtocolCommandListener listener)
Removes a ProtocolCommandListener.
|
void |
setCharset(java.nio.charset.Charset charset)
Sets the charset.
|
void |
setConnectTimeout(int connectTimeout)
Sets the connection timeout in milliseconds, which will be passed to the
Socket object's
connect() method. |
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 |
setKeepAlive(boolean keepAlive)
Sets the SO_KEEPALIVE flag on the currently opened socket.
|
void |
setProxy(java.net.Proxy proxy)
Sets the proxy for use with all the connections.
|
void |
setReceiveBufferSize(int size)
Sets the underlying socket receive buffer size.
|
void |
setSendBufferSize(int size)
Set the underlying socket send buffer size.
|
void |
setServerSocketFactory(javax.net.ServerSocketFactory factory)
Sets the ServerSocketFactory used by the SocketClient to open ServerSocket
connections.
|
void |
setSocketFactory(javax.net.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(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.
|
public static final java.lang.String NETASCII_EOL
protected int _timeout_
protected java.net.Socket _socket_
protected java.lang.String _hostname_
protected int _defaultPort_
protected java.io.InputStream _input_
protected java.io.OutputStream _output_
protected javax.net.SocketFactory _socketFactory_
protected javax.net.ServerSocketFactory _serverSocketFactory_
protected int connectTimeout
public SocketClient()
Charset.defaultCharset()
and _socketFactory_ to a shared instance of
DefaultSocketFactory
.protected void _connectAction_() throws java.io.IOException
This method sets the timeout on the just opened socket to the default
timeout set by 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.
java.io.IOException
- (SocketException) if a problem occurs with the socketpublic void connect(java.net.InetAddress host, int port) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
host
- The remote host.port
- The port to connect to on the remote host.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.public void connect(java.lang.String hostname, int port) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
hostname
- The name of the remote host.port
- The port to connect to on the remote host.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.java.net.UnknownHostException
- If the hostname cannot be resolved.public void connect(java.net.InetAddress host, int port, java.net.InetAddress localAddr, int localPort) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
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.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.public void connect(java.lang.String hostname, int port, java.net.InetAddress localAddr, int localPort) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
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.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.java.net.UnknownHostException
- If the hostname cannot be resolved.public void connect(java.net.InetAddress host) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
host
- The remote host.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.public void connect(java.lang.String hostname) throws java.net.SocketException, java.io.IOException
_connectAction_()
is called to perform connection initialization actions.
hostname
- The name of the remote host.java.net.SocketException
- If the socket timeout could not be set.java.io.IOException
- If the socket could not be opened. In most
cases you will only want to catch IOException since SocketException is
derived from it.java.net.UnknownHostException
- If the hostname cannot be resolved.public void disconnect() throws java.io.IOException
connect()
again. _isConnected_ is set to false, _socket_ is set to null,
_input_ is set to null, and _output_ is set to null.
java.io.IOException
- If there is an error closing the socket.public boolean isConnected()
Delegates to Socket.isConnected()
public boolean isAvailable()
true
if the socket appears to be available for usepublic void setDefaultPort(int port)
_defaultPort_
variable stores this value. If never set, the default port is equal
to zero.
port
- The default port to set.public int getDefaultPort()
_defaultPort_
).
public void setDefaultTimeout(int timeout)
connect()
and should not be confused with setSoTimeout()
which operates on an the currently opened socket. _timeout_ contains
the new timeout value.
timeout
- The timeout in milliseconds to use for the socket
connection.public int getDefaultTimeout()
public void setSoTimeout(int timeout) throws java.net.SocketException
connect()
.
To set the initial timeout, use setDefaultTimeout(int)
instead.
timeout
- The timeout in milliseconds to use for the currently
open socket connection.java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic void setSendBufferSize(int size) throws java.net.SocketException
size
- The size of the buffer in bytes.java.net.SocketException
- never thrown, but subclasses might want to do soprotected int getSendBufferSize()
public void setReceiveBufferSize(int size) throws java.net.SocketException
size
- The size of the buffer in bytes.java.net.SocketException
- never (but subclasses may wish to do so)protected int getReceiveBufferSize()
public int getSoTimeout() throws java.net.SocketException
java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic void setTcpNoDelay(boolean on) throws java.net.SocketException
on
- True if Nagle's algorithm is to be enabled, false if not.java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic boolean getTcpNoDelay() throws java.net.SocketException
java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic void setKeepAlive(boolean keepAlive) throws java.net.SocketException
keepAlive
- If true, keepAlive is turned onjava.net.SocketException
- if there is a problem with the socketjava.lang.NullPointerException
- if the socket is not currently openpublic boolean getKeepAlive() throws java.net.SocketException
Socket.getKeepAlive()
java.net.SocketException
- if there is a problem with the socketjava.lang.NullPointerException
- if the socket is not currently openpublic void setSoLinger(boolean on, int val) throws java.net.SocketException
on
- True if linger is to be enabled, false if not.val
- The linger timeout (in hundredths of a second?)java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic int getSoLinger() throws java.net.SocketException
java.net.SocketException
- If the operation fails.java.lang.NullPointerException
- if the socket is not currently openpublic int getLocalPort()
Socket.getLocalPort()
java.lang.NullPointerException
- if the socket is not currently openpublic java.net.InetAddress getLocalAddress()
Socket.getLocalAddress()
java.lang.NullPointerException
- if the socket is not currently openpublic int getRemotePort()
Socket.getPort()
java.lang.NullPointerException
- if the socket is not currently openpublic java.net.InetAddress getRemoteAddress()
Socket.getInetAddress()
java.lang.NullPointerException
- if the socket is not currently openpublic boolean verifyRemote(java.net.Socket socket)
socket
- the item to check againstpublic void setSocketFactory(javax.net.SocketFactory factory)
factory
- The new SocketFactory the SocketClient should use.public void setServerSocketFactory(javax.net.ServerSocketFactory factory)
factory
- The new ServerSocketFactory the SocketClient should use.public void setConnectTimeout(int connectTimeout)
Socket
object's
connect() method.connectTimeout
- The connection timeout to use (in ms)public int getConnectTimeout()
public javax.net.ServerSocketFactory getServerSocketFactory()
ServerSocketFactory
public void addProtocolCommandListener(ProtocolCommandListener listener)
listener
- The ProtocolCommandListener to add.public void removeProtocolCommandListener(ProtocolCommandListener listener)
listener
- The ProtocolCommandListener to remove.protected void fireReplyReceived(int replyCode, java.lang.String reply)
replyCode
- the code extracted from the replyreply
- the full reply textprotected void fireCommandSent(java.lang.String command, java.lang.String message)
command
- the command namemessage
- the complete message, including command nameprotected void createCommandSupport()
protected ProtocolCommandSupport getCommandSupport()
null
public void setProxy(java.net.Proxy proxy)
proxy
- the new proxy for connections.public java.net.Proxy getProxy()
public java.nio.charset.Charset getCharset()
public void setCharset(java.nio.charset.Charset charset)
charset
- the charset.