Dart.Emulation Namespace : Telnet Class |
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.
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 SendCommand to send any non-option control command to the host.
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.
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
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Dart.Emulation.ComponentBase
Dart.Emulation.SocketBase
Dart.Emulation.TcpBase
Dart.Emulation.PtyBase
Dart.Emulation.Telnet