Using the Telnet class |
The Telnet class allows you to connect to a telnet server in interactive mode (console). To establish a telnet session with a telnet server you first instatiate Telnet passing in the hostname, assign the necessary event handlers, and invoke the Connect method. Once the connection is established you obtain the network stream used to send data to the telnet server.
Example
This example demonstrates an interactive session using the Telnet class.
[C#]
using Jscape.Telnet;
namespace TelnetExample {
public class TelnetExample {
public bool connected = false;
public Telnet myTelnet = null;
public TelnetExample(string hostname){
// Instantiate Telnet
myTelnet = new Telnet(hostname);
// Subscribe to connection events
myTelnet.ConnectedEvent += new Telnet.ConnectedEventHandler(OnConnected);
myTelnet.DisconnectedEvent += new Telnet.DisconnectedEventHandler(OnDisconnected);
// Subscribe to option events. In this example, refuse all options.
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);
// Connect to telnet server
myTelnet.Connect();
this.Connected = true;
// 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;
}
}
// Disconnect from telnet server
myTelnet.Disconnect();
}
public bool Connected {
get {
return connected;
}
set {
connected = value;
}
}
public static void Main() {
Console.Write("Telnet server: ");
string hostname = "";
if ((hostname = Console.ReadLine()) != "") {
TelnetExample telnetexample = new TelnetExample(hostname);
}
}
public void OnConnected(object sender, TelnetConnectedEventArgs e) {
Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port);
}
public void OnDisconnected(object sender, TelnetDisconnectedEventArgs e) {
this.Connected = false;
Console.WriteLine("Disconnected.");
}
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);
}
public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {
Console.Write(myTelnet.Encoding.GetString(e.Data));
}
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class TelnetExample
Public WithEvents myTelnet As Telnet
Private connect As Boolean = False
Public Sub New(ByVal hostname As String)
' Instantiate Telnet
myTelnet = New Telnet(hostname)
' Connect to telnet server
myTelnet.Connect()
' 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
' Disconnect from telnet server
myTelnet.Disconnect()
End Sub
Property Connected() As Boolean
Get
Return connect
End Get
Set(ByVal Value As Boolean)
connect = Value
End Set
End Property
Public Shared Sub Main()
Dim hostname As String
Dim example As TelnetExample
Console.Write("Telnet server: ")
hostname = Console.ReadLine()
If (hostname <> "") Then
example = New TelnetExample(hostname)
End If
End Sub
Public Sub OnConnected(ByVal sender As Object, ByVal e As TelnetConnectedEventArgs) Handles myTelnet.ConnectedEvent
Me.Connected = True
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
Me.Connected = False
Console.WriteLine("Disconnected.")
End Sub
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
Private Sub OnDataReceived(ByVal sender As Object, ByVal e As TelnetDataReceivedEventArgs) Handles myTelnet.DataReceivedEvent
Console.Write(myTelnet.Encoding.GetString(e.Data))
End Sub
End Class