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
2
  • 1
    My current work around is to catch (SshConnectionException ex) { } -which isn't ideal Commented Aug 27, 2014 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]stackoverflow.com/questions/2972600/… Commented Apr 25, 2018 at 20:10

1 Answer 1

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);
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

The link is dead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.