How can I set ServerAliveInterval into SFTP

0 votes
asked Mar 5, 2011 by Maximo Rosales (120 points)
edited Jan 22, 2014

I have a SFTP enabled application it uses Rebex to upload and download files to a linux server (RedHat it uses openssh) those files are uploaded on demand. In my app I have one connection which is open during my application is up. However After 1 hour that I did not send/receive any files the connection is closed and despite of I try to reconnect I cannot upload files again. Researching in forums I found this configuration attribute "ServerAliveInterval" in SSH client which they say send null data to server in order to keep alive the connection. My question are : is there any way to prevent connection lost? / Is there any way to set this configuration to my SFTP connection? Source: http://ocaoimh.ie/how-to-fix-ssh-timeout-problems/

Applies to: Rebex SFTP

2 Answers

0 votes
answered Mar 7, 2011 by Lukas Pokorny (102,210 points)
edited Jan 22, 2014

In Rebex SFTP, the same functionality can be achieved by calling the Sftp.Session.KeepAlive method every minute or so. This sends null data to the server to keep the connection alive. Although this is not done automatically by Rebex SFTP (yet), it can be safely called from any thread, which makes reproducing the "ServerAliveInterval" functionality quite easy:

    /// <summary>
    /// Enhanced Sftp class which implements the keep-alive functionality.
    /// Use this instead of Sftp in your code.
    /// </summary>
    public class SftpKeepAlive : Sftp
    {
        // Timer object (needs to be referenced to prevent it from being
        // claimed by the garbage collector).
        private readonly System.Threading.Timer _keepAliveTimer;

public SftpKeepAlive()
        {
            // initialize the timer
            var oneMinute = new TimeSpan(0, 1, 0);
            _keepAliveTimer = new System.Threading.Timer(KeepAliveCallback, null, oneMinute, oneMinute);
        }

private void KeepAliveCallback(object state)
        {
            try
            {
                if (State == SftpState.Ready)
                {
                    // send keep-alive packet to the server
                    Session.KeepAlive();
                }
            }
            catch (Exception ex)
            {
                // log the exception here
            }
        }
    }

If you prefer VB.NET, please let us know!

commented Jan 22, 2014 by Jan Sotola (16,920 points)
edited Jan 22, 2014

Here is the VB.NET version of the sample code:

''' <summary> '
''' Enhanced Sftp class which implements the keep-alive functionality. '    
''' Use this instead of Sftp in your code. '   
''' </summary> '
Class SftpKeepAlive : Inherits Sftp

    ' Timer object (needs to be referenced to prevent it from being '        
    ' claimed by the garbage collector). '
    Private ReadOnly keepAliveTimer As System.Threading.Timer

    Public Sub New()
        Dim oneMinute = New TimeSpan(0, 1, 0)
        keepAliveTimer = New System.Threading.Timer(AddressOf KeepAliveCallback, Nothing, oneMinute, oneMinute)
    End Sub

    Private Sub KeepAliveCallback(o As Object)
        Try
            ' send keep-alive packet to the server '
            If (State = SftpState.Ready) Then
                Session.KeepAlive()
            End If
        Catch ex As Exception
            ' log the exception here '
        End Try
    End Sub

End Class
0 votes
answered Apr 17, 2015 by moski (210 points)
reshown Jan 12, 2016 by Lukas Pokorny

Is this already done?

...