1

I am using Renci.ssh library in my .net console app to download content from secured FTP(SFTP). All I do is connect using SFTPClient of Renci.ssh library and download content and once the download is completed, I try to disconnect using same SFTPClient instance and application behaves bit strange. Sometimes it doesnt throw any error, at times it throws "Client not connected" or "Error while connecting to something that is not a Socket". I am clueless as to why app is behaving strangely.

Code:

try
        {
            ConnectionInfo conInfo = new PasswordConnectionInfo("********.com", username, password);
            using (SftpClient client=new SftpClient(conInfo))
            {
                client.Connect();
                Console.WriteLine("Connection before sleep: " + client.IsConnected);
                Thread.Sleep(10 * 1000);
                Console.WriteLine("Connection after sleep: " + client.IsConnected);
                client.Disconnect();
            }

        }
        catch (Exception ex)
        {
        }

StackTrace:

at System.Net.Sockets.Socket.Poll(Int32 microSeconds, SelectMode mode) at Renci.SshNet.Extensions.CanWrite(Socket socket) at Renci.SshNet.Session.SendMessage(Message message) at Renci.SshNet.Session.SendDisconnect(DisconnectReason reasonCode, String message) at Renci.SshNet.BaseClient.Disconnect() at TestSFTP.Program.Main(String[] args) in C:\Users*********\TestSFTP\TestSFTP\Program.cs:line 26

kedarK
  • 213
  • 4
  • 17
  • 2
    Is your SFTPClient instance wrapped in a using statement so its Dispose method gets called? It helps a lot if you provide an [mcve] because then we can run the code our self and provide you with the best answer. – rene Jun 27 '18 at 09:02
  • No it isnt under "using" statement. `ConnectionInfo conInfo = new PasswordConnectionInfo("*********.com", username, password); SftpClient client = new SftpClient(conInfo); client.Connect(); client.Disconnect(); ` – kedarK Jun 27 '18 at 09:07
  • 1
    Sftpclient overrides [Dispose](https://github.com/sshnet/SSH.NET/blob/develop/src/Renci.SshNet/SftpClient.cs#L2178) so you better do wrap all that code in a [using statement](https://stackoverflow.com/questions/75401/what-are-the-uses-of-using-in-c-sharp). and then retry if you see the same behavior. – rene Jun 27 '18 at 09:17
  • Yes tried, but no luck – kedarK Jun 27 '18 at 09:27
  • Please [edit] your question to include an MCVE. The code you posted in the comment earlier makes no sense. Also include how you applied the using statement. – rene Jun 27 '18 at 09:28
  • Hi Rene, Please check the question now. Thanks – kedarK Jun 27 '18 at 09:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173868/discussion-between-kedark-and-rene). – kedarK Jun 27 '18 at 09:34
  • Show us exception stack trace. – Martin Prikryl Jun 27 '18 at 12:46
  • Do not post important information into comments. Edit your question. – Martin Prikryl Jun 28 '18 at 14:54
  • So the connection is lost while "sleeping". Why do you even "sleep" in the code? – Martin Prikryl Jun 29 '18 at 07:25
  • It loses even If i dont make the thread sleep. I had put it there just to test if there is any operation timeout or anything of that sort. – kedarK Jun 29 '18 at 10:33

0 Answers0