Client not connected exception with SSH.Net

dav_i picture dav_i · Aug 27, 2014 · Viewed 17.4k times · Source

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

Answer

Rishab picture Rishab · Oct 16, 2014

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);
            }
        }