Check Connection

+1 vote
asked Jun 16, 2015 by ndsinfodeveloper (290 points)

I have written code to download files from multiple Remote ocation.
I write a loop for each Remote Location.
In this loop, there is a code to connect Remote Server.
try
{
if (ServerType == "SFTP")
{

                if (ndsSFTP == null)
                {
                    ndsSFTP = new FileTransferClient();

                }
                ndsSFTP.Settings.UseLargeBuffers = true;




                if (!ndsSFTP.IsConnected)
                {
                    string Host = sFtpAddress;
                    string  UserName = sFtpUserName;
                    string  Password = sFtpPassword;
                    int Port = Convert.ToInt32(!string.IsNullOrEmpty(sPort) ?
                        sPort : "22");

                     ndsSFTP.Connect(Host, Port, FileTransferMode.Sftp);
                    ndsSFTP.Login(UserName,Password);
                    string str = ndsSFTP.GetCurrentDirectory();

                }
                else
                {
                    ndsSFTP.Disconnect();

                    string Host = sFtpAddress;
                    string UserName = sFtpUserName;
                    string Password = sFtpPassword;
                    int Port = Convert.ToInt32(!string.IsNullOrEmpty(sPort) ?
                        sPort : "22");

                     ndsSFTP.Connect(Host, Port, FileTransferMode.Sftp);                        

ndsSFTP.Login(UserName, Password);

                }
            }
            else if (ServerType == "FTPS")
            {
                if (ndsSFTP == null)
                {
                    ndsSFTP = new FileTransferClient();

                }
        ndsSFTP .Settings.UseLargeBuffers = true;

                if (!ndsSFTP .IsConnected)
                {
                    string Host = sFtpAddress;

                    int Port = 990;                        
                    ndsSFTP.Passive = true;

                    ndsSFTP.Settings.SslClientCertificateRequestHandler = new RequestHandler();
                    ndsSFTP.ValidatingCertificate += new EventHandler<SslCertificateValidationEventArgs>(ConsoleVerifier.ValidatingCertificate);
                    ndsSFTP.Connect(Host,Port,FileTransferMode.FtpSslImplicit);

                    string UserName = sFtpUserName;
                    string Password = sFtpPassword;
                    ndsSFTP.Login(UserName, Password);

                }
                else
                {
                    ndsSFTP.Disconnect();


                    int Port = 990;
                    ndsSFTP.Passive = true;

                    ndsSFTP.Settings.SslClientCertificateRequestHandler = new RequestHandler();
                    ndsSFTP.ValidatingCertificate += new EventHandler<SslCertificateValidationEventArgs>(ConsoleVerifier.ValidatingCertificate);
                    ndsSFTP.Connect(Host, Port, FileTransferMode.FtpSslImplicit);

                    string UserName = sFtpUserName;
                    string Password = ssFtpPassword;
                    ndsSFTP.Login(UserName, Password);

                }
            }               

            return true;
        }
        catch (Exception ex)
        {
           return false;
        }

Sometime It will throws error "Client is alreday connected". and conitnuiously for each Remote Location till i restart the application

Please Let me know where i am wrong in this code and why it is happened?

commented Jun 17, 2015 by Tomas Knopp (58,560 points)
Have you had a chance to create the log? Seeing it we would be able to tell what is going on better than from seeing the partial code that you sent  - two possibilites for the error - A) the error message suggests that you tried to call the Connect method for the second time after the client instance has already been connected - make sure you are using the FileTransferClient for one connection only (or Disconnect before calling Connect method again). Second option - there might be a problem in Rebex FileTransferClient. So it would be good to see the log for sure. Thank you for letting us know.
commented Jun 18, 2015 by ndsinfodeveloper (290 points)
You are right that this is partial code but we are using this code for connection from where we we found this error. I have written DisConnect() method if we found SFTP.IsConnected = true. So can you confirm it that Disconnect will disconnect the connection immediately and IsConnected Method gives true value if Client is connected. If Channel is closed means connection is disconnected or not because i had face error "Channel is close" and then repeatadely throws "Client is Connected" for each Remote location  while each remote location has diffrent IP and User name password
commented Jun 18, 2015 by Tomas Knopp (58,560 points)

2 Answers

0 votes
answered Jun 16, 2015 by Tomas Knopp (58,560 points)

Please create a log of communication and either post it here, or even better send it to support@rebex.net.

0 votes
answered Jun 18, 2015 by Tomas Knopp (58,560 points)

If you get an exception the key is to handle it properly in your code.

Unless the Status of the exception is set to ProtocolError, then you will have to reconnect for sure. The IsConnected property cannot be used for checking the actual connection state, see the Note in its documentation: "There is no guarantee that the session is still connected even though IsConnected returns true."

The only way to really know for sure you that are still connected to the SFTP/FTP or any other server is to send a command and receive the response, the easiest with Rebex FTP or SFTP would be to do:

Sftp sftp = new Sftp();
sftp.GetInfo("."); // or sftp.GetInfo("/");

If the above succeeds then your connection is active and working.

...