Receiving data |
Data sent by the telnet server may be obtained by capturing the DataReceivedEvent fired by the Telnet class.
Capturing the DataReceievedEvent event
To capture and respond to the DataReceivedEvent do the following:
1. | Create a method that has the same signature as the DataReceivedEventHandler delegate. |
2. | Add code within the newly created method to handle the event. |
3. | Subscribe newly created method to capture the DataReceivedEvent when fired. |
Example
[C#]
using System;
using Jscape.Telnet;
public class Test {
private Telnet telnet = null;
private string hostname = null;
public Test(string hostname) {
// set hostname
this.hostname = hostname;
// instantiate Telnet instance
telnet = new Telnet(hostname);
// delegate OnDataReceived method to capture DataReceivedEvent
telnet.DataReceivedEvent += new Telnet.DataReceivedEventHandler(this.OnDataReceived);
}
public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {
// get data and print to console
Console.Write(Encoding.ASCII.GetString(e.Data));
}
}
[Visual Basic]
Imports System
Imports Jscape.Telnet
Public Class Test
Private WithEvents myTelnet As Telnet
Private hostname As String = ""
Public Sub Test(ByVal hostname As String)
' set hostname
Me.hostname = hostname
' instantiate Telnet instance
myTelnet = New Telnet(hostname)
End Sub
Private Sub OnDataReceived(ByVal sender As Object, ByVal e As TelnetDataReceivedEventArgs) Handles myTelnet.DataReceivedEvent
' get data and print to console
Console.WriteLine(myTelnet.Encoding.GetString(e.Data))
End Sub
End Class
See Also