【发布时间】:2014-08-27 08:56:16
【问题描述】:
Renci SSH.Net 遇到了一个奇怪的问题:
var sftp = new SftpClient(remoteHost, remotePort, remoteUserName, remotePassword);
try
{
sftp.Connect();
using (var file = new FileOutputStream(filePath))
{
sftp.DownloadFile(remoteFileName, file);
}
sftp.Disconnect(); // *
}
catch (Exception ex)
{
// log stuff
throw;
}
finally
{
sftp.Dispose();
}
上面的代码在// * 处抛出SshConnectionException:“客户端未连接”,即使在产生true 之前检查sftp.IsConnected。
文件按预期下载。
stacktrace如下:
at Renci.SshNet.Session.SendMessage(Message message)
at Renci.SshNet.Session.SendDisconnect(DisconnectReason reasonCode, String message)
at Renci.SshNet.Session.Disconnect()
at Renci.SshNet.BaseClient.Disconnect()
at My.Program.MyMethod() in c:\path\to\my\program.cs:line 42
【问题讨论】:
-
我目前的工作是
catch (SshConnectionException ex) { }- 这并不理想 -
我遇到了同样的问题和以下异常: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x80004005): No connection could be made because the target machine positively denied it [fe80::1486:3389 :4e77:f708%10]:22 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) 在 Renci.SshNet.Session.SocketConnect(字符串主机,Int32 端口) [链接]stackoverflow.com/questions/2972600/…