
Visual Basic (Declaration) | |
---|---|
Public Class Telnet Inherits Dart.Common.PtyBase |
Visual Basic (Usage) | ![]() |
---|---|
Dim instance As Telnet |
C# | |
---|---|
public class Telnet : Dart.Common.PtyBase |
Managed Extensions for C++ | |
---|---|
public __gc class Telnet : public Dart.Common.PtyBase |
C++/CLI | |
---|---|
public ref class Telnet : public Dart.Common.PtyBase |
C# | ![]() |
---|---|
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 DataEventHandler(telnet1_Data); telnet1.Error += new Dart.Common.ComponentErrorEventHandler(telnet1_Error); telnet1.StateChanged += new EventHandler(telnet1_StateChanged); //Start the process on a worker thread telnet1.Start(automateSession, null); } private void automateSession(Telnet telnet, object notUsed) { //This function executes on a worker thread, providing multi-threaded, asynchronous operation try { //Connect to the server telnet.Connect("myServer"); //Login and marshal data to the UI thread telnet.Marshal(telnet.Login("myUsername", "myPassword", "$"), null); //Send a list command telnet.Write("ls -la\r"); //Wait for prompt, marshal data to the UI thread telnet.Marshal(telnet.Read("$"), null); //Send an exit command; server will close the connection telnet.Write("exit\r"); //Read any remaining data before the shutdown telnet.Marshal(telnet.ReadToEnd(), null); } catch (Exception ex) { //Report errors to the UI thread telnet.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, Dart.Common.ComponentErrorEventArgs 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; } |
Visual Basic | ![]() |
---|---|
Private telnet1 As Telnet Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click '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 telnet As Telnet, ByVal notUsed As Object) 'This function executes on a worker thread, providing multi-threaded, asynchronous operation Try 'Connect to the server telnet.Connect("myServer") 'Login and marshal data to the UI thread telnet.Marshal(telnet.Login("myUsername", "myPassword", "$"), Nothing) 'Send a list command telnet.Write("ls -la" & Constants.vbCr) 'Wait for prompt, marshal data to the UI thread telnet.Marshal(telnet.Read("$"), Nothing) 'Send an exit command; server will close the connection telnet.Write("exit" & Constants.vbCr) 'Read any remaining data before the shutdown telnet.Marshal(telnet.ReadToEnd(), Nothing) Catch ex As Exception 'Report errors to the UI thread telnet.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 Dart.Common.ComponentErrorEventHandler) '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 If (telnet1.State = ConnectionState.Closed) Then textBox1.BackColor = SystemColors.ControlDark Else textBox1.BackColor = SystemColors.ControlLight End If End Sub |
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.
-
Connecting: To connect to a host, use the Connect method. Once connected, establish a secure connection using AuthenticateAsClient.
-
Default option negotiation: The Telnet component uses a default option negotiation sequence which occurs automatically after login. To add or remove options to negotiate, modify the ClientOptions and ServerOptions collections appropriately. The ClientOptionChanged and ServerOptionChanged events fire when the state of an option changes.
-
Customized option negotiation: Manual option negotiation is also possible. Use the SendOption call to send option commands, and and the CommandReceived event to examine server responses.
-
Sending Data: Once a connection is established, send data using the Write method, which includes overloads for sending data as byte arrays or as strings.
-
Receiving Data: Receive data using the Read method. This method returns a Data object containing data read off the data stream. Data in this object can easily be converted to a string using its ToString method. The Read method includes overloads for reading until a specified delimiter is found. A set of delimiters can also be specified for situations in which one of several responses may be received. Marshal data to the Data event by calling the Marshal method.
-
Sending commands: Use the SendMethod method to send any non-option control command to the host.
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.
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Dart.Common.ComponentBase
Dart.Common.SocketBase
Dart.Common.TcpBase
Dart.Common.PtyBase
Dart.Emulation.Telnet
Target Platforms: Microsoft .NET Framework 2.0