SSH.NET - 几秒钟后断开连接而没有活动

时间:2017-02-27 17:39:11

标签: vb.net ssh

美好的一天

问候朋友。这是我第一次在论坛上提问......

我正在使用VB.Net 2015建立SSH连接的应用程序,为此我使用的SSH.NET库对我来说几乎是完美的。当我启动应用程序连接优秀时,我启动第一个命令,一切都没问题,但如果我等待几秒钟连接断开,我已经在3分钟内设置了超时,并且KeepAliveInterval在5秒内仍然断开连接仍然存在。

Option Strict On
Option Explicit On

Imports System.Text
Imports Renci.SshNet
Imports Renci.SshNet.Common

Public Class Form1

  Private oSshClient As SshClient
  Private oShellStream As ShellStream

  Private Delegate Sub RtbCallBack(ByVal sText As String)

  Private Sub SetRtbText(ByVal sText As String)

    If RichTextBox1.InvokeRequired Then

      RichTextBox1.Invoke(New RtbCallBack(AddressOf SetRtbText), {sText})
    Else

      RichTextBox1.AppendText(sText)
    End If
  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

      oSshClient = New SshClient("x.x.x.x", 22, "user", "1234567890")
      oSshClient.KeepAliveInterval = TimeSpan.FromSeconds(5)
      oSshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(3)
      oSshClient.Connect()

      If oSshClient.IsConnected Then

        oShellStream = oSshClient.CreateShellStream("oShellStream", 0, 0, 0, 0, 1024)

        AddHandler oSshClient.ErrorOccurred, AddressOf OnErrorOccurred
        AddHandler oShellStream.DataReceived, AddressOf OnDataReceived
      End If

    Catch ex As Exception

      Debug.Print("Código de Error: " & ex.HResult)

      If ex.HResult = -2146233088 Then

        MsgBox("Usuario y/o Password Incorrectos. Por favor Verifique...!",
               MsgBoxStyle.Critical, "Mesaje del Sistema")

      Else
        MsgBox(ex.Message & vbCrLf & "Código de Error: " & ex.HResult,
               MsgBoxStyle.Critical, "Mesaje del Sistema")
      End If

      If (oSshClient IsNot Nothing) Then

        oSshClient.Dispose()
        oSshClient = Nothing
      End If
    End Try
  End Sub

  Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress

    If Asc(e.KeyChar) = Keys.Enter Then

      e.Handled = True
    End If
  End Sub

  Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown

    If e.KeyCode = Keys.Enter Then

      oShellStream.WriteLine(TextBox2.Text)
      TextBox2.Text = ""
    End If
  End Sub

  Private Sub OnErrorOccurred(ByVal sender As Object, ByVal e As ExceptionEventArgs)

    SetRtbText(e.Exception.Message)
    MsgBox("Paso")
  End Sub

  Private Sub OnDataReceived(ByVal sender As Object, ByVal e As ShellDataEventArgs)

    SetRtbText(Replace(Encoding.Default.GetString(e.Data), vbBack, ""))
  End Sub

  Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

    If oSshClient IsNot Nothing Then

      oSshClient.Disconnect()
      oSshClient.Dispose()
    End If
  End Sub
End Class

请帮我详细说明这个细节。

问候并非常感谢

0 个答案:

没有答案