Using the Rlogin class |
The Rlogin class allows you to connect to a remote host as you would with a Telnet session. Unlike telnet, however, Rlogin does not support option negotiation and therefore is simpler to use. To establish an Rlogin session with a remote host you first instantiate Rlogin, assign event handlers, and pass the desired remote host hostname and username to the Connect method.
Example
This example demonstrates an interactive session using the Rlogin class.
[C#]
using Jscape.Telnet;
namespace RloginTest {
class RloginExample {
public RloginExample() {
// Instantiate Rlogin
Rlogin rlogin = new Rlogin();
// Assign delegate methods to capture events
rlogin.ConnectedEvent += new Rlogin.RloginConnectedEventHandler(OnConnected);
rlogin.DataReceivedEvent += new Rlogin.RloginDataReceivedEventHandler(OnDataReceived);
// Connect to Remote Host
rlogin.Connect("remotehost.com", "username");
string line = null;
while((line = Console.ReadLine()) != null) {
rlogin.Send(line + "\r\n");
}
// Disconnect from Remote Host
rlogin.Disconnect();
}
public void OnDataReceived(object sender, RloginDataReceivedEventArgs e) {
byte[] buffer = e.Data;
string data = rlogin.Encoding.GetString(buffer);
Console.Write(data);
}
public void OnConnected(object sender, RloginConnectedEventArgs e) {
Console.WriteLine("Connected.");
}
/// The main entry point for the application.
[STAThread]
static void Main(string[] args) {
RloginExample example = new RloginExample();
}
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class RloginExample
Public WithEvents myRlogin As Rlogin
Dim connect As Boolean = False
Public Sub New(ByVal hostname As String)
' Instantiate Rlogin
myRlogin = New Rlogin
' Connect to remote host
myRlogin.Connect(hostname, "sconcepts")
' Begin reading and writing data to remote host
Dim dataIn As String = ""
Do
dataIn = Console.ReadLine()
If (dataIn <> "exit") Then
myRlogin.Send(dataIn & vbCr)
Else
Me.Connected = False
End If
Loop While Me.Connected = True
' Disconnect from remote host
myRlogin.Disconnect()
End Sub
Property Connected() As Boolean
Get
Return connect
End Get
Set(ByVal Value As Boolean)
connect = Value
End Set
End Property
Public Shared Sub Main()
Dim hostname As String
Dim example As RloginExample
Console.Write("Remote Host: ")
hostname = Console.ReadLine()
If (hostname <> "") Then
example = New RloginExample(hostname)
End If
End Sub
Public Sub OnConnected(ByVal sender As Object, ByVal e As RloginConnectedEventArgs) Handles myRlogin.ConnectedEvent
Me.Connected = True
Console.WriteLine("Connected to {0}", e.Hostname)
End Sub
Private Sub OnDisconnected(ByVal sender As Object, ByVal e As RloginDisconnectedEventArgs) Handles myRlogin.DisconnectedEvent
Console.WriteLine("Disconnected.")
Me.Connected = False
End Sub
Private Sub OnDataReceived(ByVal sender As Object, ByVal e As RloginDataReceivedEventArgs) Handles myRlogin.DataReceivedEvent
Console.Write(myRlogin.Encoding.GetString(e.Data))
End Sub
End Class