| Method from org.apache.commons.net.SocketClient Detail: |
protected void _connectAction_() throws IOException {
_socket_.setSoTimeout(_timeout_);
_input_ = _socket_.getInputStream();
_output_ = _socket_.getOutputStream();
}
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() ,
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. |
public void connect(InetAddress host) throws SocketException, IOException {
connect(host, _defaultPort_);
}
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_()
is called to perform connection initialization actions.
|
public void connect(String hostname) throws SocketException, IOException {
connect(hostname, _defaultPort_);
}
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_()
is called to perform connection initialization actions.
|
public void connect(InetAddress host,
int port) throws SocketException, IOException {
_socket_ = _socketFactory_.createSocket();
_socket_.connect(new InetSocketAddress(host, port), connectTimeout);
_connectAction_();
}
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_()
is called to perform connection initialization actions.
|
public void connect(String hostname,
int port) throws SocketException, IOException {
_socket_= _socketFactory_.createSocket();
_socket_.connect(new InetSocketAddress(hostname, port), connectTimeout);
_connectAction_();
}
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_()
is called to perform connection initialization actions.
|
public void connect(InetAddress host,
int port,
InetAddress localAddr,
int localPort) throws SocketException, IOException {
_socket_ = _socketFactory_.createSocket();
_socket_.bind(new InetSocketAddress(localAddr, localPort));
_socket_.connect(new InetSocketAddress(host, port), connectTimeout);
_connectAction_();
}
Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning, _connectAction_()
is called to perform connection initialization actions.
|
public void connect(String hostname,
int port,
InetAddress localAddr,
int localPort) throws SocketException, IOException {
_socket_ =
_socketFactory_.createSocket(hostname, port, localAddr, localPort);
_connectAction_();
}
Opens a Socket connected to a remote host at the specified port and
originating from the specified local address and port.
Before returning, _connectAction_()
is called to perform connection initialization actions.
|
public void disconnect() throws IOException {
if (_socket_ != null) _socket_.close();
if (_input_ != null) _input_.close();
if (_output_ != null) _output_.close();
if (_socket_ != null) _socket_ = null;
_input_ = null;
_output_ = null;
}
Disconnects the socket connection.
You should call this method after you've finished using the class
instance and also before you call
connect()
again. _isConnected_ is set to false, _socket_ is set to null,
_input_ is set to null, and _output_ is set to null.
|
public int getConnectTimeout() {
return connectTimeout;
}
Get the underlying socket connection timeout. |
public int getDefaultPort() {
return _defaultPort_;
}
Returns the current value of the default port (stored in
_defaultPort_ ).
|
public int getDefaultTimeout() {
return _timeout_;
}
Returns the default timeout in milliseconds that is used when
opening a socket.
|
public InetAddress getLocalAddress() {
return _socket_.getLocalAddress();
}
Returns the local address to which the client's socket is bound.
|
public int getLocalPort() {
return _socket_.getLocalPort();
}
Returns the port number of the open socket on the local host used
for the connection.
|
public InetAddress getRemoteAddress() {
return _socket_.getInetAddress();
}
|
public int getRemotePort() {
return _socket_.getPort();
}
Returns the port number of the remote host to which the client is
connected.
|
public int getSoLinger() throws SocketException {
return _socket_.getSoLinger();
}
Returns the current SO_LINGER timeout of the currently opened socket.
|
public int getSoTimeout() throws SocketException {
return _socket_.getSoTimeout();
}
Returns the timeout in milliseconds of the currently opened socket.
|
public boolean getTcpNoDelay() throws SocketException {
return _socket_.getTcpNoDelay();
}
Returns true if Nagle's algorithm is enabled on the currently opened
socket.
|
public boolean isConnected() {
if (_socket_ == null)
return false;
return _socket_.isConnected();
}
Returns true if the client is currently connected to a server.
|
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
Sets the connection timeout in milliseconds, which will be passed to the Socket object's
connect() method. |
public void setDefaultPort(int port) {
_defaultPort_ = port;
}
Sets the default port the SocketClient should connect to when a port
is not specified. The _defaultPort_
variable stores this value. If never set, the default port is equal
to zero.
|
public void setDefaultTimeout(int timeout) {
_timeout_ = timeout;
}
Set the default timeout in milliseconds to use when opening a socket.
This value is only used previous to a call to
connect()
and should not be confused with setSoTimeout()
which operates on an the currently opened socket. _timeout_ contains
the new timeout value.
|
public void setReceiveBufferSize(int size) throws SocketException {
_socket_.setReceiveBufferSize(size);
}
Sets the underlying socket receive buffer size.
|
public void setSendBufferSize(int size) throws SocketException {
_socket_.setSendBufferSize(size);
}
Set the underlying socket send buffer size.
|
public void setServerSocketFactory(ServerSocketFactory factory) {
if (factory == null)
_serverSocketFactory_ = __DEFAULT_SERVER_SOCKET_FACTORY;
else
_serverSocketFactory_ = factory;
}
Sets the ServerSocketFactory used by the SocketClient to open ServerSocket
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).
|
public void setSoLinger(boolean on,
int val) throws SocketException {
_socket_.setSoLinger(on, val);
}
Sets the SO_LINGER timeout on the currently opened socket.
|
public void setSoTimeout(int timeout) throws SocketException {
_socket_.setSoTimeout(timeout);
}
Set the timeout in milliseconds of a currently open connection.
Only call this method after a connection has been opened
by connect() .
|
public void setSocketFactory(SocketFactory factory) {
if (factory == null)
_socketFactory_ = __DEFAULT_SOCKET_FACTORY;
else
_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).
|
public void setTcpNoDelay(boolean on) throws SocketException {
_socket_.setTcpNoDelay(on);
}
Enables or disables the Nagle's algorithm (TCP_NODELAY) on the
currently opened socket.
|
public boolean verifyRemote(Socket socket) {
InetAddress host1, host2;
host1 = socket.getInetAddress();
host2 = getRemoteAddress();
return host1.equals(host2);
}
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.
|