Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Telnet using C#

This article will demonstrate how using the Telnet Factory for .NET component you can establish an interactive TELNET session with a TELNET server. This article will also serve as a prerequisite to an upcoming article titled Scripting Telnet Sessions using C#, which will demonstrate how to automate TELNET commands in a batch like process.

To see what else Telnet Factory for .NET has to offer Download a FREE 30 day Telnet Factory for .NET Evaluation.

Overview

The Telnet Factory for .NET component provides a method for communicating with a TELNET server. The process for establishing an interactive session with a TELNET server using the Telnet Factory for .NET component is as follows:

  1. Creating a new Telnet instance
  2. Subscribing to Telnet Events
  3. Establishing a connection
  4. Performing option negotiation
  5. Receiving data
  6. Sending data
  7. Releasing a connection

Each of these processes is described in the sections below.

Creating a new Telnet instance

Before creating a new Telnet instance, ensure that the Jscape.Telnet scope is defined in your using statements, and that the Jscape.Telnet.dll is referenced in your project. Refer to Getting Started in the Telnet Factory for .NET Help for more information about adding the Jscape.Telnet.dll reference to your projects.

Create a new Telnet instance providing the TELNET server hostname as an argument.

Telnet myTelnet = new Telnet(hostname); 

Subscribing to Telnet Events

The Telnet instance MUST subscribe to the Telnet events prior to invoking the Connect() method to ensure that all data sent by the TELNET server is captured. See Receiving data later in this article for more information about processing this event.

// Subscribe to connection events
myTelnet.ConnectedEvent += new Telnet.ConnectedEventHandler(OnConnected);
myTelnet.DisconnectedEvent += new Telnet.DisconnectedEventHandler(OnDisconnected);
// Subscribe to option events
myTelnet.DoOptionEvent += new Telnet.DoOptionEventHandler(OnDoOption);
myTelnet.DontOptionEvent += new Telnet.DontOptionEventHandler(OnDontOption);
myTelnet.WillOptionEvent += new Telnet.WillOptionEventHandler(OnWillOption);
myTelnet.WontOptionEvent += new Telnet.WontOptionEventHandler(OnWontOption);
// Subscribe to data received event
myTelnet.DataReceivedEvent += new Telnet.DataReceivedEventHandler(OnDataReceived);

Now you can create the event handler methods you previously defined when subscribing to the Telnet events. See Performing option negotiation later in this article for more information about capturing and processing option negotiation events.

public void OnConnected(object sender, TelnetConnectedEventArgs e) {
   // Tell user we connected
   Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port);
}
public void OnDisconnected(object sender, TelnetDisconnectedEventArgs e) {
   // Tell user we disconnected
   this.Connected = false;
   Console.WriteLine("Disconnected.");
}

Establishing a connection

Once a Telnet instance has been created and subscribed to the Telnet events you may establish a connection to the TELNET server by invoking the Connect() method.

myTelnet.Connect();

Performing option negotiation

Upon establishing a connection to a TELNET server the process of option negotiation automatically begins. Option negotiation is a communications process for the TELNET client and the TELNET server to come up with a set of agreed upon protocols. An example of option negotiation is agreeing upon the terminal emulation (e.g. vt100, xterm, dumb) to use during the TELNET session.

Option negotiation, as its name implies, is optional and may be initiated by either the client or server. This does not mean, however, that option negotiation may be ignored. For example, in the event that the server attempts to perform option negotiation the client must respond by either accepting or rejecting the option request. Capturing option negotiation data from the TELNET server is accomplished using the option event handlers subscribed to in the previous section.

In performing option negotiation there are four (4) TELNET protocol commands that can be used by the client and server.

  1. DO OPTION - Requests to enable an option.
  2. DONT OPTION - Refuses offer to enable an option.
  3. WILL OPTION - Offers to enable an option.
  4. WONT OPTION - Refuses request to enable an option.

For the purposes of this article we will refuse all options both requested and offered by the TELNET server. This, in effect, will give us a basic TELNET client that is capable of exchanging data with the TELNET server. In order to capture and refuse options requested or offered by the TELNET server you will need to create the option event handler methods as follows:

public void OnDoOption(object sender, TelnetDoOptionEventArgs e) {
   myTelnet.SendWontOption(e.Option);
}
public void OnDontOption(object sender, TelnetDontOptionEventArgs e) {
   myTelnet.SendDontOption(e.Option);
}
public void OnWillOption(object sender, TelnetWillOptionEventArgs e) {
   myTelnet.SendDontOption(e.Option);
}
public void OnWontOption(object sender, TelnetWontOptionEventArgs e) {
   myTelnet.SendWontOption(e.Option);
}

Receiving data

Once option negotiation has been completed, you may begin receiving data sent by the TELNET server. To capture and display data received from the TELNET server, create a data received event method as follows:

public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {
   // Write ascii characters of byte data received from TELNET server
   Console.Write(Encoding.ASCII.GetString(e.Data));
}

Sending data

To send data to the TELNET server you first obtain a TelnetOutputStream from the Telnet instance. You can then send data to the TELNET server from the console as follows:

// Begin reading and writing data to telnet server
TelnetOutputStream output = myTelnet.GetOutputStream();
string input = "";
while ((input = Console.ReadLine()) != null) {
   if ((this.Connected) && (input != "exit")) {
      ((TelnetOutputStream) output).PrintLn(input);
   } else {
      break;
   }
}

In the example above the input comes from the console and is redirected to the TELNET server using the PrintLn() method. The TelnetOutputStream class is used as it automatically appends a \r\n (carriage return line feed) to the end of the data sent. This is required by the TELNET server for it to know when it may begin processing the data received.

Releasing a connection

To release an established connection simply invoke the Disconnect() method as follows:

myTelnet.Disconnect();

Examples

The source code for this article is available for download and for viewing.

View example source code