PowerTCP Telnet for .NET CF
Login(String,String,String) Method
See Also  Example Send comments on this topic.
Dart.Telnet Namespace > Telnet Class > Login Method : Login(String,String,String) Method




username
The username provided for login.
password
The password provided for login.
commandPrompt
The command prompt used by the remote host.
Logs into a telnet server using standard prompts.

Syntax

Visual Basic (Declaration) 
Public Overloads Function Login( _
   ByVal username As String, _
   ByVal password As String, _
   ByVal commandPrompt As String _
) As Data
Visual Basic (Usage)Copy Code
Dim instance As Telnet
Dim username As String
Dim password As String
Dim commandPrompt As String
Dim value As Data
 
value = instance.Login(username, password, commandPrompt)
C# 
public Data Login( 
   string username,
   string password,
   string commandPrompt
)
Managed Extensions for C++ 
public: Data* Login( 
   string* username,
   string* password,
   string* commandPrompt
) 
C++/CLI 
public:
Data^ Login( 
   String^ username,
   String^ password,
   String^ commandPrompt
) 

Parameters

username
The username provided for login.
password
The password provided for login.
commandPrompt
The command prompt used by the remote host.

Return Value

A Data object containing the data received during the login.

Example

The following example demonstrates execution of an automated session using the Telnet component.
C#Copy Code
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.Text += e.Data.ToString();
}

void telnet1_Error(object sender, Dart.Common.ComponentErrorEventArgs e)
{
    //Add error messages to the textbox
    textBox1.Text += e.GetException().Message;
}

void telnet1_StateChanged(object sender, EventArgs e)
{
    //Change appearance of textbox when connected
    textBox1.BackColor = (telnet1.State == Dart.Common.ConnectionState.Closed)
    ? SystemColors.ControlDark
    : SystemColors.ControlLight;
}
Visual BasicCopy Code
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.Text += 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.Text += 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 = Dart.Common.ConnectionState.Closed) Then
        textBox1.BackColor = SystemColors.ControlDark
    Else
        textBox1.BackColor = SystemColors.ControlLight
    End If
End Sub

Remarks

For an established connection, this function will send username and password after receiving standard login prompts ("gin:" and "word:", respectively). It will then read the data sent by the remote host up to the commandPrompt.

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

A SocketException will be thrown with an ErrorCode of 10060 (receive timeout) if the password or login prompt is not provided by the server within 6 seconds from when it is expected.

This method blocks until the login process completes successfully, the default receive timeout period of 6 seconds expires on any socket read, or the server closes the connection.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 4.2
© 2010 Dart Communications. All Rights Reserved.