Reputation: 2828
I am trying to access the FTP server using SSH.NET library without any luck. I am providing same credidentials as in FileZilla which are working fine. The SSH throws errow "Socket read operation has timed out". If I use the same code as bellow but without specifying the port :21 I got an error : "User cannot authenitcated" . Can someone privide insights?
string tempHost = @"ftp.mywebsite.com";
string tempUser = @"[email protected]";
string tempPassword = @"try123";
using (SftpClient sftpClient =
new SftpClient((ConnectionInfo)new PasswordConnectionInfo(tempHost,21, tempUser, tempPassword)))
{
sftpClient.Connect();
}
Upvotes: 1
Views: 14293
Reputation: 91
SSH tools (like Renci SSH) are meant to be used on port 22 (secure). If you want to connect to port 21 you need another library. I have used FluentFTP which is not as easy to use as Renci, but gets the job done.
Here is a code sample you can use to upload a file to a server (on version 19.1.2). Regardless if you use port 21 or 22, I would highly recommend at least 500ms between write operations to give time to the server to breath.
using (FtpClient client = new FtpClient())
{
client.Host = ftpAddress;
client.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
client.Port = 21;
client.Connect();
using (Stream s = new FileStream(localPathWithFileName, FileMode.Open))
{
try
{
//log.Info($"Uploading file...");
client.Upload(s, ftpFilePathWithFileName);
//log.Info($"File uploaded!");
}
catch(Exception e)
{
//log.Info($"{e.StackTrace}");
}
finally
{
client.Disconnect();
}
}
}
Upvotes: 1
Reputation: 51
If in case it's still unresolved for you, below code worked for me
using (var sftp = new SftpClient(host, userName, password))
{
sftp.Connect();
//Do some operation
sftp.Disconnect();
}
Ananth
Upvotes: 3