socketclient.java

来自「apache推出的net包」· Java 代码 · 共 505 行 · 第 1/2 页

JAVA
505
字号
    /**     * Disconnects the socket connection.     * You should call this method after you've finished using the class     * instance and also before you call     * {@link #connect connect() }     * again.  _isConnected_ is set to false, _socket_ is set to null,     * _input_ is set to null, and _output_ is set to null.     * <p>     * @exception IOException  If there is an error closing the socket.     */    public void disconnect() throws IOException    {        _socket_.close();        _input_.close();        _output_.close();        _socket_ = null;        _input_ = null;        _output_ = null;        _isConnected_ = false;    }    /**     * Returns true if the client is currently connected to a server.     * <p>     * @return True if the client is currently connected to a server,     *         false otherwise.     */    public boolean isConnected()    {        return _isConnected_;    }    /**     * Sets the default port the SocketClient should connect to when a port     * is not specified.  The {@link #_defaultPort_  _defaultPort_ }     * variable stores this value.  If never set, the default port is equal     * to zero.     * <p>     * @param port  The default port to set.     */    public void setDefaultPort(int port)    {        _defaultPort_ = port;    }    /**     * Returns the current value of the default port (stored in     * {@link #_defaultPort_  _defaultPort_ }).     * <p>     * @return The current value of the default port.     */    public int getDefaultPort()    {        return _defaultPort_;    }    /**     * Set the default timeout in milliseconds to use when opening a socket.     * This value is only used previous to a call to     * {@link #connect connect()}     * and should not be confused with {@link #setSoTimeout setSoTimeout()}     * which operates on an the currently opened socket.  _timeout_ contains     * the new timeout value.     * <p>     * @param timeout  The timeout in milliseconds to use for the socket     *                 connection.     */    public void setDefaultTimeout(int timeout)    {        _timeout_ = timeout;    }    /**     * Returns the default timeout in milliseconds that is used when     * opening a socket.     * <p>     * @return The default timeout in milliseconds that is used when     *         opening a socket.     */    public int getDefaultTimeout()    {        return _timeout_;    }    /**     * Set the timeout in milliseconds of a currently open connection.     * Only call this method after a connection has been opened     * by {@link #connect connect()}.     * <p>     * @param timeout  The timeout in milliseconds to use for the currently     *                 open socket connection.     * @exception SocketException If the operation fails.     */    public void setSoTimeout(int timeout) throws SocketException    {        _socket_.setSoTimeout(timeout);    }    /**     * Returns the timeout in milliseconds of the currently opened socket.     * <p>     * @return The timeout in milliseconds of the currently opened socket.     * @exception SocketException If the operation fails.     */    public int getSoTimeout() throws SocketException    {        return _socket_.getSoTimeout();    }    /**     * Enables or disables the Nagle's algorithm (TCP_NODELAY) on the     * currently opened socket.     * <p>     * @param on  True if Nagle's algorithm is to be enabled, false if not.     * @exception SocketException If the operation fails.     */    public void setTcpNoDelay(boolean on) throws SocketException    {        _socket_.setTcpNoDelay(on);    }    /**     * Returns true if Nagle's algorithm is enabled on the currently opened     * socket.     * <p>     * @return True if Nagle's algorithm is enabled on the currently opened     *        socket, false otherwise.     * @exception SocketException If the operation fails.     */    public boolean getTcpNoDelay() throws SocketException    {        return _socket_.getTcpNoDelay();    }    /**     * Sets the SO_LINGER timeout on the currently opened socket.     * <p>     * @param on  True if linger is to be enabled, false if not.     * @param val The linger timeout (in hundredths of a second?)     * @exception SocketException If the operation fails.     */    public void setSoLinger(boolean on, int val) throws SocketException    {        _socket_.setSoLinger(on, val);    }    /**     * Returns the current SO_LINGER timeout of the currently opened socket.     * <p>     * @return The current SO_LINGER timeout.  If SO_LINGER is disabled returns     *         -1.     * @exception SocketException If the operation fails.     */    public int getSoLinger() throws SocketException    {        return _socket_.getSoLinger();    }    /**     * Returns the port number of the open socket on the local host used     * for the connection.     * <p>     * @return The port number of the open socket on the local host used     *         for the connection.     */    public int getLocalPort()    {        return _socket_.getLocalPort();    }    /**     * Returns the local address to which the client's socket is bound.     * <p>     * @return The local address to which the client's socket is bound.     */    public InetAddress getLocalAddress()    {        return _socket_.getLocalAddress();    }    /**     * Returns the port number of the remote host to which the client is     * connected.     * <p>     * @return The port number of the remote host to which the client is     *         connected.     */    public int getRemotePort()    {        return _socket_.getPort();    }    /**     * @return The remote address to which the client is connected.     */    public InetAddress getRemoteAddress()    {        return _socket_.getInetAddress();    }    /**     * 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.     * <p>     * @return True if the remote hosts are the same, false if not.     */    public boolean verifyRemote(Socket socket)    {        InetAddress host1, host2;        host1 = socket.getInetAddress();        host2 = getRemoteAddress();        return host1.equals(host2);    }    /**     * 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).     * <p>     * @param factory  The new SocketFactory the SocketClient should use.     */    public void setSocketFactory(SocketFactory factory)    {        if (factory == null)            _socketFactory_ = __DEFAULT_SOCKET_FACTORY;        else            _socketFactory_ = factory;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?