PowerTCP Telnet for .NET

from $349.00
Available Platforms

See all PowerTCP Telnet for .NET Code Examples

Automated Session Example

The following example demonstrates an automated Telnet session in a Windows Forms environment. The session is executed asynchronously, without blocking the UI. The session is written to the VT display.

 

private void button1_Click(object sender, EventArgs e)
{
    //This application has a User-Interface, which we do not want to block.
    //Start launches the specified method on a worker thread.
    //Event handlers execute on the UI thread.
    telnet1.Start(automateSession, null);
}

private void automateSession(object notUsed)
{
    //This function performs an automated telnet session using blocking calls.
    //Use the Start method to execute this function on a worker thread.
    try
    {
        //Connect to the server
        telnet1.Connect("myServer");

        //Login and marshal data to the UI thread
        telnet1.Marshal(telnet1.Login(new Credentials("myUser""myPass""~$")), ""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 VT display
    if (e.Data != null) vt1.Write(e.Data.ToString());
}

void telnet1_Error(object sender, ErrorEventArgs e)
{
    //Add error messages to the VT display
    vt1.Write(e.GetException().Message);
}

void telnet1_StateChanged(object sender, EventArgs e)
{
    //Indicate in app title when connected
    this.Text = (telnet1.State == Dart.Emulation.ConnectionState.Closed)
        ? "Automated Session Demo"
        : "Automated Session Demo <Connected to " + telnet1.RemoteEndPoint.ToString() + ">";
}

 

To download a trial please visit the PowerTCP Telnet for .NET product page.