Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a connection routine that attempts to connect to the ftp server. I have a decent handle on that. My issue is that it only catches exceptions during the connection. I need to watch the connection and catch anything that might happen afterwards.

This is part of a MUCH larger program, the gist is that the main program does a bunch of processing and puts files into a folder. It connects to the FTP server, moves files to and from and then processes them.

My thought is to create a thread after successful connection that would stay active and monitor the connection.

Here is what I have....
C#
static public void sftpConnect()
       {

           // Set max attempt number
           int trymax = 10;
           int trycount = 1;
           int connectDelay = 0;

           Random rnd = new();

           while (trycount <= trymax)
           {
               try
               {
                   sFTPFiles = new();
                   sftpc = new(Config.sFTPServer, Config.sFTPServerPort, Config.sFTPServerUID, Config.sFTPServerPWD);
                       Logging.Log(Logging.LogLevel.Info, new string[] { $"Connection Attempt: {trycount}" });
                       Console.WriteLine($"Connection Attempt: {trycount}");
                   sftpc.Connect();
                   if (sftpc.IsConnected)
                   {   // Log successful connection
                       Logging.Log(Logging.LogLevel.Notice, new string[] { $"Successfully Connected to sFTP Server at '{DateTime.Now}" });
                       Console.WriteLine($"Successfully Connected to sFTP Server at '{DateTime.Now}");
                       //Create Listener Thread to Monitor sftpc?
                       trycount = 11;  // Exits Loop
                   }
               }
               catch (Exception ex)
               {   // If max attemps is reached, log at higher level and die
                   if (trycount >= trymax)
                   {
                       Logging.Log(Logging.LogLevel.Critical, new string[] { $"Exceeded Max Attempts.  Refresh Required" });
                       Console.WriteLine($"Exceeded Max Attempts.  Refresh Required");
                       return;  // Need to restart program
                   }
                   else
                   {   // Log and try again
                       Logging.Log(Logging.LogLevel.Error, new string[] { $"[Reconnect]: Exception Occured at {DateTime.Now}: {ex.Message}.  Will attempt to reconnect." });
                       Console.WriteLine($"[Reconnect]: Exception Occured at {DateTime.Now}: {ex.Message}.  Will attempt to reconnect.");
                       trycount++;

                       // Waits between 5 and 10 seconds to try again
                       connectDelay = rnd.Next(5000, 10000);
                       Thread.Sleep(connectDelay);
                       continue;


                   }
               }
           }
       }


What I have tried:

I'm thinking of something like this for the listener, but I'm not good with threads, so I don't know how to create a thread that would monitor and reconnect, and not create a spaghetti mess of threads.

C#
static public void sftpListener(sftpClient sftpc)
          {
              while (sftpc.IsConnected)
              {
                  try
                  {
                      // Not sure what to put here
                  }
                  catch (Exception ex)
                  {
                      // Log Exception
                      if (!sftpc.IsConnected)
                          sftpConnect();
                  }
              }
          }
Posted
Comments
Maciej Los 20-Feb-22 8:30am    
Why do you want to monitor the connection to ftp server? If your application has to move files between ftp server and client, you should add error handling to the functions responsible for data exchanging.
[EDIT]
Take a look here: c# - FileSystemWatcher for FTP - Stack Overflow[^]
There's an answer provided by [shafqat ali] which had created FtpFileSystemWatcher class. This might be helpful in monitoring file uploading/downloading process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900