Rebex.Net.SftpException: The connection was closed by the server.

0 votes
asked Mar 8 by filo.morales (120 points)

I have an application in C # which sends txt files to a SFTP server, at the moment of starting the application it sends the files without problem, but if it has inactivity of sending files and when sending again it is when the error appears.

How can I control this problem?

Thank you!!

08/03/2018 01:40:25 p.m.Error Archivo TXT: The connection was closed by the server.
08/03/2018 01:40:25 p.m. Rebex.Sftp
08/03/2018 01:40:25 p.m. en Rebex.Net.Sftp.VQ(HPP I, THP J, String C, Stream D, Int64 U, Int64 L, PI B)
en Rebex.Net.Sftp.VQ(HPP I, THP J, String C, String D, Int64 U, Int64 L, Int64 B, PI M)
en Rebex.Net.Sftp.WA(HPP I, String J, String C, Int64 D, Int64 U, Int64 L, PI B)
en Rebex.Net.Sftp.PutFile(String localPath, String remotePath)
en SABINOsftp.Program.OnChanged(Object source, FileSystemEventArgs e)
08/03/2018 01:40:25 p.m. Rebex.Net.SftpException: The connection was closed by the server. ---> Rebex.Net.SshException: The connection was closed by the server.
en Rebex.Net.SshSession.PF(PE I, Boolean J)
en Rebex.Net.SshChannel.DJ(PE I)
en Rebex.Net.SshChannel.Send(Byte[] buffer, Int32 offset, Int32 count)
en Rebex.Net.DHP.UI(Byte[] I, Int32 J, Int32 C)
en Rebex.Net.JVP.BJ(PE I, THP J)
en Rebex.Net.JVP.EJ(PE I, UInt32 J, GVP C, THP D)
en Rebex.Net.JVP.MJ(PE I, UInt32 J, THP C)
en Rebex.Net.JVP.BI(String I, HPP J, Boolean C, THP D)
en Rebex.Net.Sftp.VQ(HPP I, THP J, String C, Stream D, Int64 U, Int64 L, PI B)
--- Fin del seguimiento de la pila de la excepción interna ---
en Rebex.Net.Sftp.VQ(HPP I, THP J, String C, Stream D, Int64 U, Int64 L, PI B)
en Rebex.Net.Sftp.VQ(HPP I, THP J, String C, String D, Int64 U, Int64 L, Int64 B, PI M)
en Rebex.Net.Sftp.WA(HPP I, String J, String C, Int64 D, Int64 U, Int64 L, PI B)
en Rebex.Net.Sftp.PutFile(String localPath, String remotePath)
en SABINOsftp.Program.OnChanged(Object source, FileSystemEventArgs e)

1 Answer

0 votes
answered Mar 9 by Lukas Pokorny (95,290 points)

This looks like the server (or a router along the way) closes the connection after a period of inactivity. There are several solutions to this:

  • Use try/catch blocks to detect this issue (use SftpException object's Status property to determine whether the connection was closed) and react accordingly (reconnect and retry the operation).
  • Prevent the server (or a router along the way) from disconnecting the session by calling Sftp.Settings.KeepAlive method periodically (see the forum post on KeepAlive for sample code).
  • Configure the server (or router along the way) not to disconnect SFTP/SSH sessions after a period of inactivity.
...