Creating an interactive telnet session

Top  Previous  Next

To create an interactive telnet session you must first understand how to establish a connection, perform option negotiation, send data to the telnet server and receive data from the telnet server. Each of these is covered in the sections titled Establishing a connection , Option negotiation , Receiving data and Sending data respectively.


This section will demonstrate how to create a very basic interactive telnet client that can be run from the console. The source code for this example is located in the file included in the examples directory of the distribution.


Starting telnet client


To start the interactive telnet client example from the console the TelnetExample#main method is invoked.



* Main method for launching program

* @param args program arguments


public static void main(String[] args) {

try {

   reader = new BufferedReader(new InputStreamReader(;


  // prompt user for telnet server hostname

   System.out.print("Enter telnet server hostname (e.g. ");

   String hostname = reader.readLine();


  // create new TelnetExample instance

   TelnetExample example = new TelnetExample(hostname);


 } catch (Exception e) {





A is created using as the . This will be used to read data entered by the user to be sent to the telnet server. The user is prompted for a telnet server hostname. This hostname will be used when establishing a connection.


Establishing a connection and listening for events


Upon invoking the TelnetExample constructor a Telnet instance is created and the TelnetExample class is registered to handle any events fired by the Telnet instance. Next, a connection to the telnet server is established by invoking the Telnet#connect method and an OutputStream is retrieved by invoking the Telnet#getOutputStream method.


Next, a while loop is created that constantly reads data entered by the user at the console. This data is then sent to the telnet server using the TelnetOutputStream#println method.


public TelnetExample(String hostname) throws IOException, TelnetException {


 String input = null;

// create new Telnet instance

 telnet = new Telnet(hostname);


// register this class as TelnetListener



// establish telnet connection


 connected = true;


// get output stream

 output = telnet.getOutputStream();


// sends all data entered at console to telnet server

while ((input = reader.readLine()) != null) {

  if (connected) {

     ((TelnetOutputStream) output).println(input);

   } else {






Capturing events


Several events are fired during the telnet session. To capture and process these events the TelnetExample class extends the TelnetAdapter class and overloads the event handling methods it is interested in. For more information on capturing events see Capturing telnet related events.




The first event to be fired is the TelnetConnectedEvent . This event is fired once a successful connection has been established. To capture the TelnetConnectedEvent overload the TelnetAdapter#connected method.



* Invoked when telnet socket is connected.

* @see TelnetConnectedEvent

* @see Telnet#connect


public void connected(TelnetConnectedEvent event) {




DoOptionEvent and WillOptionEvent


Once connected option negotiation will begin. To keep things simple the TelnetExample client refuses all options requested or offered by the telnet server. This is done by overloading the TelnetAdapter#doOption and TelnetAdapter#willOption methods and refusing the options by invoking the Telnet#sendWontOption and Telnet#sendDontOption methods respectively.



* Invoked when telnet server requests that the telnet client begin performing specified <code>TelnetOption</code>.

* @param event a <code>DoOptionEvent</code>

* @see DoOptionEvent

* @see TelnetOption


public void doOption(DoOptionEvent event) {

// refuse any options requested by telnet server





* Invoked when telnet server offers to begin performing specified <code>TelnetOption</code>.

* @param event a <code>WillOptionEvent</code>

* @see WillOptionEvent

* @see TelnetOption


public void willOption(WillOptionEvent event) {

// refuse any options offered by telnet server






Data that is sent by the telnet server for display to the user is captured in the TelnetDataReceivedEvent . To capture this data overload the TelnetAdapter#dataReceived method. In the TelnetExample class, client data is received and printed out to the console using System.out



* Invoked when data is received from telnet server.

* @param event a <code>TelnetDataReceivedEvent</code>

* @see TelnetDataReceivedEvent


public void dataReceived(TelnetDataReceivedEvent event) {

// print data received from telnet server to console






To disconnect from the telnet server the user may enter the command "exit" (without quotes) at the shell prompt. This will in effect release the connection. Once the connection has been released a TelnetDisconnectedEvent will be fired. In the TelnetExample this sets the connected variable to false which in turn triggers the BufferedReader constructed in the TelnetExample#main method to stop reading data from the console.



* Invoked when telnet socket is disconnected. Disconnect can

* occur in many circumstances including IOException during socket read/write.

* @see TelnetDisconnectedEvent

* @see Telnet#disconnect


public void disconnected(TelnetDisconnectedEvent event) {

 connected = false;

 System.out.print("Disconnected.  Press enter key to quit.");




Home | Company | Products | Solutions | Purchase | Support | Services | Blog