PowerTCP Emulation for .NET
Login(Credentials) Method
Example 




Credentials including username, password and command prompt.
Logs into a telnet server using properties found in the settings parameter.
Syntax
Public Function Login( _
   ByVal credentials As Credentials _
) As Data
Dim instance As Telnet
Dim credentials As Credentials
Dim value As Data
 
value = instance.Login(credentials)
public Data Login( 
   Credentials credentials
)
public: Data* Login( 
   Credentials* credentials
) 
public:
Data^ Login( 
   Credentials^ credentials
) 

Parameters

credentials
Credentials including username, password and command prompt.

Return Value

A Data object containing the data received during the login.
Remarks

For an established connection, this function will send Credentials.Username and Credentials.Password after receiving login prompts. It will then read the data sent by the remote host up to the Credentials.CommandPrompt.

The return value contains a record of all data received during this process. A return value with no data typically indicates that the server closed the connection.

This method blocks until the login process completes successfully, the server does not respond with an expected prompt within six seconds, or the server closes the connection.

A DataException will be thrown if the password or login prompt is not provided by the server within the timeout period specified, or if any other exception occurs. Examine the DataException.DataRead property for data read up to the exception, and the InnerException for the exception wrapped by DataException. An exception will NOT be thrown if a timeout occurs waiting for the command prompt. Instead, the method will return and the Data.Delimiter will be null. Note that a successful login with an incorrectly specified command prompt will result in the method blocking for the timeout period before returning with the actual prompt, manifesting in a perceived delay. An incorrect password could result in a similar delay.

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
See Also

Reference

Telnet Class
Telnet Members


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