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 VB

This article will demonstrate how using the Telnet Factory for .NET component you can establish an interactive Telnet session with a TELNET server using Visual Basic. This article compliments the Telnet using C# article previously released. To see what else Telnet Factory for .NET has to offer Download a FREE 30 day Telnet Factory for .NET Evaluation.


Overview of Telnet

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. Define Telnet Event Handler methods
  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 imports 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.

myTelnet = New Telnet("hostname")


Subscribing to Telnet Events

The Telnet instance subscribes to the Telnet events using the WithEvents keyword. This is necessary 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.

Public WithEvents myTelnet As Telnet

Now you create the event handler methods for each of the Telnet events. See Performing option negotiation later in this article for more information about capturing and processing option negotiation events.

Public Sub OnConnected(ByVal sender As Object, ByVal e As TelnetConnectedEventArgs) Handles myTelnet.ConnectedEvent
   ' Tell user we are connected.
   Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port)
End Sub

Private Sub OnDisconnected(ByVal sender As Object, ByVal e As TelnetDisconnectedEventArgs) Handles myTelnet.DisconnectedEvent
   ' Tell user we disconnected.
   Console.WriteLine("Disconnected.")
End Sub


Establishing a connection

Once a Telnet instance has been created 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.

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:

Private Sub OnDoOption(ByVal sender As Object, ByVal e As TelnetDoOptionEventArgs) Handles myTelnet.DoOptionEvent
   myTelnet.SendWontOption(e.Option)
End Sub

Private Sub OnDontOption(ByVal sender As Object, ByVal e As TelnetDontOptionEventArgs) Handles myTelnet.DontOptionEvent
   myTelnet.SendDontOption(e.Option)
End Sub

Private Sub OnWillOption(ByVal sender As Object, ByVal e As TelnetWillOptionEventArgs) Handles myTelnet.WillOptionEvent
   myTelnet.SendDontOption(e.Option)
End Sub

Private Sub OnWontOption(ByVal sender As Object, ByVal e As TelnetWontOptionEventArgs) Handles myTelnet.WontOptionEvent
   myTelnet.SendWontOption(e.Option)
End Sub


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:

Private Sub OnDataReceived(ByVal sender As Object, ByVal e As TelnetDataReceivedEventArgs) Handles myTelnet.DataReceivedEvent
   ' Write characters of byte data received from TELNET server using default encoding (UTF8)
   Console.Write(myTelnet.Encoding.GetString(e.Data))
End Sub


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
Dim output As TelnetOutputStream = myTelnet.GetOutputStream()
Dim input As String = ""
Do
   input = Console.ReadLine()
   If (input <> "exit") Then
      output.PrintLn(input)
   Else
      Me.Connected = False
   End If
Loop While Me.Connected = True


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 data. 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