com.jscape.inet.telnet

## Class Telnet

- java.lang.Object
- - com.jscape.inet.telnet.Telnet

- All Implemented Interfaces:
    java.io.Serializable
  Direct Known Subclasses:
    TelnetSsh, TelnetSsl

      public class Telnet
      extends java.lang.Object
      implements java.io.Serializable

  Implements the basic functionality of a Telnet client as defined in RFC 854. Upon establishing a connection the Telnet server may attempt to perform option negotiation (see RFC 855). Any option negotiation options exchanged must be handled prior to receiving a login or shell prompt.

  To handle option negotiation you must add a TelnetListener to the Telnet instance and overload the doOption, dontOption, willOption and wontOption methods.

  Once option negotiation has completed sucessfully a separate thread is spawned that reads data sent by the Telnet server. You can capture this data by overloading the dataReceived method in the TelnetListener interface or TelnetAdapter class.

  Example:

       public class TelnetExample extends TelnetAdapter {

            private Telnet telnet = null;
            private OutputStream output = null;
            private static BufferedReader reader = null;
            private boolean connected = false;
            private String hostname = "10.0.0.1";

            public TelnetExample() throws IOException, TelnetException {

                    // create new Telnet instance
                    telnet = new Telnet(hostname);

                    // register this class as TelnetListener
                    telnet.addTelnetListener(this);

                    // establish Telnet connection
                    telnet.connect();
                    connected = true;

                    // get output stream
                    output = telnet.getOutputStream();

                    // sends all data entered at console to Telnet server
                    String input = null;
                    while ((input = reader.readLine()) != null) {
                            if (connected) {
                                    ((TelnetOutputStream) output).println(input);
                   } else {
                                    break;
                   }
               }
           }

            // Invoked when Telnet socked is connected.
            public void connected(TelnetConnectedEvent event) {
                    System.out.println("Connected");
           }

            // Invoked when Telnet socket is disconnected. Disconnect can
            public void disconnected(TelnetDisconnectedEvent event) {
                    connected = false;
                    System.out.print("Disconnected.  Press enter key to quit.");
           }

            // Invoked when Telnet server requests that the Telnet client begin performing specified TelnetOption.
            public void doOption(DoOptionEvent event) {
                    // refuse any options requested by Telnet server
                    telnet.sendWontOption(event.getOption());
           }

            // Invoked when Telnet server offers to begin performing specified TelnetOption.
            public void willOption(WillOptionEvent event) {
                    // refuse any options offered by Telnet server
                    telnet.sendDontOption(event.getOption());
           }

            // Invoked when data is received from Telnet server.
            public void dataReceived(TelnetDataReceivedEvent event) {
                    // print data recevied from Telnet server to console
                    System.out.print(event.getData());
           }

            // starts console program
            public static void main(String[] args) {
                    try {
                            // create BufferedReader to read data from console
                            reader = new BufferedReader(new InputStreamReader(System.in));

                            // create new TelnetExample instance
                            TelnetExample example = new TelnetExample();

               } catch (Exception e) {
                            e.printStackTrace(System.out);
               }
           }

       }

  See Also:
    Serialized Form

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `Telnet.ByteDecoder` Byte Decoder
    `class` | `Telnet.DefaultDecoder` Default Decoder
    `class` | `Telnet.EUCKoreanDecoder` EUC Korean Decoder
    `class` | `Telnet.UTF8Decoder` UTF8 Decoder

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.lang.String[]` | `COMMAND_MAP` This field can be used to map command codes to their String representation.
    `static int` | `TC_AO` Telnet command : Abort output
    `static int` | `TC_AYT` Telnet command : Are You There
    `static int` | `TC_BRK` Telnet command : Break. NVT character BRK.
    `static int` | `TC_DM` Telnet command : Data Mark. The data stream portion of a Synch.
    `static int` | `TC_DO` Telnet command : DO. Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option.
    `static int` | `TC_DONT` Telnet command : DON'T. Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option.
    `static int` | `TC_GA` Telnet command : Go ahead
    `static int` | `TC_IP` Telnet command : Interrupt Process.
    `static int` | `TC_NOP` Telnet command : No operation.
    `static int` | `TC_SB` Telnet command : Subnegotiation. Indicates that what follows is subnegotiation of the indicated option.
    `static int` | `TC_SE` Telnet command : End of subnegotiation parameters.
    `static int` | `TC_WILL` Telnet command : WILL. Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option.
    `static int` | `TC_WONT` Telnet command : WON'T. Indicates the refusal to perform, or continue performing, the indicated option.
    `static int` | `TSC_BEL` Telnet special character : BELL. Produces an audible or visible signal (which does NOT move the print head).
    `static int` | `TSC_BS` Telnet special character : Back Space. Moves the print head one character position towards the left margin.
    `static int` | `TSC_CR` Telnet special character : Carriage Return. Moves the printer to the left margin of the current line.
    `static int` | `TSC_EC` Telnet special character : Erase character
    `static int` | `TSC_EL` Telnet special character : Erase line
    `static int` | `TSC_FF` Telnet special character : Form Feed. Moves the printer to the top of the next page, keeping the same horizontal position.
    `static int` | `TSC_HT` Telnet special character : Horizontal Tab. Moves the printer to the next horizontal tab stop.
    `static int` | `TSC_IAC` Telnet escape charater : Interpret as Command
    `static int` | `TSC_LF` Telnet special character : Line Feed. Moves the printer to the next print line, keeping the same horizontal position.
    `static int` | `TSC_NULL` Telnet special character : No operation
    `static int` | `TSC_VT` Telnet special character : Vertical Tab. Moves the printer to the next vertical tab stop.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Telnet(java.lang.String hostname)` Constructs Telnet object for connection to specified host.
    `Telnet(java.lang.String host, int port)` Constructs Telnet object.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addTelnetListener(TelnetListener listener)` Add telnet event listener.
    `void` | `clearProxySettings()` Clears proxy server values.
    `void` | `connect()` Connect the client socket to host and port specified by contructor or `setHostname(java.lang.String)`, `setPort(int)` methods.
    `void` | `disconnect()` Disconnects and releases all associated resources.
    `java.lang.String` | `getCharacterSet()` Gets the character set used when reading data from Telnet server.
    `java.lang.String` | `getCommandName(int command)` Gets name of command based on corresponding command.
    `boolean` | `getDebug()` Gets debugging state.
    `java.io.PrintStream` | `getDebugStream()` Gets PrintStream used in reporting debug statements.
    `java.lang.String` | `getHostname()` Return server hostname.
    `java.io.InputStream` | `getInputStream()` Returns input stream associated with current Telnet connection.
    `java.io.OutputStream` | `getOutputStream()` Returns output stream associated with current telnet connection.
    `int` | `getPort()` Return server port.
    `java.io.Reader` | `getReader()` Returns Reader object for reading from the server.
    `int` | `getReadingTimeout()` Returns the socket reading timeout value.
    `int` | `getTimeout()` Gets the timeout for opening connection to Telnet server.
    `java.io.Writer` | `getWriter()` Returns Writer object for writing to server.
    `void` | `removeTelnetListener(TelnetListener listener)` Remove telnet event listener.
    `void` | `sendCommand(int command)` Sends telnet command.
    `void` | `sendCommand(int command, int parameter)` Sends telnet command with specified parameter.
    `void` | `sendDontOption(TelnetOption opt)` Sends Telnet option to server with Telnet DONT OPTION (refuse request to use option) command
    `void` | `sendDoOption(TelnetOption opt)` Sends Telnet option to server with Telnet DO OPTION (request to use option) command
    `void` | `sendOptionSubnegotiation(TelnetOption opt)` Initiate subnegotiation for option.
    `void` | `sendWillOption(TelnetOption opt)` Sends Telnet option to server with Telnet WILL OPTION (accept offer to use option) command.
    `void` | `sendWontOption(TelnetOption opt)` Sends Telnet option to server with Telnet WONT OPTION (refuse offer to use option) command.
    `void` | `setCharacterSet(java.lang.String characterSet)` Sets character set used when reading data from telnet server
    `void` | `setDebug(boolean debug)` Enable debugging output to debugging stream.
    `void` | `setDebugStream(java.io.PrintStream debugStream)` Sets PrintStream used in reporting debug statements.
    `void` | `setHostname(java.lang.String hostname)` Set server hostname.
    `void` | `setPort(int port)` Set server port.
    `void` | `setProxyAuthentication(java.lang.String proxyUsername, java.lang.String proxyPassword)` Sets the username and password to use when for authentication with proxy server.
    `void` | `setProxyHost(java.lang.String proxyHostname, int proxyPort)` Sets the proxy hostname and port for this connection.
    `void` | `setProxyType(java.lang.String proxyType)` Sets the proxy type will be used for this connection.
    `void` | `setReadingTimeout(int value)` Sets the socket reading timeout value.
    `void` | `setTimeout(int timeout)` Sets the timeout for opening connection to Telnet server.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### TSC_NULL

          public static final int TSC_NULL

      Telnet special character : No operation
      See Also:
        Constant Field Values

    - #### TSC_BEL

          public static final int TSC_BEL

      Telnet special character : BELL.
      Produces an audible or visible signal (which does NOT move the print head).
      See Also:
        Constant Field Values

    - #### TSC_BS

          public static final int TSC_BS

      Telnet special character : Back Space.
      Moves the print head one character position towards the left margin.
      See Also:
        Constant Field Values

    - #### TSC_HT

          public static final int TSC_HT

      Telnet special character : Horizontal Tab.
      Moves the printer to the next horizontal tab stop. It remains unspecified how either party determines or establishes where such tab stops are located.
      See Also:
        Constant Field Values

    - #### TSC_LF

          public static final int TSC_LF

      Telnet special character : Line Feed.
      Moves the printer to the next print line, keeping the same horizontal position.
      See Also:
        Constant Field Values

    - #### TSC_VT

          public static final int TSC_VT

      Telnet special character : Vertical Tab.
      Moves the printer to the next vertical tab stop. It remains unspecified how either party determines or establishes where such tab stops are located.
      See Also:
        Constant Field Values

    - #### TSC_FF

          public static final int TSC_FF

      Telnet special character : Form Feed.
      Moves the printer to the top of the next page, keeping the same horizontal position.
      See Also:
        Constant Field Values

    - #### TSC_CR

          public static final int TSC_CR

      Telnet special character : Carriage Return.
      Moves the printer to the left margin of the current line.
      See Also:
        Constant Field Values

    - #### TC_SE

          public static final int TC_SE

      Telnet command : End of subnegotiation parameters.
      See Also:
        Constant Field Values

    - #### TC_NOP

          public static final int TC_NOP

      Telnet command : No operation.
      See Also:
        Constant Field Values

    - #### TC_DM

          public static final int TC_DM

      Telnet command : Data Mark.
      The data stream portion of a Synch. This should always be accompanied by a TCP Urgent notification.
      See Also:
        Constant Field Values

    - #### TC_BRK

          public static final int TC_BRK

      Telnet command : Break.
      NVT character BRK.
      See Also:
        Constant Field Values

    - #### TC_IP

          public static final int TC_IP

      Telnet command : Interrupt Process.
      See Also:
        Constant Field Values

    - #### TC_AO

          public static final int TC_AO

      Telnet command : Abort output
      See Also:
        Constant Field Values

    - #### TC_AYT

          public static final int TC_AYT

      Telnet command : Are You There
      See Also:
        Constant Field Values

    - #### TSC_EC

          public static final int TSC_EC

      Telnet special character : Erase character
      See Also:
        Constant Field Values

    - #### TSC_EL

          public static final int TSC_EL

      Telnet special character : Erase line
      See Also:
        Constant Field Values

    - #### TC_GA

          public static final int TC_GA

      Telnet command : Go ahead
      See Also:
        Constant Field Values

    - #### TC_SB

          public static final int TC_SB

      Telnet command : Subnegotiation.
      Indicates that what follows is subnegotiation of the indicated option.
      See Also:
        Constant Field Values

    - #### TC_WILL

          public static final int TC_WILL

      Telnet command : WILL.
      Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option.
      See Also:
        Constant Field Values

    - #### TC_WONT

          public static final int TC_WONT

      Telnet command : WON'T.
      Indicates the refusal to perform, or continue performing, the indicated option.
      See Also:
        Constant Field Values

    - #### TC_DO

          public static final int TC_DO

      Telnet command : DO.
      Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option.
      See Also:
        Constant Field Values

    - #### TC_DONT

          public static final int TC_DONT

      Telnet command : DON'T.
      Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option.
      See Also:
        Constant Field Values

    - #### TSC_IAC

          public static final int TSC_IAC

      Telnet escape charater : Interpret as Command
      See Also:
        Constant Field Values

    - #### COMMAND_MAP

          public static final java.lang.String[] COMMAND_MAP

      This field can be used to map command codes to their String representation.

  - ### Constructor Detail

    - #### Telnet

          public Telnet(java.lang.String hostname)

      Constructs Telnet object for connection to specified host.
      Parameters:
        `hostname` - the hostname or IP address of the Telnet server

    - #### Telnet

          public Telnet(java.lang.String host,
                        int port)

      Constructs Telnet object.
      Parameters:
        `host` - the hostname or IP address of the Telnet server
        `port` - the port of the Telnet server

  - ### Method Detail

    - #### connect

          public void connect()
                       throws TelnetException

      Connect the client socket to host and port specified by contructor or `setHostname(java.lang.String)`, `setPort(int)` methods.
      Throws:
        `TelnetException`

    - #### disconnect

          public void disconnect()

      Disconnects and releases all associated resources.

    - #### setHostname

          public void setHostname(java.lang.String hostname)

      Set server hostname.
      Parameters:
        `hostname` - the hostname or IP address of the Telnet server

    - #### getHostname

          public java.lang.String getHostname()

      Return server hostname.
      Returns:
        the hostname or IP address of the Telnet server

    - #### setPort

          public void setPort(int port)

      Set server port.
      Parameters:
        `port` - the port of the Telnet server

    - #### getPort

          public int getPort()

      Return server port.
      Returns:
        the port of the Telnet server

    - #### setProxyAuthentication

          public void setProxyAuthentication(java.lang.String proxyUsername,
                                             java.lang.String proxyPassword)

      Sets the username and password to use when for authentication with proxy server. To clear these settings invoke the `#clearProxySettings` method.
      Parameters:
        `proxyUsername` - the proxy username
        `proxyPassword` - the proxy password
      See Also:
        `clearProxySettings()`

    - #### setProxyHost

          public void setProxyHost(java.lang.String proxyHostname,
                                   int proxyPort)

      Sets the proxy hostname and port for this connection. To clear these settings invoke the `#clearProxySettings` method.
      Parameters:
        `proxyHostname` - the hostname or ip address of the proxy server
        `proxyPort` - the port of the proxy server
      See Also:
        `clearProxySettings()`

    - #### setProxyType

          public void setProxyType(java.lang.String proxyType)

      Sets the proxy type will be used for this connection.
      Parameters:
        `proxyType` - The proxy type. Valid values: HTTP, SOCKS5

    - #### clearProxySettings

          public void clearProxySettings()

      Clears proxy server values.

    - #### addTelnetListener

          public void addTelnetListener(TelnetListener listener)

      Add telnet event listener.
      Parameters:
        `listener` - a TelnetListener
      See Also:
        `TelnetListener`

    - #### removeTelnetListener

          public void removeTelnetListener(TelnetListener listener)

      Remove telnet event listener.
      Parameters:
        `listener` - a TelnetListener
      See Also:
        `TelnetListener`

    - #### sendCommand

          public void sendCommand(int command)

      Sends telnet command.
      Parameters:
        `command` - the command to send

    - #### sendCommand

          public void sendCommand(int command,
                                  int parameter)

      Sends telnet command with specified parameter.
      Parameters:
        `command` - the command to send
        `parameter` - the parameter to send

    - #### getInputStream

          public java.io.InputStream getInputStream()

      Returns input stream associated with current Telnet connection. Telnet commands, special and control characters are filtered out of this stream.
      Returns:
        an InputStream

    - #### getOutputStream

          public java.io.OutputStream getOutputStream()

      Returns output stream associated with current telnet connection. Commands, special and control characters should be written only using Telnet class methods, rather than directly writing to this stream.
      Returns:
        an OutputStream

    - #### getReader

          public java.io.Reader getReader()

      Returns Reader object for reading from the server.
      Returns:
        a Reader

    - #### getWriter

          public java.io.Writer getWriter()

      Returns Writer object for writing to server.
      Returns:
        a Writer

    - #### sendOptionSubnegotiation

          public void sendOptionSubnegotiation(TelnetOption opt)

      Initiate subnegotiation for option.
      Parameters:
        `opt` - a TelnetOption
      See Also:
        `TelnetOption`

    - #### getCommandName

          public java.lang.String getCommandName(int command)

      Gets name of command based on corresponding command.
      Parameters:
        `command` - the command code
      Returns:
        the name of command code or null if not found

    - #### sendWillOption

          public void sendWillOption(TelnetOption opt)

      Sends Telnet option to server with Telnet WILL OPTION (accept offer to use option) command.
      Parameters:
        `opt` - the option to send

    - #### sendWontOption

          public void sendWontOption(TelnetOption opt)

      Sends Telnet option to server with Telnet WONT OPTION (refuse offer to use option) command.
      Parameters:
        `opt` - the option to send

    - #### sendDoOption

          public void sendDoOption(TelnetOption opt)

      Sends Telnet option to server with Telnet DO OPTION (request to use option) command
      Parameters:
        `opt` - the option to send

    - #### sendDontOption

          public void sendDontOption(TelnetOption opt)

      Sends Telnet option to server with Telnet DONT OPTION (refuse request to use option) command
      Parameters:
        `opt` - the option to send

    - #### setDebug

          public void setDebug(boolean debug)

      Enable debugging output to debugging stream.
      Parameters:
        `debug` - true if debugging enabled, false otherwise
      See Also:
        `setDebugStream(java.io.PrintStream)`

    - #### getDebug

          public boolean getDebug()

      Gets debugging state.
      Returns:
        `true` if debugging is enabled

    - #### getDebugStream

          public java.io.PrintStream getDebugStream()

      Gets PrintStream used in reporting debug statements. Default PrintStream is System.out
      Returns:
        the PrintStream used in reporting debug statements
      See Also:
        `PrintStream`

    - #### setTimeout

          public void setTimeout(int timeout)

      Sets the timeout for opening connection to Telnet server.
      Parameters:
        `timeout` - the timeout in milliseconds

    - #### getTimeout

          public int getTimeout()

      Gets the timeout for opening connection to Telnet server.
      Returns:
        timeout in milliseconds

    - #### getReadingTimeout

          public int getReadingTimeout()

      Returns the socket reading timeout value.
      Returns:
        reading timeout in millis

    - #### setReadingTimeout

          public void setReadingTimeout(int value)

      Sets the socket reading timeout value.
      Parameters:
        `value` - timeout value is millis

    - #### setCharacterSet

          public void setCharacterSet(java.lang.String characterSet)

      Sets character set used when reading data from telnet server
      Parameters:
        `characterSet` - the character set

    - #### getCharacterSet

          public java.lang.String getCharacterSet()

      Gets the character set used when reading data from Telnet server. Default is null, using the default system character set.
      Returns:
        the character set or null

    - #### setDebugStream

          public void setDebugStream(java.io.PrintStream debugStream)

      Sets PrintStream used in reporting debug statements. Default PrintStream is System.out
      Parameters:
        `debugStream` - the PrintStream to send debug statements to
      See Also:
        `PrintStream`

Copyright © JSCAPE LLC. 1999-2021. All Rights Reserved