3

I'm having a strange issue with 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();
}

The above code throws at // * with the SshConnectionException: "Client not connected", even though on inspecting sftp.IsConnected just before yields true.

The file downloads as expected.

The stacktrace is as follows:

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
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • 1
    My current work around is to `catch (SshConnectionException ex) { }` -which isn't ideal – dav_i Aug 27 '14 at 12:37
  • I got the same issue and the following exception: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x80004005): No connection could be made because the target machine actively refused it [fe80::1486:3389:4e77:f708%10]:22 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at Renci.SshNet.Session.SocketConnect(String host, Int32 port) [link]https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it[/link] – Canada Wan Apr 25 '18 at 20:10

1 Answers1

2

I have the same problem as well.Please go through this https://sshnet.codeplex.com/workitem/1561 to find out the cause. Here is my current work around:

        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);
            }
        }
Rishab
  • 143
  • 14