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?