PDA

View Full Version : Dont/Do/Wont/Will event handlers


akagus
08-06-2008, 04:57 AM
I have successfully connected to the Telnet server using the following code. However, connection is only successfull when I comment out the Dont/Do/Wont/Will event handlers.

What is function of Dont/Do/Wont/Will event handlers?

Here is my complete code:

Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

Dim hostname As String
Dim username As String
Dim password As String
Dim shellPrompt As String
Dim script As TelnetScript

hostname = "10.92.92.92"

' create new TelnetmyTelnetinstance
myTelnet = New Telnet(hostname)

' uncomment line below and enter license key
myTelnet.LicenseKey = "myKey"

' register event handlers
AddHandler myTelnet.DontOptionEvent, AddressOf OnDontOption
AddHandler myTelnet.DoOptionEvent, AddressOf OnDoOption
AddHandler myTelnet.WontOptionEvent, AddressOf OnWontOption
AddHandler myTelnet.WillOptionEvent, AddressOf OnWillOption
AddHandler myTelnet.ConnectedEvent, AddressOf OnConnected
AddHandler myTelnet.DisconnectedEvent, AddressOf OnDisconnected
AddHandler myTelnet.DataReceivedEvent, AddressOf OnDataReceived

username = "TestUser
password = "TestPassword"
shellPrompt = "$"

' create new TelnetScript instance
script = New TelnetScript(myTelnet)

' create login task
Dim loginTask As TelnetTask = New TelnetTask("Username:", username, "Password:")
loginTask.Terminator = ControlChars.CrLf
loginTask.StartPromptRegex = False
loginTask.EndPromptRegex = False

' create password task
Dim passwordTask As TelnetTask = New TelnetTask("Password:", password, shellPrompt)
passwordTask.Terminator = ControlChars.CrLf
passwordTask.StartPromptRegex = False
loginTask.EndPromptRegex = False

' create dir task
Dim dirTask As TelnetTask = New TelnetTask(shellPrompt, "DIR/OUT=DEF.TXT", shellPrompt)
dirTask.Terminator = ControlChars.CrLf
dirTask.StartPromptRegex = False
dirTask.EndPromptRegex = False

' add tasks to script
script.AddTask(loginTask)
script.AddTask(passwordTask)
'script.AddTask(dirTask)

' establish connection
myTelnet.Connect()

' wait for last task to complete
Dim tail As TelnetTask = script.Tail

While Not tail.IsComplete
Thread.Sleep(100)
End While

' disconnect
myTelnet.Disconnect()

End Sub

Public WithEvents myTelnet As Telnet


Public Sub OnConnected(ByVal sender As Object, ByVal e As TelnetConnectedEventArgs) Handles myTelnet.ConnectedEvent
' Tell user we are connected.
Debug.Print("Connected to", e.Host, e.Port)
End Sub

Private Sub OnDisconnected(ByVal sender As Object, ByVal e As TelnetDisconnectedEventArgs) Handles myTelnet.DisconnectedEvent
' Tell user we disconnected.
Debug.Print("Disconnected.")
End Sub

Private Sub OnDoOption(ByVal sender As Object, ByVal e As TelnetDoOptionEventArgs) Handles myTelnet.DoOptionEvent
myTelnet.SendWontOption(e.Option)
End Sub

Private Sub OnDontOption(ByVal sender As Object, ByVal e As TelnetDontOptionEventArgs) Handles myTelnet.DontOptionEvent
myTelnet.SendDontOption(e.Option)
End Sub

Private Sub OnWillOption(ByVal sender As Object, ByVal e As TelnetWillOptionEventArgs) Handles myTelnet.WillOptionEvent
myTelnet.SendDontOption(e.Option)
End Sub

Private Sub OnWontOption(ByVal sender As Object, ByVal e As TelnetWontOptionEventArgs) Handles myTelnet.WontOptionEvent
myTelnet.SendWontOption(e.Option)
End Sub

Private Sub OnDataReceived(ByVal sender As Object, ByVal e As TelnetDataReceivedEventArgs) Handles myTelnet.DataReceivedEvent
' Write characters of byte data received from TELNET server using default encoding (UTF8)
Debug.Print(myTelnet.Encoding.GetString(e.Data))
End Sub

vglass
08-08-2008, 12:34 AM
It is interesting that you are unable to connect when the Do/Dont/Will/Wont options are enabled. Generally this is exactly the opposite behavior you would expect to see since telnet servers require that options be negotiated before displaying any sort of login prompt. The purpose of these events are to negotiate options between client and server. Options include things like line length, enable/disable echo etc.

Are you able to determine what event/option it is hanging on? This could prove helpful.