Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client not connected exception with SSH.Net

Tags:

c#

ssh.net

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
like image 411
dav_i Avatar asked Mar 14 '26 18:03

dav_i


1 Answers

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);
            }
        }
like image 63
Rishab Avatar answered Mar 17 '26 06:03

Rishab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!