How to detect internet connection loss during transfer?

Advertisement

doydoy
Joined:
Posts:
2

How to detect internet connection loss during transfer?

Hey everyone, I'm fairly new to WinSCP in .NET. I have been using SSH.NET so far, but I can't make it handle internet connection losses properly. And since WinSCP offers resuming file transfer after disconnection, I just HAD to try it :)

Currently I am testing a situation where a user would download 1 file only, while they are conencted through a VPN. Turning off the VPN doesn't seem to faze WinSCP as the download resumes after a while without issue. Yet turning all internet connections off (Wifi in this case) just makes the transfer stay on a lull, with no exceptions thrown or any prompt whatsoever.

My question is, is there a way where WinSCP can react to a complete internet connection loss? Or at least a disconnection from the server?

I have had a look (and tested) both Session.GetFiles() and Session.GetFileToDirectory().
GetFiles does have the Check() method to throw any errors, but it does not seem to do anything.

Any feedback is appreciated. Thanks in advance! :)

Edit: I use the SFTP protocol, if it helps.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
36,510
Location:
Prague, Czechia

Re: How to detect internet connection loss during transfer?

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

doydoy
Joined:
Posts:
2

Log attached!

The wifi connection was turned off at 13:56. However, I can see that WinSCP was attempting to reconnect with the server several times. During this, no output was given. It was only when I turned the wifi on again when the session went on to resume the transfer, and then I shut down the debugging process.

For reference, here is how my session is configured:
static void Main(string[] args)
{
    try
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
            HostName = "SERVER_URL",
            UserName = "USERNAME",
            SshPrivateKeyPath = @"C:\PuttyPrivateKey.ppk",
            SshHostKeyPolicy = SshHostKeyPolicy.GiveUpSecurityAndAcceptAny,
            Timeout = new TimeSpan(0, 0, 15)
        };
 
        using (Session session = new Session())
        {
            session.FileTransferProgress += SessionFileTransferProgress;
            session.SessionLogPath = @"C:\connection_loss_log_1.log";
            
            // Connect
            session.Open(sessionOptions);
 
            if (!session.FileExists(fileToDownload))
            {
                Console.WriteLine("File does not exist!");
                session.Close();
            }
            else
            {
                RemoteFileInfo remoteFileInfo = session.GetFileInfo(fileToDownload);
                remoteFileSize = remoteFileInfo.Length;
 
                // Download files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                string fullDestPath = Path.Combine(destinationPath, fileToDownload);
                TransferOperationResult transferResult = session.GetFiles(fileToDownload, fullDestPath, false, transferOptions);
 
                // Throw on any error
                transferResult.Check();
 
                session.Close();
            }
 
        }
    }
    catch (SessionLocalException sle)
    {
        Console.WriteLine("SessionLocalException " + sle.Message);
    }
    catch (SessionRemoteException sre)
    {
        Console.WriteLine("SessionRemoteException " + sre.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error in ProcessEdi() while downloading EDI files via FTP: Message:" + ex.Message + "Stacktrace: " + ex.StackTrace);
    }
Console.WriteLine("End.");
            Console.ReadLine();
}
 
static void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
{
    long currentSize = (long)(remoteFileSize * e.FileProgress);
    Console.WriteLine("Progress: " + currentSize +" of " + remoteFileSize);
 
    Console.WriteLine("OVerallProgress: " + e.OverallProgress);
 
 
 
    Console.WriteLine("==========================");
}
  • connection_loss_log_1.log (19.14 KB, Private file)

Reply with quote

martin
Site Admin
martin avatar

In the log I see that WinSCP reconnected successfully after the connection was restored. What do you want it to do differently?

Reply with quote

Advertisement

You can post new topics in this forum