第二连接上的ssh.net sshoperation timeout
我要以第二种形式连接到SSH。当我第一次执行Connect()命令时,一切正常。如果我关闭表单并尝试重新连接,则在调用Connect()之后将获得SshoperationTimeout。
ConnectionInfo connNfo = new ConnectionInfo(ip, 22, username,
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod(username,password),
}
)
client = new SshClient(connNfo);
try
{
client.Connect();
}
catch
{
MessageBox.Show("Error","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
this.Close();
return;
}
shellStreamSSH = client.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
shellThread = new Thread(() => recvSSHData(shellStreamSSH));
shellThread.Start();
的控制台方法输出的方法:
void recvSSHData(ShellStream shellStreamSSH)
{
while (true)
{
try
{
if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
{
string strData = shellStreamSSH.Read();
appendRichTextBox(strData);
}
}
catch
{}
Thread.Sleep(200);
}
}
打印称为关闭和断开SSH客户端
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
if (shellThread != null && shellThread.IsAlive)
{
shellThread.Abort();
}
if (shellStreamSSH != null)
{
shellStreamSSH.Close();
shellStreamSSH.Dispose();
}
if (client != null && client.IsConnected)
{
client.Disconnect();
client.Dispose();
}
}
I'm connecting to the ssh on a second form. When I first execute the connect() command, everything works fine. If I close the form and try to reconnect I get SshOperationTimeout after the method connect() is called.
ConnectionInfo connNfo = new ConnectionInfo(ip, 22, username,
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod(username,password),
}
)
client = new SshClient(connNfo);
try
{
client.Connect();
}
catch
{
MessageBox.Show("Error","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
this.Close();
return;
}
shellStreamSSH = client.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
shellThread = new Thread(() => recvSSHData(shellStreamSSH));
shellThread.Start();
Method to print the output of the console
void recvSSHData(ShellStream shellStreamSSH)
{
while (true)
{
try
{
if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
{
string strData = shellStreamSSH.Read();
appendRichTextBox(strData);
}
}
catch
{}
Thread.Sleep(200);
}
}
Method called to close and disconnect the ssh client:
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
if (shellThread != null && shellThread.IsAlive)
{
shellThread.Abort();
}
if (shellStreamSSH != null)
{
shellStreamSSH.Close();
shellStreamSSH.Dispose();
}
if (client != null && client.IsConnected)
{
client.Disconnect();
client.Dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论