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.
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:
- Creating a new Telnet instance
- Define Telnet Event Handler methods
- Establishing a connection
- Performing option negotiation
- Receiving data
- Sending data
- Releasing a connection
Each of these processes is described in the sections below.
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")
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
Once a Telnet instance has been created you may establish a connection to the TELNET server by invoking the Connect() method.
myTelnet.Connect();
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.
- DO OPTION - Requests to enable an option.
- DONT OPTION - Refuses offer to enable an option.
- WILL OPTION - Offers to enable an option.
- 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
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
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.
To release an established connection simply invoke the Disconnect() method as follows:
myTelnet.Disconnect();
The source code for this article is available for download and for viewing.
View example source code
|