PowerTCP Emulation for .NET
Telnet Class
Members  Example 




The Telnet component enables use of the Telnet protocol within any .NET application.
Object Model
Telnet ClassOptionCollection ClassOption ClassOptionCollection ClassOption ClassTcpSocketOption Class
Syntax
Public Class Telnet 
   Inherits PtyBase
Dim instance As Telnet
public class Telnet : PtyBase 
public __gc class Telnet : public PtyBase 
public ref class Telnet : public PtyBase 
Remarks

The Telnet component establishes and manages Telnet-level streams of data. Methods for establishing connections, handling option negotiation, and sending and receiving data greatly simplify Telnet communications for the user, while still providing many options for advanced customization. Secure SSL connections, asynchronous use, and advanced option negotiation are all fully supported.

The Telnet component can optionally be bound to a display for easy user-interactive development.

Using the Telnet Component

The Telnet component's clean interface provides clear, straight-forward use. All methods calls are blocking calls, making programming an intuitive step-by-step process. Functions can be passed to the Start method, to execute on independent threads, without blocking the user interface. Marshaling data from these threads is very convenient using the Marshal method. See the Asynchronous Operation page for more information on this topic.

Binding the Telnet Component to a Display

Set the Pty (pseudo terminal) property to bind the Telnet component to a display control derived from TextBoxBase (such as a standard TextBox), and call Connect to establish a connection to the specified host. Once connected, keystrokes will be sent automatically, and data received will appear in the text area.

Example
The following example demonstrates execution of an automated session using the Telnet component.
private Telnet telnet1;

private void button1_Click(object sender, EventArgs e)
{
    //Dynamically create an instance of the Telnet component
    //The component can also be dragged onto a form, so that the
    //properties and event handlers can be managed in the designer
    telnet1 = new Telnet();

    //Events should fire on the UI thread
    telnet1.SynchronizingObject = this;

    //Add event handlers
    telnet1.Data += new EventHandler<DataEventArgs>(telnet1_Data);
    telnet1.Error += new EventHandler<ErrorEventArgs>(telnet1_Error);
    telnet1.StateChanged += new EventHandler(telnet1_StateChanged);

    //Start the process on a worker thread
    telnet1.Start(automateSession, null);
}

private void automateSession(object notUsed)
{
    //This function executes on a worker thread, providing multi-threaded, asynchronous operation
    try
    {
        //Connect to the server
        telnet1.Connect("myServer");

        //Login and marshal data to the UI thread
        telnet1.Marshal(telnet1.Login(new Credentials("myUsername", "myPassword", "$")), "", null);

        //Send a list command
        telnet1.Write("ls -la\r");

        //Wait for prompt, marshal data to the UI thread
        telnet1.Marshal(telnet1.ReadToDelimiter("$"), "", null);

        //Send an exit command; server will close the connection
        telnet1.Write("exit\r");

        //Read any remaining data before the shutdown
        telnet1.Marshal(telnet1.ReadToEnd(), "", null);
    }
    catch (Exception ex)
    {
        //Report errors to the UI thread
        telnet1.Marshal(ex);
    }
}

void telnet1_Data(object sender, DataEventArgs e)
{
    //Add data received to the textbox
    textBox1.AppendText(e.Data.ToString());
}

void telnet1_Error(object sender, ErrorEventArgs e)
{
    //Add error messages to the textbox
    textBox1.AppendText(e.GetException().Message);
}

void telnet1_StateChanged(object sender, EventArgs e)
{
    //Change appearance of textbox when connected
    textBox1.BackColor = (telnet1.State == ConnectionState.Closed)
    ? SystemColors.ControlDark
    : SystemColors.ControlLight;
}
Private telnet1 As Telnet

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Dynamically create an instance of the Telnet component
    'The component can also be dragged onto a form, so that the
    'properties and event handlers can be managed in the designer
    telnet1 = New Telnet()

    'Events should fire on the UI thread
    telnet1.SynchronizingObject = Me

    'Add event handlers
    AddHandler telnet1.Data, AddressOf telnet1_Data
    AddHandler telnet1.Error, AddressOf telnet1_Error
    AddHandler telnet1.StateChanged, AddressOf telnet1_StateChanged

    'Start the process on a worker thread
    telnet1.Start(AddressOf automateSession, Nothing)
End Sub

Private Sub automateSession(ByVal notUsed As Object)
    'This function executes on a worker thread, providing multi-threaded, asynchronous operation
    Try
        'Connect to the server
        telnet1.Connect("myServer")

        'Login and marshal data to the UI thread
        telnet1.Marshal(telnet1.Login(New Credentials("myUsername", "myPassword", "$")), "", Nothing)

        'Send a list command
        telnet1.Write("ls -la" & Constants.vbCr)

        'Wait for prompt, marshal data to the UI thread
        telnet1.Marshal(telnet1.ReadToDelimiter("$"), "", Nothing)

        'Send an exit command; server will close the connection
        telnet1.Write("exit" & Constants.vbCr)

        'Read any remaining data before the shutdown
        telnet1.Marshal(telnet1.ReadToEnd(), "", Nothing)
    Catch ex As Exception
        'Report errors to the UI thread
        telnet1.Marshal(ex)
    End Try
End Sub

Private Sub telnet1_Data(ByVal sender As Object, ByVal e As DataEventArgs)
    'Add data received to the textbox
    textBox1.AppendText(e.Data.ToString())
End Sub

Private Sub telnet1_Error(ByVal sender As Object, ByVal e As ErrorEventArgs)
    'Add error messages to the textbox
    textBox1.AppendText(e.GetException().Message)
End Sub

Private Sub telnet1_StateChanged(ByVal sender As Object, ByVal e As EventArgs)
    'Change appearance of textbox when connected
    textBox1.BackColor = If((telnet1.State = ConnectionState.Closed), SystemColors.ControlDark, SystemColors.ControlLight)
End Sub
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         Dart.Emulation.ComponentBase
            Dart.Emulation.SocketBase
               Dart.Emulation.TcpBase
                  Dart.Emulation.PtyBase
                     Dart.Emulation.Telnet

See Also

Reference

Telnet Members
Dart.Emulation Namespace


PowerTCP Emulation for .NET Documentation Version 4.7
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic