Check SFTP connection using WinSCP .NET assembly in C# while loop

141 Views Asked by At

I want to display the live connection status in my console application. The problem is that I'm stuck in a loop and I have no idea how to check correctly the connection status while I'm looping. When the connection is lost (in most cases about ~4min) I want to wait until the connection is back again. Online and Offline time should be displayed in the console all the time.

Thanks for any ideas or help!

using WinSCP;

using (Session session = new Session())
{
    try
    {
        int i = 0;

        // Session Config
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = "xxx.xx.xx.xx",
            UserName = "xxxxxx",
            Password = "xxxxxxxxx",
            SshHostKeyFingerprint = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        };
        // Connect
        session.Open(sessionOptions);

        while (true)
        {
            if (session.Opened == true) //this is displayed all the time
            {
                Console.WriteLine(i++ + "   " + DateTime.Now + "  " + "Online");
                // check again here if session.Opened still true? 
            }
            else if (session.Opened == false) 
            {
                Console.WriteLine(i++ + "   " + DateTime.Now + "  " + "Offline");
            }
        }
    }
    catch (Exception)
    {
   
    }
}

With for-loop:

using WinSCP;

for (int k = 0; k < 100; k++)
{
    using (Session session = new Session())
    {
        try
        {
            int i = 0;

            // Session Config
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "",
                UserName = "",
                Password = "",
                SshHostKeyFingerprint = "",
            };
            
            session.Open(sessionOptions);
            if (session.Opened == true)
            {
                Console.WriteLine(k + "    " + DateTime.Now + "     " + "Online");
                System.Threading.Thread.Sleep(1000);
                session.Close();
            }
            else if (session.Opened == false)
            {
                Console.WriteLine("Timeout");
                System.Threading.Thread.Sleep(1000);
            }

        }
        catch (Exception)
        {
            Console.WriteLine(k + "    " + DateTime.Now + "     " + "Offline");
        }
    }
}
2

There are 2 best solutions below

0
On

The Session class does not actively check the connection. If you do nothing in the loop, it will never find out the connection was lost. Moreover, if the connection is lost, it won't automatically reconnect.

So you have to

  • Try doing something with the connection to make sure it is still active
  • When the connection is lost, keep trying to reconnect

Something like this:

DateTime lastChange = DateTime.Now;
Session session = null;
while (true)
{
    if (session == null)
    {
        session = new Session();
        try
        {
            session.Open(sessionOptions);
            lastChange = DateTime.Now;
        }
        catch (SessionException)
        {
            session.Dispose();
            session = null;
        }
    }
    else
    {
        try
        {
            session.FileExists(session.HomePath);
        }
        catch (SessionException)
        {
            lastChange = DateTime.Now;
            session.Dispose();
            session = null;
        }
    }

    var status = (session != null ? "Online" : "Offline");
    Console.WriteLine($"{status} for {DateTime.Now - lastChange}");

    Thread.Sleep(TimeSpan.FromSeconds(5));
}

Example output:

Online for 00:00:00.0000067
Online for 00:00:05.0721274
Online for 00:00:10.1424717
Online for 00:00:15.2207413
Offline for 00:00:00.1085855
Offline for 00:00:10.2624759
Offline for 00:00:20.3726673
Online for 00:00:00.0000051
Online for 00:00:05.0674031
0
On

I think this what you searching for:

using WinSCP;


using (Session session = new Session())
{
        try
        {
            int i = 0;

            // Session Config
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "xxx.xx.xx.xx",
                UserName = "xxxxxx",
                Password = "xxxxxxxxx",
                SshHostKeyFingerprint = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            };
            // Connect
            session.Open(sessionOptions);

            while (true)
            {
              CheckConnectionStatus();
              //OPTIONAL:Thread.Sleep(5000)
            }
        }
        

        catch (Exception)
        {
       
        }
}

void CheckConnectionStatus()
{
  if (session.Opened)
  {
    Console.WriteLine(i++ + "   " + DateTime.Now + "  " + "Online");
  }
  else 
  {
    Console.WriteLine(i++ + "   " + DateTime.Now + "  " + "Offline");

    try
    {
      session.Open(sessionOptions);
    }
    catch { }
  }
}