Class TelnetClient

  • Direct Known Subclasses:
    FTP

    public class TelnetClient
    extends SocketClient
    The TelnetClient class implements the simple network virtual terminal (NVT) for the Telnet protocol according to RFC 854. It does not implement any of the extra Telnet options because it is meant to be used within a Java program providing automated access to Telnet accessible resources.

    The class can be used by first connecting to a server using the SocketClient connect method. Then an InputStream and OutputStream for sending and receiving data over the Telnet connection can be obtained by using the getInputStream() and getOutputStream() methods. When you finish using the streams, you must call disconnect rather than simply closing the streams.

    Author:
    Daniel F. Savarese, Bruno D'Avanzo
    • Field Detail

      • readerThread

        protected boolean readerThread
      • TERMINAL_TYPE

        protected static final int TERMINAL_TYPE
        Terminal type option
        See Also:
        Constant Field Values
      • TERMINAL_TYPE_SEND

        protected static final int TERMINAL_TYPE_SEND
        Send (for subnegotiation)
        See Also:
        Constant Field Values
      • TERMINAL_TYPE_IS

        protected static final int TERMINAL_TYPE_IS
        Is (for subnegotiation)
        See Also:
        Constant Field Values
    • Constructor Detail

      • TelnetClient

        public TelnetClient()
        Default TelnetClient constructor.
      • TelnetClient

        public TelnetClient​(java.lang.String termtype)
    • Method Detail

      • _connectAction_

        protected void _connectAction_()
                                throws java.io.IOException
        Handles special connection requirements.

        Throws:
        java.io.IOException - If an error occurs during connection setup.
      • disconnect

        public void disconnect()
                        throws java.io.IOException
        Disconnects the telnet session, closing the input and output streams as well as the socket. If you have references to the input and output streams of the telnet connection, you should not close them yourself, but rather call disconnect to properly close the connection.
        Overrides:
        disconnect in class SocketClient
        Throws:
        java.io.IOException - If there is an error closing the socket.
      • getOutputStream

        public java.io.OutputStream getOutputStream()
        Returns the telnet connection output stream. You should not close the stream when you finish with it. Rather, you should call disconnect .

        Returns:
        The telnet connection output stream.
      • getInputStream

        public java.io.InputStream getInputStream()
        Returns the telnet connection input stream. You should not close the stream when you finish with it. Rather, you should call disconnect .

        Returns:
        The telnet connection input stream.
      • getLocalOptionState

        public boolean getLocalOptionState​(int option)
        Returns the state of the option on the local side.

        Parameters:
        option - - Option to be checked.

        Returns:
        The state of the option on the local side.
      • getRemoteOptionState

        public boolean getRemoteOptionState​(int option)
        Returns the state of the option on the remote side.

        Parameters:
        option - - Option to be checked.

        Returns:
        The state of the option on the remote side.
      • sendAYT

        public boolean sendAYT​(long timeout)
                        throws java.io.IOException,
                               java.lang.IllegalArgumentException,
                               java.lang.InterruptedException
        Sends an Are You There sequence and waits for the result.

        Parameters:
        timeout - - Time to wait for a response (millis.)

        Returns:
        true if AYT received a response, false otherwise
        Throws:
        java.lang.InterruptedException
        java.lang.IllegalArgumentException
        java.io.IOException -

      • registerSpyStream

        public void registerSpyStream​(java.io.OutputStream spystream)
        Registers an OutputStream for spying what's going on in the TelnetClient session.

        Parameters:
        spystream - - OutputStream on which session activity will be echoed.
      • stopSpyStream

        public void stopSpyStream()
        Stops spying this TelnetClient.

      • registerNotifHandler

        public void registerNotifHandler​(TelnetNotificationHandler notifhand)
        Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.

        Parameters:
        notifhand - - TelnetNotificationHandler to be registered
      • unregisterNotifHandler

        public void unregisterNotifHandler()
        Unregisters the current notification handler.

      • setReaderThread

        public void setReaderThread​(boolean flag)
        Sets the status of the reader thread. The reader thread status will apply to all subsequent connections

        Parameters:
        flag - - true switches the reader thread on, false switches it off
      • getReaderThread

        public boolean getReaderThread()
        Gets the status of the reader thread.

        Returns:
        true if the reader thread is on, false otherwise