|
|||||||||
| Home >> All >> org >> apache >> commons >> [ net overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
org.apache.commons.net
Class SocketClient

java.lang.Objectorg.apache.commons.net.SocketClient
- Direct Known Subclasses:
- CharGenTCPClient, DaytimeTCPClient, DiscardTCPClient, FingerClient, TimeTCPClient
- public abstract class SocketClient
- extends java.lang.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 setSocketFactory 55 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_ 55 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.
| Field Summary | |
private static SocketFactory |
__DEFAULT_SOCKET_FACTORY
The default SocketFactory shared by all SocketClient instances. |
protected int |
_defaultPort_
The default port the client should connect to. |
protected java.io.InputStream |
_input_
The socket's InputStream. |
protected boolean |
_isConnected_
A status variable indicating if the client's socket is currently open. |
protected java.io.OutputStream |
_output_
The socket's OutputStream. |
protected java.net.Socket |
_socket_
The socket used for the connection. |
protected SocketFactory |
_socketFactory_
The socket's SocketFactory. |
protected int |
_timeout_
The timeout to use after opening a socket. |
static java.lang.String |
NETASCII_EOL
The end of line character sequence used by most IETF protocols. |
| Constructor Summary | |
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. |
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. |
void |
disconnect()
Disconnects the socket connection. |
int |
getDefaultPort()
Returns the current value of the default port (stored in _defaultPort_ 55 ). |
int |
getDefaultTimeout()
Returns the default timeout in milliseconds that is used when opening a 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.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(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. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
NETASCII_EOL
public static final java.lang.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"
- See Also:
- Constant Field Values
__DEFAULT_SOCKET_FACTORY
private static final SocketFactory __DEFAULT_SOCKET_FACTORY
- The default SocketFactory shared by all SocketClient instances.
_timeout_
protected int _timeout_
- The timeout to use after opening a socket.
_socket_
protected java.net.Socket _socket_
- The socket used for the connection.
_isConnected_
protected boolean _isConnected_
- A status variable indicating if the client's socket is currently open.
_defaultPort_
protected int _defaultPort_
- The default port the client should connect to.
_input_
protected java.io.InputStream _input_
- The socket's InputStream.
_output_
protected java.io.OutputStream _output_
- The socket's OutputStream.
_socketFactory_
protected SocketFactory _socketFactory_
- The socket's SocketFactory.
| Constructor Detail |
SocketClient
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
DefaultSocketFactory.
| Method Detail |
_connectAction_
protected void _connectAction_()
throws java.io.IOException
- 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 setDefaultTimeout() 55 , 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(java.net.InetAddress host, int port) throws java.net.SocketException, java.io.IOException
- 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, _connectAction_() 55
is called to perform connection initialization actions.
connect
public void connect(java.lang.String hostname, int port) throws java.net.SocketException, java.io.IOException
- 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, _connectAction_() 55
is called to perform connection initialization actions.
connect
public void connect(java.net.InetAddress host, int port, java.net.InetAddress localAddr, int localPort) throws java.net.SocketException, java.io.IOException
- Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning, _connectAction_() 55
is called to perform connection initialization actions.
connect
public void connect(java.lang.String hostname, int port, java.net.InetAddress localAddr, int localPort) throws java.net.SocketException, java.io.IOException
- Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning, _connectAction_() 55
is called to perform connection initialization actions.
connect
public void connect(java.net.InetAddress host) throws java.net.SocketException, java.io.IOException
- 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, _connectAction_() 55
is called to perform connection initialization actions.
connect
public void connect(java.lang.String hostname) throws java.net.SocketException, java.io.IOException
- 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, _connectAction_() 55
is called to perform connection initialization actions.
disconnect
public void disconnect()
throws java.io.IOException
- Disconnects the socket connection.
You should call this method after you've finished using the class
instance and also before you call
connect() 55
again. _isConnected_ is set to false, _socket_ is set to null,
_input_ is set to null, and _output_ is set to null.
isConnected
public boolean isConnected()
- Returns true if the client is currently connected to a server.
setDefaultPort
public void setDefaultPort(int port)
- Sets the default port the SocketClient should connect to when a port
is not specified. The _defaultPort_ 55
variable stores this value. If never set, the default port is equal
to zero.
getDefaultPort
public int getDefaultPort()
- Returns the current value of the default port (stored in
_defaultPort_ 55 ).
setDefaultTimeout
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
connect() 55
and should not be confused with setSoTimeout() 55
which operates on an the currently opened socket. _timeout_ contains
the new timeout value.
getDefaultTimeout
public int getDefaultTimeout()
- Returns the default timeout in milliseconds that is used when
opening a socket.
setSoTimeout
public void setSoTimeout(int timeout)
throws java.net.SocketException
- Set the timeout in milliseconds of a currently open connection.
Only call this method after a connection has been opened
by connect() 55 .
getSoTimeout
public int getSoTimeout()
throws java.net.SocketException
- Returns the timeout in milliseconds of the currently opened socket.
setTcpNoDelay
public void setTcpNoDelay(boolean on)
throws java.net.SocketException
- Enables or disables the Nagle's algorithm (TCP_NODELAY) on the
currently opened socket.
getTcpNoDelay
public boolean getTcpNoDelay()
throws java.net.SocketException
- Returns true if Nagle's algorithm is enabled on the currently opened
socket.
setSoLinger
public void setSoLinger(boolean on,
int val)
throws java.net.SocketException
- Sets the SO_LINGER timeout on the currently opened socket.
getSoLinger
public int getSoLinger()
throws java.net.SocketException
- Returns the current SO_LINGER timeout of the currently opened socket.
getLocalPort
public int getLocalPort()
- Returns the port number of the open socket on the local host used
for the connection.
getLocalAddress
public java.net.InetAddress getLocalAddress()
- Returns the local address to which the client's socket is bound.
getRemotePort
public int getRemotePort()
- Returns the port number of the remote host to which the client is
connected.
getRemoteAddress
public java.net.InetAddress getRemoteAddress()
verifyRemote
public 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. 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.
setSocketFactory
public void setSocketFactory(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).
|
|||||||||
| Home >> All >> org >> apache >> commons >> [ net overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC
org.apache.commons.net.SocketClient