【问题标题】:Client not connected exception with SSH.Net客户端未连接 SSH.Net 异常
【发布时间】: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/…

标签: c# ssh.net


【解决方案1】:

我也有同样的问题。请通过这个https://sshnet.codeplex.com/workitem/1561找出原因。这是我目前的工作:

        catch (Exception ex)
        {
            if (ex is SshConnectionException || ex is SocketException)
            {
                _ifwInstance.Error(string.Format("Ignoring {0} during listing directory", ex.Message));
            }
            else
            {
                Debugger.Log(0, null, ex.InnerException.ToString());
                throw new Exception("Login to SFT FAILED", ex);
            }
        }

【讨论】:

  • 链接失效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多